Thread: Text Boxes
View Single Post
Old Jun 5th, 2005, 2:31 PM   #7
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Ok, this should work then. When Command1 is clicked, it moves the I-Bar to the start of the 4th line (index of line is 3 as base 0) of text1.
Private Sub Command1_Click()
    With Text1
        .SelLength = 0
        .SelStart = GetLineStart(Text1.Text, 3)
        .SetFocus
    End With
End Sub

Private Function GetLineStart(ByVal nText As String, ByVal nLine As Integer) As Integer
' Returns the starting character (base 0)
' of line nLine (base 0) of nText

Dim nLines() As String
Dim nLineEnum As Integer
Dim nLinePos As Integer
    nLines = Split(nText, vbNewLine)
    For nLineEnum = 0 To sUbound(nLines)
        If nLineEnum = nLine Then
            GetLineStart = nLinePos
            Exit For
        End If
        nLinePos = nLinePos + Len(nLines(nLineEnum) & vbNewLine)
    Next
End Function

Private Function sUbound(ByRef What() As String) As Integer
' Returns ubound of what or -1 if empty

On Error GoTo UnDimensioned:
    sUbound = UBound(What)
Exit Function
UnDimensioned:
    sUbound = -1
End Function
Rory is offline   Reply With Quote