Results 1 to 6 of 6

Thread: Inserting String in cursor position Rich text Box

  1. #1
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Inserting String in cursor position Rich text Box

    Hi Guys,
    How do I add a text stored in a variable into the cursor position?

    I know it may be as easy as:
    Code:
    Dim var1 As String = "Text to Insert"
    
    RichTextBox.Cursor.Current = var1
    
    or
    
    RichTextBox1.Text.Insert(RichTextBox.Cursor.Current, var1)
    Thanks in advance,
    Mike

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,242

    Re: Inserting String in cursor position Rich text Box

    When you say cursor, do you mean the mouse pointer or do you mean the caret, i.e. the blinking vertical bar in the control itself that indicates the insertion position?

  3. #3
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Inserting String in cursor position Rich text Box

    I meant the caret, but it's alright
    I used

    Code:
     
    RichTextBox1.SelectedText = var1
    And it worked.
    If there's another and more efficient way can you let me know?

    Thanks,
    Mike

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,968

    Re: Inserting String in cursor position Rich text Box

    By complete coincidence I came across this yesterday ...



    Get the index of the caret in the TextBox:

    C#

    int caretIndex = textBox.SelectionStart;
    VB.NET

    Dim caretIndex As Integer = textBox.SelectionStart
    Get the line number from the caret index:

    C#

    int lineNumber = textBox.GetLineFromCharIndex(caretIndex);
    VB.NET

    Dim lineNumber As Integer = textBox.GetLineFromCharIndex(caretIndex)
    Get the character index in the current line:

    C#

    Point characterXY = textBox.GetPositionFromCharIndex(caretIndex);
    int characterIndex = textBox.GetCharIndexFromPosition(characterXY);
    VB.NET

    Dim characterXY As Point = textBox.GetPositionFromCharIndex(caretIndex)
    Dim characterIndex As Integer = textBox.GetCharIndexFromPosition(characterXY)

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,242

    Re: Inserting String in cursor position Rich text Box

    Quote Originally Posted by MikeSpider View Post
    I meant the caret, but it's alright
    I used

    Code:
     
    RichTextBox1.SelectedText = var1
    And it worked.
    If there's another and more efficient way can you let me know?

    Thanks,
    Mike
    That is absolutely the way it should be done. Just note that, if the user has selected some text, that code will replace it. If that's not what you want then you should set the SelectionLength to zero first.

  6. #6
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Inserting String in cursor position Rich text Box

    Thanks for the tip.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •