Get Caret Coordinates

6 posts, 0 answers
  1. Alessio Bulleri
    Alessio Bulleri avatar
    34 posts
    Member since:
    Sep 2008

    Posted 16 Feb 2017 Link to this post

    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.

  2. Dimitar
    Admin
    Dimitar avatar
    2907 posts

    Posted 17 Feb 2017 Link to this post

    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.
  3. Danny
    Danny avatar
    11 posts
    Member since:
    May 2018

    Posted 09 May 2018 in reply to Dimitar Link to this post

    Hi,

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

  4. Dimitar
    Admin
    Dimitar avatar
    2907 posts

    Posted 10 May 2018 Link to this post

    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.
  5. Danny
    Danny avatar
    11 posts
    Member since:
    May 2018

    Posted 10 May 2018 in reply to Dimitar Link to this post

    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

  6. Dimitar
    Admin
    Dimitar avatar
    2907 posts

    Posted 11 May 2018 Link to this post

    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.
Back to Top