Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27517

VS 2013 [RESOLVED] Sub refuses to cooperate.

$
0
0
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.
Quote:

Draped over the window, a pair of purple lined curtains. Stretching their full length, they slowly sway and caress the floor.
The Format Code
vb Code:
  1. Private Sub ctmFormat_Click(sender As Object, e As EventArgs) Handles ctmFormat.Click
  2.         Dim rb As RichTextBox = GetRTBFocus(sender)
  3.         If rb Is Nothing Then Exit Sub
  4.         Conv(rb)
  5.         Highlight(rb)
  6. End Sub

GetRTBFocus (FYI)
vb Code:
  1. Private Function GetRTBFocus(sender As Object) As RichTextBox
  2.         Dim c As Control = DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl
  3.         Dim rb = DirectCast(c, RichTextBox)
  4.         Return (RB)
  5. End Function

Conv This sub converts the string to "Sentence Case" And again (FYI)
vb Code:
  1. Public Sub Conv(ByVal rtb As RichTextBox)
  2.         'convert Text to Sentence Case
  3.         rtb.Text = rtb.Text.ToLower
  4.  
  5.         Dim sentences() As String = rtb.Text.Split(New String() {". ", "? ", "! ", Chr(10) & Chr(10), "." & Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
  6.         Dim newSentences() As String = DirectCast(sentences.Clone, String())
  7.  
  8.         Dim searchStart As Integer = 0
  9.         For x As Integer = 0 To sentences.GetUpperBound(0)
  10.  
  11.             sentences(x) = sentences(x).Trim
  12.             newSentences(x) = newSentences(x).Trim
  13.             newSentences(x) = newSentences(x).Substring(0, 1).ToUpper & newSentences(x).Substring(1).Replace(" i ", " I ").Replace(" i'", " I'")
  14.  
  15.             Dim start As Integer = rtb.Text.IndexOf(sentences(x), searchStart)
  16.             For c As Integer = start To start + sentences(x).Length - 1
  17.                 rtb.SelectionStart = c
  18.                 rtb.SelectionLength = 1
  19.                 rtb.SelectedText = newSentences(x).Substring(c - start, 1)
  20.             Next
  21.             searchStart += sentences(x).Length + 2
  22.         Next
  23. 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:
  1. Public Sub Highlight(ByVal r As RichTextBox)
  2.     r.SelectAll()
  3.     r.SelectionColor = Color.White
  4.     r.DeselectAll()
  5.  
  6.     For Each strWord As String In ObjectList
  7.         Dim startIndex As Integer = 0
  8.         Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
  9.         For Each wMatch As Match In wordMatchers.Matches(r.Text)
  10.             With r
  11.                 .Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
  12.                 .SelectionColor = Color.Lime
  13.                 .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
  14.                 startIndex = .SelectionStart
  15.             End With
  16.         Next
  17.     Next
  18. End Sub

Viewing all articles
Browse latest Browse all 27517

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>