Results 1 to 5 of 5

Thread: RichTextBox .SelBold is driving me nuts * RESOLVED

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    RichTextBox .SelBold is driving me nuts * RESOLVED

    I'm using this code which I got here at VBForums:

    VB Code:
    1. Private Sub txtSession_Change()
    2.  
    3.     Dim nPos As Long
    4.    
    5.     nPos = -1
    6.     Do
    7.     nPos = txtSession.Find((Trim(txtSendTo.Text) & ":"), nPos + 1, Len(txtSession.Text))
    8.         If nPos <> -1 Then
    9.             txtSession.SelColor = vbBlack
    10.             txtSession.SelBold = True
    11.         End If
    12.     Loop While nPos <> -1
    13.    
    14.     'txtSession.SelStart = 0
    15.     'txtSession.SelLength = 0
    16.     txtSession.SelBold = False
    17. End Sub

    First time it runs, its ok. But on succeeding calls it sets all text to bold instead of just the result from .Find. Actually, even before the procedure starts statements on the 2nd and succeeding calls(I put a break on Private Sub txtSession_Change()), all the text is already changed to bold.

    I inserted txtSession.SelBold = False in hopes of preventing this but it had no effect. I even tried the statements above that are commented out. This is the only procedure in the project wherein .SelBold is used so the prob can't be anywhere else. I checked .SelLength, .SelStart, and .SelText right after executing .Find and their values are ok, but the text still turns to bold. I checked the value of .SelBold in several other procedures (before and after the call to this procedure) and its value is false as expected. I CAN'T FIGURE OUT WHAT'S WRONG.

    Can someone please help me?
    Last edited by leinad31; Mar 25th, 2003 at 07:20 AM.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    BTW, this is how the RTB's content are fed.

    VB Code:
    1. frmMessage.txtSession.Text = FindIP_Result.Info.H_MS_Info.B_MS_Log

    Which is essentially String = String.

    Also, I noticed that If I didn't load/show the form immediately where RTB txtSession exists and kept feeding successive lines, the output is ok when I load/show the form for the first time. So it's really on the successive calls to txtSessions_Change(), I think. But I'm out on ideas on how to solve this.

    Was my mistake putting it in txtSessions_Change()? If that's the case, where can I transfer the code to achieve the same effect?
    Last edited by leinad31; Mar 24th, 2003 at 05:40 PM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    *bump

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    leinad31,

    Try:

    VB Code:
    1. Private Sub BoldText(rtb As RichTextBox, TextToBold As Variant)
    2. Dim i As Long
    3.  
    4. i = InStr(rtb.Text, TextToBold)
    5.  
    6. While i <> 0
    7.    rtb.SelStart = i - 1
    8.    rtb.SelLength = Len(TextToBold)
    9.    rtb.SelBold = True
    10.    i = InStr(i + 1, rtb.Text, TextToBold)
    11.    DoEvents
    12. Wend
    13.  
    14. End Sub

    Usage: BoldText rtfText,"Happy"

    This will bold all text in the RTF box that is 'Happy'.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Thanks, but I realized that if I simply searched for the text then if a user inputs here user name, ity will also be highlighted even if its part of the message. I changed my struct and was able to finally get RTB to work by simply not referencing RTB.Text.

    VB Code:
    1. RTB.Text = RTB.Text & Whatever

    It seems, doing so re-highlights everything so all the text becomes the same color boldtype (last stored in SelColor and SelBold).

    I used SelText instead to add text, repositioning SelStart to the end of the text afterwards. Here's my code:

    VB Code:
    1. Public Sub TransferLog_ToRTB(HostIndex As Long, LineIndex As Long, ByRef RTB As RichTextBox, SendTo As String)
    2.  
    3.     Dim LineNum As Long
    4.     Dim Sel_Start As Long
    5.     Dim Sel_Length As Long
    6.    
    7.     For LineNum = LineIndex To UBound(HostInfo_Array(HostIndex).H_MS_Info.B_Log)
    8.         Select Case Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).A_Source)
    9.         Case "SYS"
    10.             Sel_Start = Len(RTB.Text)
    11.             Sel_Length = Len(Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message))
    12.            
    13.             'RTB.Text = RTB.Text & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
    14.             RTB.SelText = Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
    15.             RTB.SelStart = Sel_Start
    16.             RTB.SelLength = Sel_Length
    17.             RTB.SelColor = vbRed
    18.             RTB.SelBold = True
    19.         Case "RCV"
    20.             Sel_Start = Len(RTB.Text)
    21.             Sel_Length = Len(Trim(SendTo) & ":")
    22.            
    23.             'RTB.Text = RTB.Text & Trim(SendTo) & ":"
    24.             RTB.SelText = Trim(SendTo) & ":"
    25.             RTB.SelStart = Sel_Start
    26.             RTB.SelLength = Sel_Length
    27.             RTB.SelColor = vbBlack
    28.             RTB.SelBold = True
    29.            
    30.             RTB.SelStart = Len(RTB.Text)
    31.            
    32.             Sel_Start = Len(RTB.Text)
    33.             Sel_Length = Len(Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)) + 1
    34.            
    35.             'RTB.Text = RTB.Text & " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
    36.             RTB.SelText = " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
    37.             RTB.SelStart = Sel_Start
    38.             RTB.SelLength = Sel_Length
    39.             RTB.SelColor = vbBlack
    40.             RTB.SelBold = False
    41.         Case "SNT"
    42.             Sel_Start = Len(RTB.Text)
    43.             Sel_Length = Len(Trim(HostInfo_Local.B_UCDHostname) & ":")
    44.            
    45.             'RTB.Text = RTB.Text & Trim(HostInfo_Local.B_UCDHostname) & ":"
    46.             RTB.SelText = Trim(HostInfo_Local.B_UCDHostname) & ":"
    47.             RTB.SelStart = Sel_Start
    48.             RTB.SelLength = Sel_Length
    49.             RTB.SelColor = vbBlue
    50.             RTB.SelBold = True
    51.            
    52.             RTB.SelStart = Len(RTB.Text)
    53.            
    54.             Sel_Start = Len(RTB.Text)
    55.             Sel_Length = Len(Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)) + 1
    56.            
    57.             'RTB.Text = RTB.Text & " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
    58.             RTB.SelText = " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
    59.             RTB.SelStart = Sel_Start
    60.             RTB.SelLength = Sel_Length
    61.             RTB.SelColor = vbBlack
    62.             RTB.SelBold = False
    63.         End Select
    64.         RTB.SelStart = Len(RTB.Text)
    65.         RTB.SelText = vbCrLf
    66.         RTB.SelStart = Len(RTB.Text)
    67.     Next
    68. End Sub

    Hopefully it will keep working even after adding other code.

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