Dovresti utilizzare un controllo RichTextBox al posto della normale
TextBox.
Per impostare i valori di una parte di testo, quest'ultima deve
essere prima selezionata.
Ti posto un esempio da MSDN (dove Text2 è il nome che hai dato al
controllo) :
codice:
Private Sub Command4_Click()
HighlightWords Text2, "width", vbRed
End Sub
Private Function HighlightWords(rtb As RichTextBox, _
sFindString As String, _
lColor As Long) _
As Integer
Dim lFoundPos As Long 'Position of first character of match
Dim lFindLength As Long 'Length of string to find
Dim lOriginalSelStart As Long
Dim lOriginalSelLength As Long
Dim iMatchCount As Integer 'Number of matches
'Save the insertion points current location and length
lOriginalSelStart = rtb.SelStart
lOriginalSelLength = rtb.SelLength
'Cache the length of the string to find
lFindLength = Len(sFindString)
'Attempt to find the first match
lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight)
While lFoundPos > 0
iMatchCount = iMatchCount + 1
rtb.SelStart = lFoundPos
'The SelLength property is set to 0 as
'soon as you change SelStart
rtb.SelLength = lFindLength
rtb.SelColor = lColor
'Attempt to find the next match
lFoundPos = rtb.Find(sFindString, _
lFoundPos + lFindLength, , rtfNoHighlight)
Wend
'Restore the insertion point to its original
'location and length
rtb.SelStart = lOriginalSelStart
rtb.SelLength = lOriginalSelLength
'Return the number of matches
HighlightWords = iMatchCount
End Function
Ciao,