Search code examples
vb6formattingrtf

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


Solution

  • 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.