Click here to Skip to main content
15,886,919 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad2-Jul-12 23:57
Ammar_Ahmad2-Jul-12 23:57 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten3-Jul-12 0:16
Nick Otten3-Jul-12 0:16 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad3-Jul-12 13:36
Ammar_Ahmad3-Jul-12 13:36 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten4-Jul-12 1:01
Nick Otten4-Jul-12 1:01 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad6-Jul-12 13:24
Ammar_Ahmad6-Jul-12 13:24 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten8-Jul-12 20:38
Nick Otten8-Jul-12 20:38 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad10-Jul-12 2:32
Ammar_Ahmad10-Jul-12 2:32 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten10-Jul-12 3:13
Nick Otten10-Jul-12 3:13 
hmm oke i admid my code was a bit of mess. i cleared it out a bit and made a example program so you can see how it works. Please follow the below steps.

1. start a new form project
2. Add is Listbox. set the name to txtscroll and the size to 254; 381
3. Add a button, call this one btnadd
4. copy paste the following code into the project (under the form you just made)
VB
Imports System.Runtime.InteropServices

Public Class Form1

#Region "declarations"
    Dim WithEvents scrollhandler As MyListener 'class that will watch windows to see if your scrolling
    Private scrolled As Boolean = False 'boolean you can use to flip if a message box was displayed or not...(so it wont loop the messagebox.show)
    Private maxscroll As Int64 'int used to store the max value of the scrollbar
    Private index As Int32 = 0 'used for random loops to fill the txtscroll
    Private Const SB_HORZ As Integer = &H0
    Private Const SB_VERT As Integer = &H1
#End Region
    

#Region "dll calls"
    'dll used to get the scrollbar value NOTE: if the scrollbar is not visible or you go higher then your max it will crash!
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function GetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer) As Integer
    End Function

    'dll used to set the scrollbar value NOTE: if the scrollbar is not visible or you go higher then your max it will crash!
    <DllImport("user32.dll")> _
    Private Shared Function SetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer, ByVal nPos As Integer, ByVal bRedraw As Boolean) As Integer
    End Function
#End Region
    
    Public Property HScrollPos() As Integer 'property to set or get the horisontal scrollbar value
        Get
            Return GetScrollPos(DirectCast(txtscroll.Handle, IntPtr), SB_HORZ) 'change txtscroll to the control you use. dont forget the .handle
        End Get
        Set(ByVal value As Integer)
            SetScrollPos(DirectCast(txtscroll.Handle, IntPtr), SB_HORZ, value, True) 'change txtscroll to the control you use. dont forget the .handle
        End Set
    End Property


    Public Property VScrollPos() As Integer 'Property to set or get the Vertical scrollbar value
        Get
            Return GetScrollPos(DirectCast(txtscroll.Handle, IntPtr), SB_VERT) 'change txtscroll to the control you use. dont forget the .handle
        End Get
        Set(ByVal value As Integer)
            SetScrollPos(DirectCast(txtscroll.Handle, IntPtr), SB_VERT, value, True) 'change txtscroll to the control you use. dont forget the .handle
        End Set
    End Property

#Region "form events"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'some random loop i used to ditch some data in my listbox for testing

        Do While index < 1000
            txtscroll.Items.Add(index.ToString())
            index += 1
        Loop
        
        scrollhandler = New MyListener(txtscroll)
        maxscroll = CalcMaxScroll(txtscroll.Size.Height, txtscroll.Size.Width, 13, 254, txtscroll.Items.Count) 'see my forum post
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        'random loop that will add 10 extra items to the listbox
        Dim i As Int32 = index
        Do While i < index + 10
            txtscroll.Items.Add((index + i).ToString())
            i += 1
        Loop
        index += 10
        maxscroll = CalcMaxScroll(txtscroll.Size.Height, txtscroll.Size.Width, 13, 254, txtscroll.Items.Count) 'see my forum post
    End Sub

    Private Sub txtscroll_MouseWheelScroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtscroll.MouseWheel
        scrollhandler_MyScroll(e, e) 'needed to catch the scrollwheel event.
    End Sub


    Private Sub scrollhandler_MyScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles scrollhandler.MyScroll
        If VScrollPos = maxscroll And scrolled Then 'the scrollbar is down and no message was given, showing a messagebox
            MessageBox.Show("you reached the max on the verticle scrollbar!", "yay")
            scrolled = False 'messagebox was shown, flip bool to prevent loop
        ElseIf VScrollPos < maxscroll Then
            scrolled = True 'the scroll value is smaller then the maximum scroll value. meaning the next time the bar hits the bottem a new textbox should be shown
        End If
    End Sub
#End Region

