Results 1 to 27 of 27

Thread: Help to convert to .net please!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432

    Help to convert to .net please!

    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

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    8Bump*

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Private Sub ChangeColor(ByVal word As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    2.         'find word
    3.         Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    4.         If ret > -1 Then
    5.             rtb.SelectionColor = clr
    6.             If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
    7.         End If
    8.     End Sub
    9.  
    10. 'syntax
    11. ChangeColor(txtWord.Text, Color.Blue, rtbText)

    You could of course tie it to one RichTextBox or one color and cut out some of the parameters.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Will this look for multiple word at once ?

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes its recursive so if it searchs for the word until it doesn't find it anymore.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    :-/ ok let me try that again

    I need it to search for multiple word at once like the code at the top

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Private Sub rtbRecever_Change() Handles rtbReviever.TextChanged()
    2.  
    3. 'probably not needed
    4. 'With rtbRecever
    5. '    .SelStart = 0
    6. '    .SelLength = Len(.Text)
    7. '    .SelColor = vbBlack
    8. '    .SelStart = Len(.Text)
    9. 'End With
    10.  
    11.                                            
    12. ' You can add words to the highlight list. I have game you
    13. ' a few to start off with.
    14. ChangeColor(frmIMSettings.txtScreenName.Text, Color.Blue, rtbReviever)
    15. ChangeColor("brb", Color.Blue, rtbReviever)
    16. ChangeColor("lol", Color.Blue, rtbReviever)
    17. ChangeColor("G2G", Color.Blue, rtbText)
    18. ChangeColor("visual basic", Color.Blue, rtbReviever)
    19. ChangeColor("java", Color.Blue, rtbReviever)
    20.                            
    21. End Sub
    Last edited by Edneeis; Oct 7th, 2002 at 12:57 AM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Thansk Work thanks

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Coool Thanks is their a way to change it to an img too or is that with someother control ?

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here try this if that is what you want:
    VB Code:
    1. Private Sub ChangeToPic(ByVal word As String, ByVal img As Image, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    2.         'find word
    3.         Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    4.         If ret > -1 Then
    5.             Clipboard.SetDataObject(img, True)
    6.             rtb.Paste()
    7.             If ret < rtb.Text.Length Then ChangeToPic(word, img, rtb, ret + 1)
    8.         End If
    9.     End Sub
    10.  
    11.  
    12.     Private Sub btnHighlight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHighlight.Click
    13.         'ChangeColor(txtWord.Text, Color.Blue, rtbText)
    14.         ChangeToPic(txtWord.Text, PictureBox1.Image, rtbText)
    15.     End Sub

    But it will net keep the transparency of the picture it pastes so give it the same background color as the richtextbox.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    THANKS

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    ok this is cool and all but theirs a problem

    its clearing the image when i input a new one

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't know what you mean. Clearing what image?

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    OK when i input :-)-
    it displays the image fine but when i input another one it erases the one alrdy in the RTB ?

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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?

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    ok its not supporting multiple imgs it show one and when i try to add another the 1st one goses away that better

    you can im me at
    AIM = Dviper1020
    ICQ = 56186569

  18. #18
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  19. #19
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    OK here is the code

    Code:
        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

  21. #21
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    sorry cut it off mybad

    Code:
            ChangeToPic(":-)", lstMain.Images.Item(12), rtbDisplay_Message)
            ChangeToPic("¿©", lstMain2.Images.Item(63), rtbDisplay_Message)
    this is in with the ColorChange

  23. #23
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

    VB Code:
    1. Private Sub FormatText()
    2.         'ChangeColor(frmIMSettings.txtScreenName.Text, Color.Blue, rtbDisplay_Message)
    3.         ChangeColor("brb", Color.Red, rtbDisplay_Message)
    4.         ChangeColor("lol", Color.Yellow, rtbDisplay_Message)
    5.         ChangeColor("G2G", Color.Orange, rtbDisplay_Message)
    6.         ChangeColor("visual basic", Color.Khaki, rtbDisplay_Message)
    7.         ChangeToPic(":-)", lstMain.Images.Item(12), rtbDisplay_Message)
    8.         ChangeToPic("¿©", lstMain2.Images.Item(63), rtbDisplay_Message)
    9.     End Sub
    10.  
    11.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    12.         'Displays the Message
    13.         rtbDisplay_Message.Text  &= String.Concat(ControlChars.NewLine,txtMessage_Send.Text)
    14.         txtMessage_Send.Text = String.Empty
    15.         [b]FormatText()[/b]
    16.     End Sub

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    hmm no its still Deleteing the Past Imgs let me show you
    Attached Images Attached Images  

  25. #25
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Private Sub ChangeToPic(ByVal word As String, ByVal img As Image, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    2.         'find word
    3.         Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    4.         If ret > -1 Then
    5.             'handle readonly
    6.             Dim orig As Boolean = rtb.ReadOnly
    7.             If orig = True Then rtb.ReadOnly = False
    8.             'save old clipboard item
    9.             Dim old As Object = Clipboard.GetDataObject
    10.             'paste picture
    11.             Clipboard.SetDataObject(img.Clone, True)
    12.             rtb.Paste()
    13.             'reset readonly
    14.             rtb.ReadOnly = orig
    15.             'reset clipboard object
    16.             Clipboard.SetDataObject(old)
    17.             'find more
    18.             If ret < rtb.Text.Length Then ChangeToPic(word, img, rtb, ret + 1)
    19.         End If
    20.         rtb.SelectionStart = rtb.Text.Length
    21.     End Sub
    22.  
    23.     Private Sub ChangeColor(ByVal word As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    24.         'find word
    25.         Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    26.         If ret > -1 Then
    27.             'color it
    28.             rtb.SelectionColor = clr
    29.             'search for more
    30.             If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
    31.         End If
    32.         'set caret to end
    33.         rtb.SelectionStart = rtb.Text.Length
    34.     End Sub
    35.  
    36.     Private Sub ChangeColor(ByVal words() As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    37.         'handle array
    38.         Dim word As String
    39.         For Each word In words
    40.             'find word
    41.             Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    42.             If ret > -1 Then
    43.                 'color it
    44.                 rtb.SelectionColor = clr
    45.                 'search for more
    46.                 If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
    47.             End If
    48.             'set caret to end
    49.         Next
    50.         rtb.SelectionStart = rtb.Text.Length
    51.     End Sub
    52.  
    53.     Private Sub FormatText(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbMain.TextChanged
    54.         ChangeColor("brb", Color.Red, rtbMain)
    55.         ChangeColor("lol", Color.Yellow, rtbMain)
    56.         ChangeColor("G2G", Color.Orange, rtbMain)
    57.         ChangeColor("visual basic", Color.Khaki, rtbMain)
    58.         ChangeToPic(":-)", imlMain.Images(0), rtbMain)
    59.         ChangeToPic("¿©", imlMain.Images(1), rtbMain)
    60.     End Sub
    61.  
    62.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    63.         'Displays the Message
    64.         rtbMain.AppendText(String.Concat(ControlChars.NewLine, txtSend.Text))
    65.         'rtbMain.Text &= String.Concat(ControlChars.NewLine, txtSend.Text)
    66.         txtSend.Text = String.Empty
    67.         'FormatText()
    68.     End Sub

  26. #26
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    To use the overloaded ChangeColor that takes an array it'd be like this:
    VB Code:
    1. Dim words() As String = {"brb", "lol", "G2G", "visual basic", "java"}
    2.         ChangeColor(words, Color.Blue, rtbMain)

  27. #27
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Had to revise the ChangeColor methods to get rid of a bug that wouldn't go back to the default color if the word was the at the end of the rtb text.

    VB Code:
    1. Private Sub ChangeColor(ByVal word As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    2.         'find word
    3.         Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    4.         If ret > -1 Then
    5.             'color it
    6.             rtb.SelectionColor = clr
    7.             'search for more
    8.             If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
    9.         End If
    10.         'set caret to end
    11.         rtb.SelectionStart = rtb.Text.Length
    12.         rtb.SelectionLength = 0
    13.         rtb.SelectionColor = rtb.ForeColor
    14.     End Sub
    15.  
    16.     Private Sub ChangeColor(ByVal words() As String, ByVal clr As Color, ByVal rtb As RichTextBox, Optional ByVal start As Integer = 0)
    17.         'handle array
    18.         Dim word As String
    19.         For Each word In words
    20.             'find word
    21.             Dim ret As Integer = rtb.Find(word, start, RichTextBoxFinds.WholeWord)
    22.             If ret > -1 Then
    23.                 'color it
    24.                 rtb.SelectionColor = clr
    25.                 'search for more
    26.                 If ret < rtb.Text.Length Then ChangeColor(word, clr, rtb, ret + 1)
    27.             End If
    28.             'set caret to end
    29.         Next
    30.         rtb.SelectionStart = rtb.Text.Length
    31.         rtb.SelectionLength = 0
    32.         rtb.SelectionColor = rtb.ForeColor
    33.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width