简体   繁体   中英

selecting and formating text in richtextbox control vb6

i need to bold some text as i add them to the richtextbox control, currently here is my code

EDIT

With txtDetails
        If Not IsNullOrEmpty(title) Then
        Dim intStart As Integer
        intStart = Len(.Text)
            .Text = .Text & title '& vbCrLf
            .SelStart = intStart
            .SelLength = Len(title)
            .SelBold = True
            .SelLength = 0
            .SelBold = False
            .Text = .Text & vbNewLine
        End If
        If Not IsNullOrEmpty(value) Then
            .Text = .Text & value & vbNewLine
        End If
        .Text = .Text & vbNewLine
    End With

can anyone help me with the fix

I have made changes to the code, but still get all the subsequent test i add to be bold, insted of the one am interested in

Setting .Text has some side-effects which prevent your code from doing what you want:

  1. It resets .SelStart so you need to save the length of .Text first.
  2. It resets all of the formatting, so the boldness gets lost.

It looks like you want to append a bolded title if not "missing" and a non-bolded value if it is not "missing."

Option Explicit

Private Function IsNullOrEmpty(ByVal Item As Variant) As Boolean
    If IsNull(Item) Then
        IsNullOrEmpty = True
    ElseIf IsEmpty(Item) Then
        IsNullOrEmpty = True
    ElseIf VarType(Item) = vbString Then
        If Len(Item) = 0 Then
            IsNullOrEmpty = True
        End If
    End If
End Function

Private Sub cmdAppend_Click()
    With rtbDisplay
        .SelStart = &H7FFFFFFF
        If Not IsNullOrEmpty(txtTitle.Text) Then
            .SelBold = True
            .SelText = txtTitle.Text
            txtTitle.Text = ""
            .SelBold = False
            .SelText = vbNewLine
        End If
        If Not IsNullOrEmpty(txtValue.Text) Then
            .SelText = txtValue.Text
            txtValue.Text = ""
            .SelText = vbNewLine
        End If
    End With
    txtTitle.SetFocus
End Sub

Here I'm using TextBox controls as the data source but it should give you the general idea. It is often cheaper to use two operations than using String concatenation to add a newline.

Fetching the current .Text and measuring it with Len() is also costly if the contents are large, so just set .SelStart to the maximum value to move to the end.

I'm not into vb but I created a project and tested it out.
Try this

    With txtDetails
        .SelectionStart = 0
        .SelectionLength = txtDetails.TextLength
        .SelectionFont = New Font(txtDetails.Font, FontStyle.Bold)
    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM