1

当我将它们添加到richtextbox控件时,我需要加粗一些文本,目前这是我的代码

编辑

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

谁能帮我解决这个问题

我已经对代码进行了更改,但我添加的所有后续测试仍然是粗体的,插入了我感兴趣的测试

4

3 回答 3

2

设置.Text有一些副作用,会阻止你的代码做你想做的事:

  1. 它会重置.SelStart,因此您需要保存.Textfirst 的长度。
  2. 它会重置所有格式,因此会丢失粗体。
于 2012-10-03T21:20:42.003 回答
2

如果不是“丢失”,您似乎想要附加一个粗体title,如果它不是“丢失”,则附加一个非粗体value

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

在这里,我使用 TextBox 控件作为数据源,但它应该为您提供总体思路。使用两个操作通常比使用字符串连接添加换行符更便宜。

如果内容很大,获取当前 .Text 并使用 Len() 测量它的成本也很高,因此只需将 .SelStart 设置为最大值即可移动到末尾。

于 2012-10-03T21:40:20.117 回答
-1

我不喜欢vb,但我创建了一个项目并对其进行了测试。
试试这个

    With txtDetails
        .SelectionStart = 0
        .SelectionLength = txtDetails.TextLength
        .SelectionFont = New Font(txtDetails.Font, FontStyle.Bold)
    End With
于 2012-10-03T21:27:24.287 回答