#Region "calculations"
    'see my forum post for more info about this thing
    Private Function CalcMaxScroll(ByVal controlhight As Int32, ByVal controlwith As Int32, ByVal itemhight As Int32, ByVal itemwith As Int32, ByVal totalitems As Int32)
        Return totalitems - (Math.Floor(controlwith / itemwith) * Math.Floor(controlhight / itemhight))
    End Function
#End Region

    
#Region "native scroll listner"
    Private Class MyListener
        Inherits NativeWindow

        Public Event MyScroll(ByVal sender As Object, ByVal e As EventArgs)
        Const WM_MOUSEACTIVATE = &H21
        Const WM_MOUSEMOVE = &H200
        Private control As Control

        Public Sub New(ByVal control As Control)
            AssignHandle(control.Handle) 'docks handler to given control
        End Sub

        Protected Overrides Sub WndProc(ByRef Messageevent As Message)
            Const WM_HSCROLL = &H114
            Const WM_VSCROLL = &H115
            If Messageevent.Msg = WM_HSCROLL Or Messageevent.Msg = WM_VSCROLL Then 'looks if the horisontal or verticle scrollbar is moving
                'one of the scrollbars is moving, throw event
                RaiseEvent MyScroll(control, New EventArgs)
            End If
            MyBase.WndProc(Messageevent)
        End Sub

        Protected Overrides Sub Finalize()
            'not used in example but drops the scroll watcher
            ReleaseHandle()
            MyBase.Finalize()
        End Sub

    End Class
#End Region


End Class


5. run it scroll all the way down, hit the button and scroll down again.

oke now how does it work:
I explained the working of the scroll handlers before so to save some text i'll skip past those in here. the interesting part is in the button klick event. the first few lines is a random loop that will add 10 extra items to my listbox. i gues thats not to interesting, the line following after is.

It will set the maxscroll value to the return of the CalcMaxScroll sub. this sub requires you to give it the following information: The height property of your control. The With property of your control. the Height of the item you send to it (a listbox row is 13 high by default). the With of the item you send to it (in a listbox thats the full with). and the total ammount of items in the listbox. it will return you the maximum scroll position of your scrollbar.

Tought i now use this function with a listbox (i was to lazy to make a new project) it will work perfectly fine aswell with a flowlayout panel. just remember to send all the info to the function and that all the items in the box are the same size. if they aint the same size calculating will get a little harder....but not impossible.

ps: just put a breakpoint on the button klick event and watch what happends after the loop. keep the breakpoint on and scroll a bit. things get pretty clear once you see it running.
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad10-Jul-12 15:15
Ammar_Ahmad10-Jul-12 15:15 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten10-Jul-12 20:41
Nick Otten10-Jul-12 20:41 
AnswerRe: Event on Scrolling to the end in flowlayoutpanel Pin
Luc Pattyn29-Jun-12 2:56
sitebuilderLuc Pattyn29-Jun-12 2:56 
JokeRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten29-Jun-12 3:01
Nick Otten29-Jun-12 3:01 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Luc Pattyn29-Jun-12 4:19
sitebuilderLuc Pattyn29-Jun-12 4:19 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Nick Otten29-Jun-12 4:31
Nick Otten29-Jun-12 4:31 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad29-Jun-12 8:28
Ammar_Ahmad29-Jun-12 8:28 
AnswerRe: Event on Scrolling to the end in flowlayoutpanel Pin
Luc Pattyn29-Jun-12 9:00
sitebuilderLuc Pattyn29-Jun-12 9:00 
GeneralRe: Event on Scrolling to the end in flowlayoutpanel Pin
Ammar_Ahmad30-Jun-12 0:08
Ammar_Ahmad30-Jun-12 0:08 
QuestionHelp for FTP client upload Pin
mikrophun27-Jun-12 21:01
mikrophun27-Jun-12 21:01 
AnswerRe: Help for FTP client upload Pin
Bernhard Hiller27-Jun-12 21:37
Bernhard Hiller27-Jun-12 21:37 
GeneralRe: Help for FTP client upload Pin
mikrophun27-Jun-12 21:42
mikrophun27-Jun-12 21:42 
GeneralRe: Help for FTP client upload Pin
mikrophun27-Jun-12 22:27
mikrophun27-Jun-12 22:27 
AnswerRe: Help for FTP client upload Pin
Eddy Vluggen28-Jun-12 1:55
professionalEddy Vluggen28-Jun-12 1:55 
AnswerRe: Help for FTP client upload Pin
mikrophun28-Jun-12 17:40
mikrophun28-Jun-12 17:40 
QuestionBasic program, very basic code, but still error, why!. [rich text box] Pin
Student1023027-Jun-12 20:55
Student1023027-Jun-12 20:55 
AnswerRe: Basic program, very basic code, but still error, why!. [rich text box] Pin
Bernhard Hiller27-Jun-12 21:39
Bernhard Hiller27-Jun-12 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.