This is a migrated thread and some comments may be shown as answers.

Get Caret Coordinates

5 Answers 28 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
Alessio Bulleri
Top achievements
Rank 1
Alessio Bulleri asked on 16 Feb 2017, 04:39 PM

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

Sort by
0
Dimitar
Telerik team
answered on 17 Feb 2017, 09:50 AM
Hello Alessio,

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
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Danny
Top achievements
Rank 1
answered on 09 May 2018, 05:17 PM

Hi,

Always pt is {0,0} and not the position of the last character. Why?

0
Dimitar
Telerik team
answered on 10 May 2018, 10:46 AM
Hello Danny,

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
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Danny
Top achievements
Rank 1
answered on 10 May 2018, 05:16 PM

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

0
Dimitar
Telerik team
answered on 11 May 2018, 11:10 AM
Hello Danny,

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
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
TextBox
Asked by
Alessio Bulleri
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Danny
Top achievements
Rank 1
Share this question
or