OK i have this code that changes the color of word in a rtb can someone help convert it to .net?
Code:
Public Sub HighlightText(sKeyword As String)
Dim nStart As Integer, sPrevChar As String, sNextChar As String
nStart = InStr(1, LCase(frmIM.rtbRecever.Text), sKeyword)
Do While nStart <> 0
If nStart > 1 Then
sPrevChar = Mid$(frmIM.rtbRecever.Text, nStart - 1, 1)
Else
sPrevChar = " "
End If
If Len(frmIM.rtbRecever.Text) >= nStart + Len(sKeyword) Then
sNextChar = Mid$(frmIM.rtbRecever.Text, nStart + Len(sKeyword), 1)
Else
sNextChar = " "
End If
If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
(sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
With frmIM.rtbRecever
.SelStart = nStart - 1
.SelLength = Len(sKeyword)
.SelColor = vbBlue
.SelText = UCase(sKeyword)
.SelStart = Len(frmIM.rtbRecever.Text)
.SelColor = vbBlack
End With
End If
nStart = InStr(nStart + Len(sKeyword), LCase(frmIM.rtbRecever.Text), sKeyword)
Loop
End Sub
Code:
Private Sub rtbRecever_Change()
With rtbRecever
.SelStart = 0
.SelLength = Len(.Text)
.SelColor = vbBlack
.SelStart = Len(.Text)
End With
' You can add words to the highlight list. I have game you
' a few to start off with.
HighlightText frmIMSettings.txtScreenName.Text
HighlightText "brb"
HighlightText "lol"
HighlightText "G2G"
HighlightText "visual basic"
HighlightText "java"
End Sub
Oh I see you want mutiple word not multiple instances of the same word. Well for that you would just have to run it in a loop passing in the different words. Since the bulk of it is done by the Find methof of the Richtextbox itself which only takes one word at a time, but it is very fast so using it several times shouldn't be a problem.
Just keep this part although the first bit shouldn't be needed:
VB Code:
Private Sub rtbRecever_Change() Handles rtbReviever.TextChanged()
'probably not needed
'With rtbRecever
' .SelStart = 0
' .SelLength = Len(.Text)
' .SelColor = vbBlack
' .SelStart = Len(.Text)
'End With
' You can add words to the highlight list. I have game you
What do you mean? Replace :> with the picture of a smile or something?
If so just write another function that finds the word and then after it selects it instead of changing the color copy/paste the picture. The richtextbox has a paste method and the clipboard object will help you copy.
Why don't you show me in code what you are doing? Everything works fine for me. What do you mean input another one, type :-)- again? Or run the ChangeToPic code again? Or change the image used in the ChangeToPic? What erases the one already there? The ChangeToPic method? Typing someting?
It must be something in the way you are implementing it, because I can do it with more than one image no problem. I'll do up a small sample app for you in a minute.
I don't mean to sound rude but with out seeing your code I can't tell what the problem is, but here is a sample project so you should be able to figure it out from there. If not post again and I'll see what I can do.
Private Sub rtbDisplay_Message_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbDisplay_Message.TextChanged
'ChangeColor(frmIMSettings.txtScreenName.Text, Color.Blue, rtbDisplay_Message)
ChangeColor("brb", Color.Red, rtbDisplay_Message)
ChangeColor("lol", Color.Yellow, rtbDisplay_Message)
ChangeColor("G2G", Color.Orange, rtbDisplay_Message)
ChangeColor("visual basic", Color.Khaki, rtbDisplay_Message)
End Sub
Code:
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'Displays the Message
rtbDisplay_Message.Text = rtbDisplay_Message.Text + vbNewLine + txtMessage_Send.Text
txtMessage_Send.Text = String.Empty
End Sub
The text only changes when the send message is added? Also this is the color code not the image code. Did the sample help? See it works with multiple images I'm not sure what the trouble is.
I think its because you are using the Changed event and Pasting the picture in would fire the Changed event again and so it messes up the Pasting. Just move that code to a normal sub and call it in the btnSend_Click event.
Well I figured it out. You have to use the AppendText method of the rtb instead of changing the text. I think its because strings aren't ever changed just recreated internally so it must wipe out non text when it recreates the text string. But the AppendMethod works fine. I also added some support so the users clipboard item doesn't get lost and to handle readonly. I also made an overloaded version of the ChangeColor to take an array of words that will be changed to the same color. Also you can use the TextChanged event if you want since that wasn't the problem.
VB Code:
Private Sub ChangeToPic(ByVal word As String, ByVal img As Image, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
'find word
Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
If ret > -1 Then
'handle readonly
Dim orig As Boolean = rtb.ReadOnly
If orig = True Then rtb.ReadOnly = False
'save old clipboard item
Dim old As Object = Clipboard.GetDataObject
'paste picture
Clipboard.SetDataObject(img.Clone, True)
rtb.Paste()
'reset readonly
rtb.ReadOnly = orig
'reset clipboard object
Clipboard.SetDataObject(old)
'find more
If ret < rtb.Text.Length Then ChangeToPic(word, img, rtb, ret + 1)
End If
rtb.SelectionStart = rtb.Text.Length
End Sub
Private Sub ChangeColor(ByVal word As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
'find word
Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
If ret > -1 Then
'color it
rtb.SelectionColor = clr
'search for more
If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
End If
'set caret to end
rtb.SelectionStart = rtb.Text.Length
End Sub
Private Sub ChangeColor(ByVal words() As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
'handle array
Dim word As String
For Each word In words
'find word
Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
If ret > -1 Then
'color it
rtb.SelectionColor = clr
'search for more
If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
End If
'set caret to end
Next
rtb.SelectionStart = rtb.Text.Length
End Sub
Private Sub FormatText(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbMain.TextChanged