【问题标题】:VB6 Textbox fontweight manipulationVB6 文本框字体粗细操作
【发布时间】:2012-08-28 23:28:33
【问题描述】:

我有一个 VB6 应用程序,我想在其中操作在文本框中输出的字符串的某些部分。

txtPhoneNums.Text = "Home:  " + strHomeNo + vbCrLf _
                    + "Mobile: " + strMobileNo + vbCrLf + "Work:  " + strWorkNo + vbCrLf

它嵌套在执行各种验证的 if 语句中。例如,我希望能够在上面的 sn-p 中以红色和粗体字突出显示单词“Work”和附加的字符串值“strWorkNo”。我可以在不创建多个文本框的情况下轻松做到这一点(并将其他两个值保留为默认外观吗?)

谢谢。

为清晰起见添加了图像。我希望这两个空字段字符串是红色和粗体。

【问题讨论】:

  • 标准 vb6 文本框不允许您混合格式,您需要寻找 RichTextBox 控件。作为旁注,我也更喜欢使用 & 而不是 +
  • 嗨,谢谢。 :) 如果 txtPhoneNums.Text 是 RichTextBox,语法会如何显示?
  • 马克的回答就是一个很好的例子。

标签: string vb6 textbox


【解决方案1】:

您想使用 RichTextBox。我建议您不要尝试使用富文本格式 (RTF) 本身,而是使用标准方法。

您的代码将更改如下:

Option Explicit

Private Sub Command1_Click()
    WritePhoneNums "01020239", "07749383", "0234394349"
End Sub

Private Sub WritePhoneNums(ByRef strHomeNo As String, ByRef strMobileNo As String, ByRef strWorkNo As String)

    Dim nPosBeginningOfWorkNo As Long
    Dim nPosCurrent As Long

    txtPhoneNums.TextRTF = vbNullString ' Clear existing code.

    ' Clear style to default.
    ApplyNormalStyle txtPhoneNums

    ' Enter standard text. The selection will be at the end of the text when finished.
    txtPhoneNums.SelText = "Home:  " + strHomeNo + vbCrLf _
                         & "Mobile: " + strMobileNo + vbCrLf + "Work:  "

    ' Save the cursor position, write the work number, and then save the cursor position again.
    nPosBeginningOfWorkNo = txtPhoneNums.SelStart
    txtPhoneNums.SelText = strWorkNo
    nPosCurrent = txtPhoneNums.SelStart

    ' From this information, select the preceding text, and make it "selected".
    txtPhoneNums.SelStart = nPosBeginningOfWorkNo
    txtPhoneNums.SelLength = nPosCurrent - nPosBeginningOfWorkNo
    ApplyHighlightedStyle txtPhoneNums

    ' Reset the selection to the end, and reset the text style.
    txtPhoneNums.SelStart = nPosCurrent
    txtPhoneNums.SelLength = 0
    ApplyNormalStyle txtPhoneNums

    txtPhoneNums.SelText = vbCrLf

End Sub

Private Sub ApplyNormalStyle(ByRef txt As RichTextBox)
    txt.SelBold = False
    txt.SelColor = vbBlack
End Sub

Private Sub ApplyHighlightedStyle(ByRef txt As RichTextBox)
    txt.SelBold = True
    txt.SelColor = vbRed
End Sub

【讨论】:

  • 很好的答案,谢谢。由于我有大量可能的场景,我最终的做法略有不同,这种方法非常完整且编码良好。很好的参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多