10-09-15, 09:18 PM
كيف استطيع تغيير لون جزء من النص فقط في RichTextBox مثلا أجعل حروف (الباء) كلها باللون الاحمر وباقي النص باللون الاسود لأجل تمييزها
Public Class Form1
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
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Words As New List(Of String)
Words.Add(TextBox1.Text)
For i As Integer = 0 To Words.Count - 1
colorWord(Words.Item(i), Color.Red)
Next
End Sub
End Class
Sub RichTextBoxHighlightText(ByVal richTextBox As RichTextBox, ByVal [string] As String, ByVal color As Color)
Dim c As Color = richTextBox.ForeColor
richTextBox.SelectAll()
richTextBox.SelectionColor = c
For i = 0 To richTextBox.TextLength
richTextBox.Find([string], i, RichTextBoxFinds.None)
richTextBox.SelectionColor = color
Next
richTextBox.ForeColor = c
End Sub