Link to home
Start Free TrialLog in
Avatar of geostp
geostp

asked on

Vscrollbar in MULTILINE TEXTBOX

I have two multiline text boxes. One text box contains text
on seeing these text i want to type the text in another text
box. As i type down, vertical scroll bar scrolls in the text
box i type, So at the same time the Vscroll bar in the source
text box should also scroll. How make the two vscrollbar scroll simultaneously and equally?
ASKER CERTIFIED SOLUTION
Avatar of cymbolic
cymbolic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cymbolic
cymbolic

32bit:
*********first, some declares***************

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SendMessageBystring Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hDC As Long, lpMetrics As TEXTMETRIC) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long

Type RECT
        left As Long
        top As Long
        right As Long
        bottom As Long
End Type

Type TEXTMETRIC
        tmHeight As Long
        tmAscent As Long
        tmDescent As Long
        tmInternalLeading As Long
        tmExternalLeading As Long
        tmAveCharWidth As Long
        tmMaxCharWidth As Long
        tmWeight As Long
        tmOverhang As Long
        tmDigitizedAspectX As Long
        tmDigitizedAspectY As Long
        tmFirstChar As Byte
        tmLastChar As Byte
        tmDefaultChar As Byte
        tmBreakChar As Byte
        tmItalic As Byte
        tmUnderlined As Byte
        tmStruckOut As Byte
        tmPitchAndFamily As Byte
        tmCharSet As Byte
End Type


Private Function GetVisibleLines%()
    Dim rc As RECT
    Dim hDC&
    Dim lfont&, oldfont&
    Dim di&
    Dim tm As TEXTMETRIC

    ' Get the formatting rectangle - this describes the

    ' rectangle in the control in which text is placed.
    lc% = SendMessage(Text1.hWnd, EM_GETRECT, Ø, rc)

    ' Get a handle to the logical font used by the control.
    ' The VB font properties are accurately reflected by
    ' this logical font.
    lfont = SendMessageBynum(Text1.hWnd, WM_GETFONT, Ø, Ø&)
   
    ' Get a device context to the text control.
    hDC = GetDC(Text1.hWnd)

    ' Select in the logical font to obtain the exact font
    ' metrics.

    If lfont <> Ø Then oldfont = SelectObject(hDC, lfont)

    di = GetTextMetrics(hDC, tm)
    ' Select out the logical font
    If lfont <> Ø Then lfont = SelectObject(hDC, oldfont)

    ' The lines depend on the formatting rectangle and font height
    GetVisibleLines% = (rc.bottom - rc.top) / tm.tmHeight

    ' Release the device context when done.
    di = ReleaseDC(Text1.hWnd, hDC)
End Function

*******then, to scroll*************
    ' Get the number of the first and last visible line
    firstvisible% = SendMessageBynum(Text1.hWnd, EM_GETFIRSTVISIBLELINE, Ø, Ø&)
    lastvisible% = GetVisibleLines%() + firstvisible% - 1
   
    ' Scroll it into view if necessary
    If (VScroll1.value < firstvisible%) Then
        dl& = SendMessageBynum(Text1.hWnd, EM_LINESCROLL, Ø, CLng(VScroll1.value - firstvisible%))
    End If
    If (VScroll1.value > lastvisible%) Then
        dl& = SendMessageBynum(Text1.hWnd, EM_LINESCROLL, Ø, CLng(VScroll1.value - lastvisible%))

    End If

***********other interesting things you can do *************
'
' Determine the number of the first line visible in the text control
'
Private Sub MenuFirstVisible_Click()
    Dim lc%
    lc% = SendMessageBynum(Text1.hWnd, EM_GETFIRSTVISIBLELINE, Ø, Ø&)
    LabelResult.Caption = "Line" + Str$(lc%) + " at top"

End Sub

'
' Determine the number of lines of text in the text control

'
Private Sub MenuLineCount_Click()
    Dim lc%
    lc% = SendMessageBynum(Text1.hWnd, EM_GETLINECOUNT, Ø, Ø&)
    LabelResult.Caption = Str$(lc%) + " lines"
End Sub

'
' Determine the number of visibile lines in the text control.
'
Private Sub MenuLinesVisible_Click()
    LabelResult.Caption = Str$(GetVisibleLines()) + " lines visible"
End Sub

'
' Determine the start and end position of the current selection
'
Private Sub MenuSelected_Click()
    Dim ls&

    ls& = SendMessageBynum&(Text1.hWnd, EM_GETSEL, Ø, Ø&)

    LabelResult.Caption = "Chars" + Str$(CInt(ls& And &HFFFF&)) + " to" + Str$(CInt(ls& / &H1ØØØØ))


End Sub

*********for a more complete description, it's worth your money to buy "Visual Basic and the Win32 API" by Dan Appleman
Oops! here are the constants you'll need (plus some others):
Public Const EM_GETSEL = &HB0
Public Const EM_SETSEL = &HB1
Public Const EM_GETRECT = &HB2
Public Const EM_SETRECT = &HB3
Public Const EM_SETRECTNP = &HB4
Public Const EM_SCROLL = &HB5
Public Const EM_LINESCROLL = &HB6
Public Const EM_SCROLLCARET = &HB7
Public Const EM_GETMODIFY = &HB8
Public Const EM_SETMODIFY = &HB9
Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINEINDEX = &HBB
Public Const EM_SETHANDLE = &HBC
Public Const EM_GETHANDLE = &HBD
Public Const EM_GETTHUMB = &HBE
Public Const EM_LINELENGTH = &HC1
Public Const EM_REPLACESEL = &HC2
Public Const EM_GETLINE = &HC4
Public Const EM_LIMITTEXT = &HC5
Public Const EM_CANUNDO = &HC6
Public Const EM_UNDO = &HC7
Public Const EM_FMTLINES = &HC8
Public Const EM_LINEFROMCHAR = &HC9
Public Const EM_SETTABSTOPS = &HCB
Public Const EM_SETPASSWORDCHAR = &HCC
Public Const EM_EMPTYUNDOBUFFER = &HCD
Public Const EM_GETFIRSTVISIBLELINE = &HCE
Public Const EM_SETREADONLY = &HCF
Public Const EM_SETWORDBREAKPROC = &HD0
Public Const EM_GETWORDBREAKPROC = &HD1
Public Const EM_GETPASSWORDCHAR = &HD2
Public Const WM_GETFONT = &H31