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
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?
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
Re: Inserting String in cursor position Rich text Box
By complete coincidence I came across this yesterday ...
Quote:
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)
Re: Inserting String in cursor position Rich text Box
Quote:
Originally Posted by
MikeSpider
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.
Re: Inserting String in cursor position Rich text Box