First things first, I have a ObjectList (List of string)
ObecjtList(1) = Window
ObjectList(2) = Curtains
The following quote is inside a RichTextBox. I have the option in a ContextMenu to "Format" the RichtextBox.
Draped over the window, a pair of purple lined curtains. Stretching their full length, they slowly sway and caress the floor.
The Format Code
GetRTBFocus (FYI)
Conv This sub converts the string to "Sentence Case" And again (FYI)
And Finally. The problem This is supposed to highlight all the Objects in (ObjectList) and colour them Lime. But it doesn't :(
ObecjtList(1) = Window
ObjectList(2) = Curtains
The following quote is inside a RichTextBox. I have the option in a ContextMenu to "Format" the RichtextBox.
Quote:
Draped over the window, a pair of purple lined curtains. Stretching their full length, they slowly sway and caress the floor.
vb Code:
Private Sub ctmFormat_Click(sender As Object, e As EventArgs) Handles ctmFormat.Click Dim rb As RichTextBox = GetRTBFocus(sender) If rb Is Nothing Then Exit Sub Conv(rb) Highlight(rb) End Sub
GetRTBFocus (FYI)
vb Code:
Private Function GetRTBFocus(sender As Object) As RichTextBox Dim c As Control = DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl Dim rb = DirectCast(c, RichTextBox) Return (RB) End Function
Conv This sub converts the string to "Sentence Case" And again (FYI)
vb Code:
Public Sub Conv(ByVal rtb As RichTextBox) 'convert Text to Sentence Case rtb.Text = rtb.Text.ToLower Dim sentences() As String = rtb.Text.Split(New String() {". ", "? ", "! ", Chr(10) & Chr(10), "." & Chr(10)}, StringSplitOptions.RemoveEmptyEntries) Dim newSentences() As String = DirectCast(sentences.Clone, String()) Dim searchStart As Integer = 0 For x As Integer = 0 To sentences.GetUpperBound(0) sentences(x) = sentences(x).Trim newSentences(x) = newSentences(x).Trim newSentences(x) = newSentences(x).Substring(0, 1).ToUpper & newSentences(x).Substring(1).Replace(" i ", " I ").Replace(" i'", " I'") Dim start As Integer = rtb.Text.IndexOf(sentences(x), searchStart) For c As Integer = start To start + sentences(x).Length - 1 rtb.SelectionStart = c rtb.SelectionLength = 1 rtb.SelectedText = newSentences(x).Substring(c - start, 1) Next searchStart += sentences(x).Length + 2 Next End Sub
And Finally. The problem This is supposed to highlight all the Objects in (ObjectList) and colour them Lime. But it doesn't :(
vb Code:
Public Sub Highlight(ByVal r As RichTextBox) r.SelectAll() r.SelectionColor = Color.White r.DeselectAll() For Each strWord As String In ObjectList Dim startIndex As Integer = 0 Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord)) For Each wMatch As Match In wordMatchers.Matches(r.Text) With r .Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord) .SelectionColor = Color.Lime .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase) startIndex = .SelectionStart End With Next Next End Sub



