NEWBIE (in VB.Net): scollbar event question

  • Thread starter Thread starter Antony
  • Start date Start date
A

Antony

Please excuse me for yet another silly question but which event on a
USERCONTROL indicates that the horizontal or vertical scrollbar has
changed position. I have written the code below which contains a
USERCONTROL with scrollbars on horizontal and vertical positions (as
Me.testUserControl.AutoScrollMinSize is greater than
Me.testUserControl.Size). It can be pasted directly into Visual
Studio, the whole thing into and over "Form1" if creating a new
project to see what I mean. All I want to do is have an event raised
by the usercontrol to Form1 saying "My scrollbars have changed
position, and current values are ...." but I just can't find the event
to use.

Thank you and apologies for asking all these simple questions without
contributing back to the newsgroup.
Tony


Public Class testUserControl
Inherits System.Windows.Forms.UserControl

Private Sub InitializeComponent()
Me.Name = "testUserControl"
Me.Size = New System.Drawing.Size(136, 150)
End Sub
End Class



Public Class Form1
Inherits System.Windows.Forms.Form

Public Sub New()
MyBase.New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Private components As System.ComponentModel.IContainer

Friend WithEvents testUserControl As
WindowsApplication7.testUserControl
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.testUserControl = New testUserControl
Me.SuspendLayout()

Me.testUserControl.AutoScroll = True
Me.testUserControl.AutoScrollMinSize = New
System.Drawing.Size(100, 100)
Me.testUserControl.Location = New System.Drawing.Point(24, 16)
Me.testUserControl.Name = "UserControl11"
Me.testUserControl.Size = New System.Drawing.Size(72, 80)
Me.testUserControl.TabIndex = 0

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.testUserControl)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.testUserControl.AutoScrollMinSize = New Size(200, 200)
Me.testUserControl.BackColor = Color.Blue
Me.BackColor = Color.Gray
Me.testUserControl.Size = New Size(100, 100)
End Sub
End Class
 
I don't think there is an OnVScroll sub to override.

For example, I added the method OnVScroll to his class and the IDE yields
(sub 'OnVScroll' cannot be decalred 'Overrides' because it does not override
a sub in a base class). I only post this because months back I had the same
issue, and didn't get it resolved. I have put the modified source below.



Public Class testUserControl
Inherits System.Windows.Forms.UserControl

Private Sub InitializeComponent()
Me.Name = "testUserControl"
Me.Size = New System.Drawing.Size(136, 150)
End Sub

Protected Overrides Sub OnVScroll(ByVal e As EventArgs)
'ERROR WITH OVERRIDES - SEE TEXT IN MAIN BODY OF POSTING
'Whatever he wants to do here, possibly raise an event etc.
End Sub
End Class
 
Try this.

Add this to "testUserControl"
========================
Public Event OnAutoScroll()
Private scrollBarInternalPosition As Point = MyBase.AutoScrollPosition
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If Not scrollBarInternalPosition.Equals(Me.AutoScrollPosition) Then
scrollBarInternalPosition = Me.AutoScrollPosition
RaiseEvent OnAutoScroll()
End If
End Sub

Add this to "Form1"
========================
Private Sub whatever() Handles testUserControl.OnAutoScroll
Debug.WriteLine("Whatever you wanted to do")
End Sub
 
OK, well you can sub class it if VScroll is a protected event. Then you can
add an event handler to use it.
 
Back
Top