gpt4 book ai didi

string - VB6 文本框字体粗细操作

转载 作者:行者123 更新时间:2023-12-02 11:47:06 38 4
gpt4 key购买 nike

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

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

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

谢谢。

为了清晰起见,添加了图片。我希望两个空字段字符串为红色且粗体。 enter image description here

最佳答案

您想要使用 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

关于string - VB6 文本框字体粗细操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160948/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com