Re: Multiline Textbox issue

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bob O`Bob (filterbob_at_yahoogroups.com)
Date: 02/26/04


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/>


Relevant Pages

  • Re: running macro from 1 wks that manipulates another
    ... specify the 4th argument. ... > worksheet with the button, you may not get what you expect. ... > Sub sDeleteSeriesHdr ... Dim strMainWks As String ...
    (microsoft.public.excel)
  • Re: List files from a direcotry
    ... It searches for the file type you specify or for all files. ... Files and folders in the specified directory are listed. ... > End Sub ... > Dim FSO As Object ...
    (microsoft.public.excel.programming)
  • Re: Set Page Number to One After Page Break
    ... I don't know how to specify which ... page on the worksheet should be set equal to p. ... Sub CheckForPageBreaks() ... Dim i As Integer ...
    (microsoft.public.excel.programming)
  • RE: VB to produce a record of all files in a directory
    ... End Sub ... Dim objFSO As Scripting.FileSystemObject ... Dim objFolder As Scripting.Folder ... 'Specify the file to look for... ...
    (microsoft.public.excel.programming)
  • Re: copy and paste a row
    ... "Rick Rothstein" wrote: ... Sub CopyRows() ... Dim Combo As Range ... Rick ...
    (microsoft.public.excel.programming)