ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   highlight text in userform textbox (https://www.excelbanter.com/excel-programming/344444-highlight-text-userform-textbox.html)

RB Smissaert

highlight text in userform textbox
 
How would I highlight a certain character in a plain multiline textbox on a
userform
given the line number and the character number in that line?

So for example given this text:

This is a test.
Trying to highlight.

and given line number 2 and character number 15
the character l in the second line would be highlighted.

RBS


RB Smissaert

highlight text in userform textbox
 
Not the most elegant method perhaps, but it does work:


Option Explicit

Private Sub UserForm_Initialize()

TextBox1.Text = "0 - Option Explicit" & vbCrLf & _
"1 - Option Compare Binary" & vbCrLf & _
"2 - Public IDWhereSQL As String" & vbCrLf & _
"3 - Public DoAddress As Boolean" & vbCrLf & _
"4 - Public DoPhone As Boolean" & vbCrLf & _
"5 - Private IDSelectSQL As String" & vbCrLf & _
"6 - Private IDFromClause As String" & vbCrLf & _
"7 - Private DoIDFromClause As Boolean"

TextBox2.Text = "2"
TextBox3.Text = "0"

End Sub


Private Sub CommandButton1_Click()

Dim i As Long
Dim lCurXStart As Long
Dim lChar As Long
Dim lLineToReach As Long
Dim lCharToReach As Long

lLineToReach = Val(TextBox2.Text)
lCharToReach = Val(TextBox3.Text)

With TextBox1

.SetFocus
.SelStart = 0

'this is the position at the
'very left margin of the textbox
'---------------------------------------
lCurXStart = .CurX

Do Until (.CurLine = lLineToReach And _
lChar = lCharToReach) Or i = Len(.Text)

.SelStart = i
lChar = lChar + 1
i = i + 1

'reset the lChar at the left margin
'----------------------------------
If .CurX = lCurXStart Then
lChar = 0
End If

DoEvents
Loop

.SelLength = 1

End With

End Sub


RBS


"RB Smissaert" wrote in message
...
How would I highlight a certain character in a plain multiline textbox on
a userform
given the line number and the character number in that line?

So for example given this text:

This is a test.
Trying to highlight.

and given line number 2 and character number 15
the character l in the second line would be highlighted.

RBS




All times are GMT +1. The time now is 06:43 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com