Hi,
I have a TextBox and I need to open a context menu (that I build dynamically based on the text present in the control) near at the current position of the caret.
How can I get the coordinates of the caret so I can pass them to the menu show method ?
I can change the control to a RadTextControl if needed.
5 Answers, 1 is accepted
Thank you for writing.
You can get the position with the following method:
var pt = ((HostedTextBoxBase) radTextBox1.TextBoxElement.TextBoxItem.HostedControl).GetPositionFromCharIndex(radTextBox1.SelectionStart > 0 ? radTextBox1.SelectionStart - 1 : 0);
Console.WriteLine(pt);
I hope this information is useful. Let me know if you need further assistance.
Regards,
Dimitar
Telerik by Progress
Hi,
Always pt is {0,0} and not the position of the last character. Why?
I have tested this and it is still working on my side (see attached). Consider posting a ticket and attaching your project there. This will allow us to properly investigate this.
Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Progress Telerik
Thanks Dimitar for your quick answer - yes, you're right, the caret coordinates are changing when the cursor is moved/clicked in different portions of the text.
In fact, what I'm trying to do is to write in a multipleLine textbox few lines of text, each of them ending with a LinkLabel, which once clicked, will open an excel file for editing (or whatever....). Running the small piece of code, the LinkLabels are not visible, nor active in the tetxtbox (moving the mouse over the textbox, seems that all Labels are put in the upper left corner of the textbox - at point (0,0), one each over the precedent one. See attached 2 captures: one representing the form with the textbox but without a proper hyperlink (no hand cursor, no clickable text, no underline....), the second the very small piece of code.
Any suggestion about this approach will be really appreciated! (probably, the best solution would be to implement a new Control - a proper HyperlinkLabel, or to use a RichTextcontrol, but for the moment, I would like to know if it's possible to use this way, using a TextBox.
Thanks,
Dan
You should add the label to the hosted textbox. Here is an example:
Dim
label
As
New
LinkLabel()
label.Text =
"Test"
label.Location = pt
Me
.radTextBox1.TextBoxElement.TextBoxItem.HostedControl.Controls.Add(label)
I do not think that this is a good approach. Perhaps if you could share more information about your requirement we will be able to suggest a better solution. Consider using a RadListView with Custom items where you can use various elements inside each item.
I hope this will be useful.
Regards,
Dimitar
Progress Telerik