Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String VB6文本框字体权重操作_String_Vb6_Textbox - Fatal编程技术网

String VB6文本框字体权重操作

String VB6文本框字体权重操作,string,vb6,textbox,String,Vb6,Textbox,我有一个VB6应用程序,我想在其中操作在文本框中输出的字符串的某些部分 txtPhoneNums.Text = "Home: " + strHomeNo + vbCrLf _ + "Mobile: " + strMobileNo + vbCrLf + "Work: " + strWorkNo + vbCrLf 它嵌套在执行各种验证的if语句中。例如,我希望能够在上面的代码片段中突出显示单词“Work”和附加的字符串值“strWorkNo”,以红色和粗

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

txtPhoneNums.Text = "Home:  " + strHomeNo + vbCrLf _
                    + "Mobile: " + strMobileNo + vbCrLf + "Work:  " + strWorkNo + vbCrLf
它嵌套在执行各种验证的if语句中。例如,我希望能够在上面的代码片段中突出显示单词“Work”和附加的字符串值“strWorkNo”,以红色和粗体显示。在不创建多个文本框(并将其他两个值保留为默认外观)的情况下,我是否可以轻松地执行此操作

谢谢


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

您希望使用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

标准vb6文本框不允许混合格式,您需要查找RichTextBox控件。作为旁注,我更愿意使用&而不是+Hi,谢谢。:)如果txtPhoneNums.Text是一个RichTextBox,那么语法将如何显示?Mark的答案是一个很好的例子。很好的答案,谢谢。最后我做的稍微不同,因为我有大量可能的场景,这个方法非常完整并且编码良好。非常好的参考资料。