Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I've done a LOT of googling for this but haven't found anything that works.

How can I set the Scroll {Vertical & Horizontal} within the Webbrowser Control?

What I'd like to do is set them to a particular spot on the page {I have multiple Webbrowsers [25 - 5x5 grid] and want them each to go to different spots so I can get various info at a glance}.

Also, once done getting info I'd like to push a single button on the form for all the Webbrowser Controls to advance to the next set of data.

Right now all I have for a code is linking each to a number via "Dictionary" so all I'll have to do is make up the formula which will send each Webbrowser to it's correct location on the page(s). It's not much, but I SHOULD be able to work out the rest of the code once I'm able to get these bloody things to scroll.

Thanks in advance!!!

Peace to you and yours,
Matthew "Dra'Gon" Stohler
Posted
Updated 4-May-12 10:57am
v3

1 solution

Hi You can use these function & com services
VB
Imports System.Runtime.InteropServices.Marshal
 Private Shared ScrollVal As Long = 500

    ' Scrollbar direction
    ' All these constents can be found in WinUser.h
    '
    Const SBS_HORZ = 0
    Const SBS_VERT = 1

    ' Windows Messages
    ' All these constents can be found in WinUser.h
    '
    Const WM_VSCROLL = &H115
    Const WM_HSCROLL = &H114
    Const SB_THUMBPOSITION = 4

Public Enum eScrollAction
        Jump = 0
        Relitive = 1
    End Enum

    Public Enum eScrollDirection
        Vertical = 0
        Horizontal = 1
    End Enum

    Private Declare Function GetScrollPos Lib "user32.dll" ( _
            ByVal hWnd As IntPtr, _
            ByVal nBar As Integer) As Integer

    ' API Function: SetScrollPos
    ' Sets ONLY the scrollbar DOES NOT change the control object
    '
    Private Declare Function SetScrollPos Lib "user32.dll" ( _
            ByVal hWnd As IntPtr, _
            ByVal nBar As Integer, _
            ByVal nPos As Integer, _
            ByVal bRedraw As Boolean) As Integer

    ' API Function: PostMessageA
    ' Sends a message to a control (We are going to tell it to synch
    ' with the scrollbar)
    '
    Private Declare Function PostMessageA Lib "user32.dll" ( _
            ByVal hwnd As IntPtr, _
            ByVal wMsg As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As Integer) As Boolean



VB
Private Sub scrollControl(ByVal hWnd As IntPtr, ByVal Direction As eScrollDirection, ByVal Action As eScrollAction, ByVal Amount As Integer)
       On Error Resume Next
       Dim position As Integer

       ' What direction are we going
       If Direction = eScrollDirection.Horizontal Then

           ' What action are we taking (Jumping or Relative)
           If Action = eScrollAction.Relitive Then
               position = GetScrollPos(hWnd, SBS_HORZ) + Amount
           Else
               position = Amount
           End If

           ' Make it so
           If (SetScrollPos(hWnd, SBS_HORZ, position, True) <> -1) Then
               PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION + &H10000 * position, Nothing)
           Else
               MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")")
           End If

       Else

           ' What action are we taking (Jumping or Relative)
           If Action = eScrollAction.Relitive Then
               position = GetScrollPos(hWnd, SBS_VERT) + Amount
           Else
               position = Amount
           End If

           ' Make it so
           If (SetScrollPos(hWnd, SBS_VERT, position, True) <> -1) Then
               PostMessageA(hWnd, WM_VSCROLL, SB_THUMBPOSITION + &H10000 * position, Nothing)
           Else
               MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")")
           End If
       End If
   End Sub


HOW TO USE :-
VB
scrollControl(control.Handle, eScrollDirection.Vertical, eScrollAction.Jump, ScrollVal)
 
Share this answer
 
v2
Comments
BlueDragonFire 9-May-12 15:35pm    
Okay, I suppose the name of my WebBrowser goes to where the "FlowLayoutPanel1" is. It produces an Error as-is so I tried the control's name and the Error was gone.

Does the rest of it go into the main Form code {which is where I just tried it}? Or where?

I have it to trigger using a button and a set of coordinates:

Friend Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
scrollControl(wbrRec1.Handle, eScrollDirection.Horizontal, eScrollAction.Jump, 71)
scrollControl(wbrRec1.Handle, eScrollDirection.Vertical, eScrollAction.Jump, 18)
End Sub

But it isn't working {at least I see no change in the WebBrowser control. I just figure I'm not putting it in correctly or something.

I appreciate your help with this!!!

Peace to you and yours,
Matthew "Dra'Gon" Stohler
Jack_321 9-May-12 15:43pm    
you can try one thing first.
comment this "On Error Resume Next" in function. then make all vars & function friend or public & make web browser control public. you can put all these code into the main form or where the control is, at least I did this.
Jack_321 9-May-12 15:44pm    
commenting this "On Error Resume Next" will trigger errors if any. then you can be specific.
Jack_321 9-May-12 15:47pm    
if you like my answer then please vote it.
BlueDragonFire 9-May-12 16:53pm    
Still nothing.

I checked the data in the "scrollControl" {using MsgBox} and it's showing that everything is getting there fine. And Using "Break" and "Step" it's getting through "scrollControl" as it's suppose to. It's just not doing anything {I tried Jump & Relative}.

Though when I use "MsgBox(GetScrollPos(hWnd, SBS_HORZ))" it always comes out 0 even when I scroll the controls manually to different spots.

Peace to you and yours,
Matthew "Dra'Gon" Stohler

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900