|
-
Mar 24th, 2003, 05:29 PM
#1
RichTextBox .SelBold is driving me nuts * RESOLVED
I'm using this code which I got here at VBForums:
VB Code:
Private Sub txtSession_Change()
Dim nPos As Long
nPos = -1
Do
nPos = txtSession.Find((Trim(txtSendTo.Text) & ":"), nPos + 1, Len(txtSession.Text))
If nPos <> -1 Then
txtSession.SelColor = vbBlack
txtSession.SelBold = True
End If
Loop While nPos <> -1
'txtSession.SelStart = 0
'txtSession.SelLength = 0
txtSession.SelBold = False
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.
-
Mar 24th, 2003, 05:36 PM
#2
BTW, this is how the RTB's content are fed.
VB Code:
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.
-
Mar 24th, 2003, 07:35 PM
#3
-
Mar 25th, 2003, 12:26 AM
#4
leinad31,
Try:
VB Code:
Private Sub BoldText(rtb As RichTextBox, TextToBold As Variant)
Dim i As Long
i = InStr(rtb.Text, TextToBold)
While i <> 0
rtb.SelStart = i - 1
rtb.SelLength = Len(TextToBold)
rtb.SelBold = True
i = InStr(i + 1, rtb.Text, TextToBold)
DoEvents
Wend
End Sub
Usage: BoldText rtfText,"Happy"
This will bold all text in the RTF box that is 'Happy'.
-
Mar 25th, 2003, 05:53 AM
#5
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:
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:
Public Sub TransferLog_ToRTB(HostIndex As Long, LineIndex As Long, ByRef RTB As RichTextBox, SendTo As String)
Dim LineNum As Long
Dim Sel_Start As Long
Dim Sel_Length As Long
For LineNum = LineIndex To UBound(HostInfo_Array(HostIndex).H_MS_Info.B_Log)
Select Case Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).A_Source)
Case "SYS"
Sel_Start = Len(RTB.Text)
Sel_Length = Len(Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message))
'RTB.Text = RTB.Text & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
RTB.SelText = Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
RTB.SelStart = Sel_Start
RTB.SelLength = Sel_Length
RTB.SelColor = vbRed
RTB.SelBold = True
Case "RCV"
Sel_Start = Len(RTB.Text)
Sel_Length = Len(Trim(SendTo) & ":")
'RTB.Text = RTB.Text & Trim(SendTo) & ":"
RTB.SelText = Trim(SendTo) & ":"
RTB.SelStart = Sel_Start
RTB.SelLength = Sel_Length
RTB.SelColor = vbBlack
RTB.SelBold = True
RTB.SelStart = Len(RTB.Text)
Sel_Start = Len(RTB.Text)
Sel_Length = Len(Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)) + 1
'RTB.Text = RTB.Text & " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
RTB.SelText = " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
RTB.SelStart = Sel_Start
RTB.SelLength = Sel_Length
RTB.SelColor = vbBlack
RTB.SelBold = False
Case "SNT"
Sel_Start = Len(RTB.Text)
Sel_Length = Len(Trim(HostInfo_Local.B_UCDHostname) & ":")
'RTB.Text = RTB.Text & Trim(HostInfo_Local.B_UCDHostname) & ":"
RTB.SelText = Trim(HostInfo_Local.B_UCDHostname) & ":"
RTB.SelStart = Sel_Start
RTB.SelLength = Sel_Length
RTB.SelColor = vbBlue
RTB.SelBold = True
RTB.SelStart = Len(RTB.Text)
Sel_Start = Len(RTB.Text)
Sel_Length = Len(Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)) + 1
'RTB.Text = RTB.Text & " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
RTB.SelText = " " & Trim(HostInfo_Array(HostIndex).H_MS_Info.B_Log(LineNum).B_Message)
RTB.SelStart = Sel_Start
RTB.SelLength = Sel_Length
RTB.SelColor = vbBlack
RTB.SelBold = False
End Select
RTB.SelStart = Len(RTB.Text)
RTB.SelText = vbCrLf
RTB.SelStart = Len(RTB.Text)
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|