Re: Multiline Textbox issue
From: Bob O`Bob (filterbob_at_yahoogroups.com)
Date: 02/26/04
- Next message: Michael Culley: "Re: VB6 vs VB .Net"
- Previous message: Tore Bostrup: "Re: Data Grid help"
- Next in thread: Rick Rothstein: "Re: Multiline Textbox issue"
- Reply: Rick Rothstein: "Re: Multiline Textbox issue"
- Maybe reply: Ravichandran J.V.: "Re: Multiline Textbox issue"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 25 Feb 2004 20:29:56 -0800
I'm catching up on some of the threads I missed at month-end when I was
busy moving...
On Fri, Jan 30, 2004, at about 09:57:35 -0500, while crossposting
to a couple more newsgroups now omitted, Rick Rothstein wrote:
>
> > I have a VB application in which I want to put my cursor on a desired
> > location/row in a multiline text box.
> >
> > I understand we can use SendMessage or SendMessageLong function for this
> > purpose.
> >
> > But can anyone give me the exact code for the user function which
> > accomplishes this?
>
> I'm sure there must be a shorter way to do this,
...Ahem!
Public Sub PositionByLine(txtTgt As TextBox, lLine As Long)
txtTgt.SelStart = Len(txtTgt.Text) - Len(Split(txtTgt.Text, vbNewLine, lLine)(lLine - 1))
End Sub
It lacks any range checking, but works fine for me otherwise.
> but here is a quick
> modification of another routine that I had laying around. Paste the
> following code into your form's code window and specify the Line Number
> (first line is numbered 1) and the CaretPosition (number of characters in
> front of the caret, so 0 is at the very beginning of the line). If you
> specify a LineNumber greater than the number of lines in the TextBox, the
> caret will be placed on the last line. If you specify a CaretPosition
> greater than the number of characters on the specified line, the caret will
> be placed at the end of that line.
>
> Rick - MVP
>
> Private Declare Function LockWindowUpdate Lib "user32" _
> (ByVal hwndLock As Long) As Long
>
> Private Declare Function SendMessage Lib "user32" _
> Alias "SendMessageA" _
> (ByVal hwnd As Long, _
> ByVal wMsg As Long, _
> ByVal wParam As Integer, _
> ByVal lParam As Long) _
> As Long
>
> Private Const EM_FMTLINES = &HC8
> Private Const EM_LINEFROMCHAR = &HC9
>
> Sub PositionCursor(LineNumber As Long, CaretPosition As Long)
> Dim MaxLine As Long
> Dim Increment As Long
> Dim TBoxText As String
> Dim IndividualLines() As String
> With Text1
> SendMessage .hwnd, EM_FMTLINES, 1, 0#
> TBoxText = Replace$(.Text, vbCr & vbCrLf, vbNewLine)
> SendMessage .hwnd, EM_FMTLINES, 0, 0#
> IndividualLines = Split(TBoxText, vbNewLine)
> LockWindowUpdate .hwnd
> .SelStart = Len(.Text)
> MaxLine = SendMessage(.hwnd, EM_LINEFROMCHAR, -1&, 0&) + 1
> If LineNumber >= MaxLine Then
> .SelStart = Len(.Text)
> ElseIf LineNumber < 1 Then
> .SelStart = 0
> Else
> .SelStart = 0
> Increment = Len(.Text) \ 2
> Do Until Increment = 0 Or SendMessage(.hwnd, _
> EM_LINEFROMCHAR, -1&, 0&) + 1 = LineNumber
> Do While SendMessage(.hwnd, _
> EM_LINEFROMCHAR, -1&, 0&) + 1 <= LineNumber
> .SelStart = .SelStart + Increment
> Loop
> .SelStart = .SelStart - Increment
> Increment = Increment \ 2
> Loop
> End If
> .SetFocus
> SendKeys "{HOME}"
> LockWindowUpdate False
> DoEvents
> If CaretPosition < Len(IndividualLines(SendMessage(.hwnd, _
> EM_LINEFROMCHAR, -1&, 0&))) Then
> .SelStart = .SelStart + CaretPosition
> Else
> SendKeys "{END}"
> End If
> End With
> End Sub
...so, who gets the "one liner award" for today, eh?
Bob
-- looking for work again <http://obob.com/bob/resume/>
- Next message: Michael Culley: "Re: VB6 vs VB .Net"
- Previous message: Tore Bostrup: "Re: Data Grid help"
- Next in thread: Rick Rothstein: "Re: Multiline Textbox issue"
- Reply: Rick Rothstein: "Re: Multiline Textbox issue"
- Maybe reply: Ravichandran J.V.: "Re: Multiline Textbox issue"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|