468,513 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Select and modify data within a RichTextBox by mouse click

I am having a problem with RichTextBox Control and hope you can help

I am working on a VB.Net project that has a RTB control, the user opens a form template. What I want to do is for a user to be able to click or right click on text within the form such as Priority: [[|Low|Medium|High]]. If the user clicked on Low the brackets and bars would go away and leave Priority: Low. And the same for Medium and High

Is there any way to do that

Nov 20 '05 #1
1 2801
I am pretty sure someone else can do this better, but here is a solution.
If nothing else, you can use what I did as a rough template and improve on
it.

----------------------------
Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
If e.Button = MouseButtons.Right Then
Dim CharIndex As Int32
Dim StartPos As Int32
Dim EndPos As Int32
Dim SelText As String
Dim Options() As String
Dim SelItem As String
Dim RunningLength As Int32
' get the position of the character they clicked at
CharIndex = RichTextBox1.GetCharIndexFromPosition(New Point(e.X,
e.Y))
' find the start and end of the option list
EndPos = RichTextBox1.Text.IndexOf("]", CharIndex) + 1
StartPos = RichTextBox1.Text.LastIndexOf("[", CharIndex)
' leave if they did not click inside of an option
If EndPos = -1 Or StartPos = -1 Or StartPos > EndPos Then
Exit Sub ' not inside of a [] section
End If
CharIndex -= StartPos
' select the total text area of the option list
RichTextBox1.Select(StartPos, EndPos - StartPos)

' get the total text area of the option list
SelText = RichTextBox1.SelectedText
Options = SelText.Split("|")

' find the item the user click on
For Each SelItem In Options
RunningLength += SelItem.Length + 1
If CharIndex < RunningLength Then
' found the item they selected, display that item
RichTextBox1.SelectedText = SelItem.Replace("[",
"").Replace("]", "")
Exit For
End If
Next

End If
End Sub
-------------------------------------

PS. still has some issues if there is more then one option list
"HJ Majoue" <an*******@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
I am having a problem with RichTextBox Control and hope you can help.

I am working on a VB.Net project that has a RTB control, the user opens a form template. What I want to do is for a user to be able to click or right
click on text within the form such as Priority: [[|Low|Medium|High]]. If
the user clicked on Low the brackets and bars would go away and leave
Priority: Low. And the same for Medium and High.
Is there any way to do that.

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rachel Suddeth | last post: by
reply views Thread by Lou | last post: by
4 posts views Thread by Aussie Rules | last post: by
reply views Thread by Linda Liu[MSFT] | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.