|
-
Aug 18th, 2012, 08:47 AM
#1
Thread Starter
Lively Member
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
-
Aug 18th, 2012, 08:53 AM
#2
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?
-
Aug 18th, 2012, 09:26 AM
#3
Thread Starter
Lively Member
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
-
Aug 18th, 2012, 09:35 AM
#4
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)
-
Aug 18th, 2012, 09:39 AM
#5
Re: Inserting String in cursor position Rich text Box
 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.
-
Aug 18th, 2012, 12:38 PM
#6
Thread Starter
Lively Member
Re: Inserting String in cursor position Rich text Box
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|