I can find a million examples of doing reg ex to apply syntax highlighting to a rich text box. but what i need it just a simple way to add in a word of a diffrent color.
What would the code be to just put the words "Hello World" into a textbox and have Hello be red and World be green?
This code doesnt work.
this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.text += "Test"
This code adds text "Hello" in red color and "World" in green to the RichTextBox.
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = "Hello "
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "World"
Select the text after you put it in and then change the color.
For example:
richTextBox1.Text += "Test"
richTextBox1.Select(richTextBox1.TextLength - 4, 4)
richTextBox1.SelectionColor = Color.Red
Ive worked with it in VB6 and i think its the same:
You must select the text and then apply
this.richTextBox1.SelectionColor = Color.Red
The added text always appears in the defaut color, you must select it and then change its color:
this.richTextBox1.text="Hello world!"
this.richTextBox1.selstart=0
this.richTextBox1.sellength=5
this.richTextBox1.SelectionColor = Color.Red
As i dont use vb.net, you must check the spelling but i think thats the key.
The code i wrote is supposed to print "Hello" in red and "World!" in black.
Try this
RichTextBox2.SelectionLength = 0
RichTextBox1.SelectionStart = 0
' We deselect everything first in case the user has something selected.
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = "Hello "
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "World "
This will add it to the start of the textbox. I think you could also make SelectionStart = RichTextBox1.TextLength which would put it at the end instead of the start.
The code doesn't work:
this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.text += "Test"
Change the second line to this:
this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.selectedtext = "Test"
Try this
Sub colorWord(ByVal word As String, ByVal color As Color) ' by im4dbr0
For i As Integer = 0 To RichTextBox1.TextLength
Try
If RichTextBox1.Text.ElementAt(i).ToString = word.ElementAt(0).ToString Then
Dim found As Boolean = False
For j As Integer = 1 To word.Count - 1
If RichTextBox1.Text.ElementAt(i + j) = word.ElementAt(j) Then
found = True
Else
found = False
Exit For
End If
Next
If found = True Then
RichTextBox1.Select(i, word.Length)
RichTextBox1.SelectionColor = color
End If
End If
Catch ex As Exception
Continue For
End Try
Next
For multiple words use loop
Dim Words As New List(Of String)
Words.Add("Its")
Words.Add("That")
Words.Add("Simple")
For i As Integer = 0 To Words.Count - 1
colorWord(Words.Item(i), Color.Red)
Next
Related
I want to select text in richtextbox from point a to b like in html format code red> to /red> soo that would be like this: This is a red>message/red> that means it should select from point a that is red> to point b that is /red> then the message word will be selected how can i do that? Thanks.
My latest code that i tried:
If RichTextBox1.Text.Contains("<red>") Then
RichTextBox1.SelectionStart = RichTextBox1.Find("<red>").ToString
RichTextBox1.SelectionLength = RichTextBox1.Find("</red>").ToString
MessageBox.Show("Format DETECTED!")
End If
RichTextBox1.SelectionColor = Color.Red
Try it.
If RichTextBox1.Text.Contains("<red>") Then
RichTextBox1.SelectionStart = CInt(RichTextBox1.Find("<red>").ToString)
RichTextBox1.SelectionLength = CInt(RichTextBox1.Find("</red>").ToString)
RichTextBox1.Select(RichTextBox1.Find("<red>") + "<red>".Length, RichTextBox1.Find("</red>") - (RichTextBox1.Find("<red>") + "<red>".Length))
RichTextBox1.SelectionColor = Color.Red
MessageBox.Show("Format DETECTED!")
End If
I have created a winform application in vb.net, it contains a parent container and other child forms. When one of the child form which contains a data gridview with some cell formatting, loading the other controls became gray colour overlayed.
Note: the gridview is created using the data from database and the color is applied according to the value in the cells. The color changing using the cellformating method. I have tried double buffering.. but it didn't help. In red color circle it is label and in green color, it is button.
Find the code below for cell formatting:
Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dataGridView1.CellFormatting
Try
Dim i As Integer
For i = 0 To dataGridView1.RowCount
If (i = (dataGridView1.RowCount)) Then
Exit For
Else
Dim dt As DateTime = DateTime.Parse(dataGridView1.Rows(i).Cells(6).Value.ToString())
dt = dt.AddHours(12)
Dim ts As TimeSpan = dt.Subtract(DateTime.Now)
Dim s As String = dataGridView1.Rows(i).Cells(7).Value.ToString()
If s = "Ready to Collect" Then
dataGridView1.Rows(i).Cells(7).Style.BackColor = Color.LimeGreen
dataGridView1.Rows(i).Cells(8).Value = "-----"
ElseIf s = "Completed" Then
dataGridView1.Rows(i).Cells(7).Style.BackColor = Color.GreenYellow
dataGridView1.Rows(i).Cells(8).Value = "-----"
ElseIf s = "Out of Stock" Then
dataGridView1.Rows(i).Cells(7).Style.BackColor = Color.Red
dataGridView1.Rows(i).Cells(8).Value = "-----"
ElseIf s = "Sent" Then
dataGridView1.Rows(i).Cells(7).Style.BackColor = Color.OrangeRed
If ts.CompareTo(TimeSpan.Zero) < 0 Then
dataGridView1.Rows(i).Cells(8).Value = "Process Immediately"
dataGridView1.Rows(i).Cells(8).Style.BackColor = Color.OrangeRed
Else
dataGridView1.Rows(i).Cells(8).Value = String.Format("{0:D2}:{1:D2}", ts.Hours, ts.Minutes)
End If
ElseIf s = "Cancelled" Then
dataGridView1.Rows(i).Cells(7).Style.BackColor = Color.White
dataGridView1.Rows(i).Cells(8).Value = "-----"
End If
End If
Next
Catch ex As Exception
System.Diagnostics.Trace.TraceError(ex.Message)
End Try
Check DoEvents. Maybe this helps you.
I used following code to color lines.
For line As Integer = 1 To RichTextBox1.Lines.Count() - 1
If RichTextBox1.Lines(line).StartsWith("You:") Then
RichTextBox1.Select(RichTextBox1.GetFirstCharIndexFromLine(line), RichTextBox1.Lines(line).Length)
RichTextBox1.SelectionColor = Color.Black
Else
RichTextBox1.Select(RichTextBox1.GetFirstCharIndexFromLine(line), RichTextBox1.Lines(line).Length)
RichTextBox1.SelectionColor = System.Drawing.ColorTranslator.FromHtml(color1)
End If
Next
It works fine when each line is actually one line long.
However if one message is longer it breaks whole coloring below the line:
On the picture the last line should be colored black, but second was colored instead.
How can I fix this?
When WordWrap is True, the Lines() array will return every line as you see it in the control, so a "wrapped" line is getting treated as a new line.
Try splitting on the line feed character and calculate the positions yourself:
Dim lines() As String = RichTextBox1.Text.Split(vbLf)
Dim startIndex As Integer = 0
For i As Integer = 0 To lines.Length - 1
RichTextBox1.Select(startIndex, lines(i).Length)
If lines(i).StartsWith("You:") Then
RichTextBox1.SelectionColor = Color.Red
Else
RichTextBox1.SelectionColor = Color.Green
End If
startIndex += lines(i).Length + vbLf.Length
Next
If you set RichTextBox1.WordWrap to False it works as expected.
Im trying to make it so, I can Specify a word or words in the code, and When I compile/debug I want the Program to search a richtextbox for all instances of those words and change there color.
Dim GRAB as String = New WebClient().DownloadString("example.com")
RichTextBox1.Text = GRAB
` Color Word Code Here
Ive looked up alot of things on google, but Everything I've tried will only highlight the FIRST word.
Sorry if my typing is bad, im typing with a broken arm..
Can someone help me with this, or write a quick snippet?
Try this :
Dim wordslist As New List(Of String)
wordslist.Add("Hello")
wordslist.Add("World")
Dim len As Integer = RichTextBox1.TextLength
For Each word As String In wordslist
Dim lastindex = RichTextBox1.Text.LastIndexOf(word)
Dim index As Integer = 0
While index < lastindex
RichTextBox1.Find(word, index, len, RichTextBoxFinds.None)
RichTextBox1.SelectionColor = Color.Blue
index = RichTextBox1.Text.IndexOf(word, index) + 1
End While
Next
Modified and translated from C# from Here
You need to select the text which will have a colour change.
RichTextBox1.Select(RichTextBox1.Text.IndexOf("example"),4)
RichTextBox1.SelectionColor = Color.Red
Would render the ".com" in red
or
RichTextBox1.Select(6,4)
RichTextBox1.SelectionColor = Color.Red
Would do the same
Suppose u want to make 'Dim' to Blue color as VS:-
Paste this:
If RichTextBox1.Text.EndsWith("Dim") Then
RichTextBox1.Select(RichTextBox1.TextLength - 3, 3)
RichTextBox1.SelectionColor = Color.Blue
RichTextBox1.Select(RichTextBox1.TextLength, RichTextBox1.TextLength)
RichTextBox1.SelectionColor = Color.Black
End If
Add this code to RichTextBox to Text_Changed.
This is a WinForms Application in VB. In my application I am trying to make it easier for the user to pick out items in a list quickly based on the items text color.. So I am trying to assign the color to each item using a select case statement as below... The problem is that I am getting an error saying "Public member 'Attributes' on type 'String' not found." VB flags the lines with the Attribute.Add param with this error.. Here is the code that I currently have...
For Each u In _units
_counter += 1
u_lookupNumber_box.Items.Add((Convert.ToString(u.UnitId)) + " - " + (Convert.ToString(u.perMonthCost)))
Select Case u.occupied
Case Is = 0
u_lookupNumber_box.Items(_counter - 1).Attributes.add("style", "color: Yellow")
Case Is = 1
u_lookupNumber_box.Items(_counter - 1).attributes.add("style", "color: Green")
Case Is = 2
u_lookupNumber_box.Items(_counter - 1).attributes.add("style", "color: Red")
Case Is = 3
u_lookupNumber_box.Items(_counter - 1).attributes.add("style", "color: Blue")
Case Is = 4
u_lookupNumber_box.Items(_counter - 1).attributes.add("style", "color: Orange")
Case Is = 5
u_lookupNumber_box.Items(_counter - 1).attributes.add("style", "color: Purple")
End Select
Next
Any ideas why I am getting this error?? I did notice that when I did the .attributes part when I hit "." and typed "att" it did not appear in the intelisense box in vb.. Which makes me think I need to assign the name "attributes" somehow to the dropdownbox first.. Thanks for any and all help...
It should be noted that u_lookupNumber_box is the name of the drop down box on my form..
attributes is not a method of the ListBox.Items property. Look at the ListBox.ObjectCollection Class documentation, it gives an example of an OwnerDrawn ListBox. I believe this or something similar will be your only option.
Ok so a long bread crumb trail of searches and a bit of trial and error and I got it to work fully... Because of constant processing of drawItem event arg I am not too happy with it but other than that it works as expected... First I set the DrawMode property of the dropdown to OwnerDrawFixed. Then created a structure in my form class as such:
Public Structure ColoredUnitItem
Dim text As String
Dim color As Color
Dim bold As Boolean
Public Overrides Function ToString() As String
Return text
End Function
End Structure
This will actually hold all the attributes for each item..
Next I changed the orginal posted code to the following:
For Each u In _units
_counter += 1
Dim myItem As New ColoredUnitItem
With myItem
.text = (Convert.ToString(u.UnitId)) + " - " + (Convert.ToString(u.perMonthCost))
Select Case u.occupied
Case Is = 0
.color = Color.Yellow
Case Is = 1
.color = Color.Green
Case Is = 2
.color = Color.Red
Case Is = 3
.color = Color.Blue
Case Is = 4
.color = Color.Orange
Case Is = 5
.color = Color.Purple
End Select
End With
u_lookupNumber_box.Items.Add(myItem)
Next
Next I simply had to draw the dropdownlist on the form as follows:
Private Sub uLookUpNumberBox_Draw(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles u_lookupNumber_box.DrawItem
e.DrawBackground()
If Not e.Index = -1 Then
Dim myItem As ColoredUnitItem = DirectCast(u_lookupNumber_box.Items(e.Index), ColoredUnitItem)
Dim FontToUse As Font = e.Font
If myItem.bold Then
FontToUse = New Font(e.Font, FontStyle.Bold)
End If
e.Graphics.DrawString(myItem.text, FontToUse, New SolidBrush(myItem.color), e.Bounds)
e.DrawFocusRectangle()
End If
End Sub
The flow is easy to follow and does work..