Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text box colors...

Status
Not open for further replies.

KEJinSC

Programmer
Sep 12, 2002
7
US
Is there any way to place a text box (or similar control) on a form and change the foreground and background colors on a portion of it? I would like to display a string, allow the user to define a starting position and length and have it highlighted in the display. I would like it to look similar to the selStart and selLength only I would like it to maintain the display even when the text box loses focus.

Thanks for any suggestions.
Keith Johnson
 
U can use a Label control and change it to 3D.It is like a textbox. Behnam2204@yahoo.com
BehnamPro
 
Well, if you are going to be inserting the text in the box with code, and not having the user enter it, then you could use 2 label controls like this:

Place a Label control on your form, and then paste a copy of it to create a control array. Put the copy directly behind the first one. Set the properties of the controls like this:

Label1(0):
Backstyle = Transparent
Borderstyle = Fixed Single

Label1(1):
Caption = ( No caption )
Visible = False
Borderstyle = None
Backcolor = 'Select the highlight color that you want. Note that very dark colors will not work too well. Because this does not change the foreground color of the selected text.

In code, do this:

Declarations:

Dim CutStart As Integer


Private Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

Label1(1).Visible = False
CutStart = X

End Sub


Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim I As Integer
Dim R As Integer
Dim CutSize As Integer

If Button = vbLeftButton Then

Label1(1).Visible = False

' Adjust CutStart to the beginning of a character. Allow for the border.

For I = 0 To Len(Label1(0).Caption)
If TextWidth(Left$(Label1(0).Caption, I)) > CutStart Then

CutStart = TextWidth(Left$(Label1(0).Caption, I - 1)) + 20
GoTo Jump

End If

Next I
Jump:
R = I
CutSize = X - CutStart

' Adjust Cutsize to the end of a character
For I = 0 To Len(Label1(0).Caption)

If (TextWidth(Left$(Label1(0).Caption, I)) > CutStart + CutSize) And (I > R) Then

CutSize = TextWidth(Mid$(Label1(0).Caption, R, I - R)) + 30

GoTo Jump2
End If
Next I
Jump2:

' If CutSize exceeds the lenth of the text, chop it off

If (CutStart + CutSize) > TextWidth(Label1(0).Caption) Then
CutSize = TextWidth(Label1(0).Caption) - CutStart + 45
End If

' draw the background label

If CutSize < 0 Then Exit Sub

Label1(1).Left = Label1(0).Left + CutStart

Label1(1).Width = CutSize

Label1(1).Visible = True

End If

End Sub


So, when you place the text in the Label1(0).Caption, the user can select text and it will be highlighted, and it will stay that way even if the control loses focus.

HTH

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top