I'm writing a text editor for my own use ( I'm "home learning") where I want to be able to leave comments for myself, like in code section of the VB express IDE. I have figured out how to get the Integer Location of the apostrophy, but can't get it to color the rest of the line. I don't know how to tell the program to find the integer value of the last character in the line. I need that to be able to subtract the apostrophy's location from the last character's location, to get the "SelectedLength". Then I can add that to the apstrophy's location to give the SelectedLength the SelectionColor.

This code was thrown together from excerpts of several codes I found on the web. I had to rewrite everything to get it to do what it does so far...

Apostrophy Location is char # 534 , the end of the line is 547. ( I cheated to get that count by placing another apostrophy there, and reclicking the btn. )
Anyway, I want to take 547 - 534 to get (13) the SelectionLength, then give the SelectionColor ( green) to that selected text.

Here is the code that I have been working around to get this:

Dim Location As Integer
        Dim char3() As String = {"'"}
        Dim z As Integer = CStr(vbCrLf.Count)
        For Each symbol As String In char3
            Location = rtf1.Find(symbol, RichTextBoxFinds.MatchCase)
            Do Until Location < 0
                Label7.Text = Location
                rtf1.SelectionStart = Location
                'rtf1.SelectionLength = (z - Location) <-- Does'nt work
                rtf1.SelectionColor = Color.Green
                Location = rtf1.Find(symbol, Location + 1, RichTextBoxFinds.MatchCase)
            Loop
        Next

I hope this is clear enough for everyone to understand. I have difficulty explaining myself sometimes...

Recommended Answers

All 6 Replies

it is might not the cleanest solution but works so far

Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged

		If RichTextBox1.Lines.Last.StartsWith("'") Then
			RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexOfCurrentLine
			RichTextBox1.SelectionLength = RichTextBox1.Lines.Last.Length
			RichTextBox1.SelectionColor = Color.Green
			RichTextBox1.Select(RichTextBox1.Lines.Last.Length, 1)
		Else
			RichTextBox1.SelectionColor =color.Black
		End If
	End Sub

This above thread got me very close to my goal. It turns the ENTIRE LINE green, in those situations it's perfect. But it still won't start at the "'" and go from there. So I rewrote it a bit to do that, but now...when I hit "Enter" to start next line, it reverts back to beheind the apostrophy!
Here is the code I'm using presently:

 Dim Location As Integer = rtf1.Find("'", RichTextBoxFinds.MatchCase)
        If rtf1.Lines.Last.Contains("'") Then
            rtf1.SelectionStart = Location
            'rtf1.SelectionStart = rtf1.GetFirstCharIndexOfCurrentLine
            rtf1.SelectionLength = rtf1.Lines.Last.Length
            rtf1.SelectionColor = Color.Green
            rtf1.Select(rtf1.Lines.Last.Length, 1)
        Else
            rtf1.SelectionColor = Color.Black
        End If
        Next

What am I doing wrong with my posts? They don't look like other peoples posts.

sometimes the solution is sooo easy o.O try this code below.

Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles rtf1.TextChanged
		If rtf1.Lines.Last.Contains("'") Then
			rtf1.SelectionLength = 1
			rtf1.SelectionColor = Color.Green
		Else
			rtf1.SelectionColor = Color.Black
		End If
	End Sub

Since the current selection is always at the end of the text you only need to set the selection length to 1 to select the current char.

sometimes the solution is sooo easy o.O try this code below.

Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles rtf1.TextChanged
		If rtf1.Lines.Last.Contains("'") Then
			rtf1.SelectionLength = 1
			rtf1.SelectionColor = Color.Green
		Else
			rtf1.SelectionColor = Color.Black
		End If
	End Sub

Since the current selection is always at the end of the text you only need to set the selection length to 1 to select the current char.

Much as I said earlier, I'm home learning. So, needless to say, I had never seen the "Lines.Last" until your first post, and even then, didn't see the simple reversal right in front of me! Google was not my friend this time around, I could not find anything on this subject at all. Oddly enough, I did figure the "" out. Took 3 list boxes to pull that one off, but it works! A Do loop collects integer position of every ", and loads the the location integer into a textbox. Then another Do loop separates them into opening and closing ", and puts them in separate listboxes. Then a 3rd Do loop with a " += 1" pulls one out of each box, by the pair, subtracting the opening " location from the closing" location, creating the "SelectionLength". ( Obviously the Opening" is the SelectionStart)
I know it's kinda "hillbilly", but it works!

This is, however, exactly what I was after! Thank you so much. You have added one new trick to my "Slowly Learning" bag of tricks !

Much as I said earlier, I'm home learning. So, needless to say, I had never seen the "Lines.Last" until your first post, and even then, didn't see the simple reversal right in front of me! Google was not my friend this time around, I could not find anything on this subject at all. Oddly enough, I did figure the "" out. Took 3 list boxes to pull that one off, but it works! A Do loop collects integer position of every ", and loads the the location integer into a textbox. Then another Do loop separates them into opening and closing ", and puts them in separate listboxes. Then a 3rd Do loop with a " += 1" pulls one out of each box, by the pair, subtracting the opening " location from the closing" location, creating the "SelectionLength". ( Obviously the Opening" is the SelectionStart)
I know it's kinda "hillbilly", but it works!

This is, however, exactly what I was after! Thank you so much. You have added one new trick to my "Slowly Learning" bag of tricks !

glad i could help.
please mark this thread as solved in case someone else is looking for the same problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.