Vb.net AddHandler MouseHover - vb.net

My Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim top As Integer = 0
For i = 0 To 10
Dim inLine As Integer = 8
Dim left As Integer = 0
For x = 0 To inLine
Dim s As New Panel
s.BackColor = Color.Black
s.Width = 10
s.Height = 10
s.Left = left
s.Top = top
left = left + 20
AddHandler s.MouseHover, AddressOf Panel1_MouseHover
Me.Controls.Add(s)
Next
top = top + 20
Next
End Sub
Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
/////---- Some code!! :/
End Sub
My code adding panels into my from , i want that when Mouse Hover on panel
the panel change the background color.
if someone not understand:
when my mouse hover my panel that i add to my form, how i change the panel back color?
Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
/////---- Some code to change the panel color !! :/
End Sub

I think the part you're missing is the fact that the sender parameter to the event handler method will always be whichever control it is that is raising the event. Before using it though, I would cast it to the correct type so you get the full advantages of intellisense and compiler type checking.
Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
Dim panel As Panel = CType(sender, Panel)
panel.BackColor = Color.White
End Sub

Related

How can get the correct Handler of an object?

I am making an application that requires generating panels dynamically, and in turn that each panel generated two events, one with left click and another with right mouse click.
The right click is the one that gives me trouble since I have not been able to call a Handler that I have put temporarily in the event of the left click, but now that I see that it works, I want to pass it to the ToolStripMenuItem event, but when it enters event, the sender takes ownership of the ToolStripMenuItem and in this case you would need the property "System.Windows.Forms.Panel" in order to work on the Panel object.
I am not sure if I am doing it correctly, can you support me with any idea how to do it?
Annex the code of what I have developed so far
Public Class Form1
Dim pb, pbdoors As New Panel
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pos As Int32 = 20
Dim contador As Int16 = 1
For i As Int16 = 1 To 3
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.Name = "Panel" & contador,
.Location = New Point(pos, 20)
}
AddHandler pb.Click, AddressOf myClickHandler_b
Me.Panel1.Controls.Add(pb)
pb.BringToFront()
pos = pos + 120
contador = contador + 1
Next
End Sub
End Class
Public Sub myClickHandler_b(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pos As Integer = Val(TextBox38.Text)
Dim clickedLabel As Panel = DirectCast(sender, Panel)
clickedLabel.Location = New Point((clickedLabel.Location.X + 120), clickedLabel.Location.Y)
TextBox38.Text = pos
End Sub
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
myClickHandler_b(sender, e)
End Sub
In order to recognize which mouse button is clicked you should use MouseClick as event handler.
The piece of code starting with and working on that is: “AddHandler pb.MouseClick……….”
I hope this might help you:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pos As Int32 = 20
Dim contador As Int16 = 1
For i As Int16 = 1 To 3
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.Name = "Panel" & contador,
.Location = New Point(pos, 20)
}
AddHandler pb.MouseClick, Sub(senderO As Object, eObj As MouseEventArgs)
If eObj.Button = MouseButtons.Left Then
'Do your tasks here
MsgBox("Left button clicked")
ElseIf eObj.Button = MouseButtons.Right Then
'Do your tasks here
MsgBox("Right button clicked")
End If
End Sub
Me.Panel1.Controls.Add(pb)
pb.BringToFront()
pos = pos + 120
contador = contador + 1
Next
End Sub
Winforms only provides a single event (Click) for both mouse buttons. You need to check (and cast, given that signature) the event arguments to know when you have a right click:
Public Sub myClickHandler_b(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mouseevent As MouseEventArgs = TryCast(e, MouseEventArgs)
If mouseevent IsNot Nothing AndAlso mouseevent.Button = MouseButtons.Right Then
RightClick(TryCast(sender, Panel))
Exit Sub
End If
'Left Click
'Ugh. Val() is not your friend.
Dim pos As Integer = Val(TextBox38.Text)
Dim clickedLabel As Control = DirectCast(sender, Control)
clickedLabel.Location = New Point((clickedLabel.Location.X + 120), clickedLabel.Location.Y)
TextBox38.Text = pos
End Sub
Public Sub RightClick(source As Panel)
End Sub
Now for the second part. In the ToolStripMenuItem1_Click() method, if you have several dynamic panels on your form, how is the method supposed to know which panel it's working with? You need something in this code knows that information, and uses it for the sender argument. Additionally, given the new handling for left vs right clicks, you also need to think carefully about how that will spill over into the click handler.
So ToolStripMenuItem1_Click() should look something (but probably not exactly!) like this:
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
myClickHandler_b(pb, Nothing)
End Sub

Visual Basic.Net: Data Grid View Disable scroll Bar but allow Mouse wheel Scroll

As the title implies, I have two data grid view side by side with each other. I would like to have the first data grid view to have no scroll bar but allow mouse wheel scroll. I have already managed to synchronize the 2 data grid view when I scroll the second data grid view. below is the code
Private Sub dgvA_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgvA.Scroll
dgvB.FirstDisplayedScrollingRowIndex = dgvA.FirstDisplayedScrollingRowIndex
dgvB.HorizontalScrollingOffset = dgvA.HorizontalScrollingOffset
End Sub
Private Sub dgvB_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dgvB.Scroll
dgvA.FirstDisplayedScrollingRowIndex = dgvB.FirstDisplayedScrollingRowIndex
dgvA.HorizontalScrollingOffset = dgvB.HorizontalScrollingOffset
End Sub
Managed to do so using these codes
Private Sub dgvA_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvA.MouseWheel
Dim currentIndex As Integer = Me.dgvA.FirstDisplayedScrollingRowIndex
Dim scrollLines As Integer = SystemInformation.MouseWheelScrollLines
Select Case e.Delta
Case (120)
Me.dgvA.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines)
Case (-120)
Me.dgvA.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines
End Select
End Sub

Trigger an event with one sub for multiple controls

I have 10 panels on my form, and when you hover them, their color changes. I have 10 private subs like so...
Private Sub pnl2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl2.MouseHover
pnl2.BackColor = Color.WhiteSmoke
End Sub
This code is repeated for each panel with the only difference being it's name, how can I do this more efficiently? as it is very repetitive.
Add them at the handler statement appending each by a comma. The sender object is the panel in question so cast it to change it's properties.
Private Sub pnl2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl2.MouseHover, pnl3.MouseHover 'etc
Dim pnl As Panel = CType(sender, Panel)
pnl.BackColor = Color.WhiteSmoke
End Sub

How to handle a variable amount of buttons clicked?

I have a pretty basic problem with button clicks in VB.Net I cannot seem to figure out.
First, I am creating a variable amount of buttons and adding them to the parent form.
Private Sub CreateUIObjects()
For i As Integer = 1 To NumberOfButtons
Dim button As Button = New Button()
Me.Controls.Add(button)
Next
End Sub
I know it is possible to handle a fixed amount of buttons clicked with the following code
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button2.Click, Button3.Click '... And so on
Dim b As Button = CType(sender, Button)
End Sub
But what do I do with not 3, but a variable amount of buttons?
Some code to experiment with
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateUIObjects(3)
End Sub
Dim myButtonNames As String = "foobar"
Private Sub myButtonsClick(sender As Object, e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Debug.WriteLine(b.Name)
End Sub
Private Sub CreateUIObjects(NumberOfButtons As Integer)
Static ct As Integer = 0
For i As Integer = 1 To NumberOfButtons
ct += 1
Dim btn As Button = New Button()
btn.Name = myButtonNames & ct.ToString
btn.Text = btn.Name
btn.Location = New Point(ct * 20, ct * 20)
AddHandler btn.Click, AddressOf myButtonsClick
Me.Controls.Add(btn)
Next
End Sub
You can use something like this
AddHandler Button1.Click, AddressOf Button_Click
take a look http://msdn.microsoft.com/en-us/library/ms172877.aspx

PictureBox MouseEnter / MouseLeave Events not Firing

Create a new form with three picture boxes. This code is intended to draw a border when the mouse enters the picture box and remove it when it leaves. It is inconsistent in the results. Sometimes it draws/removes the border, sometimes it doesn't. This code is not complex. Using VS 2012.
Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseEnter, PictureBox2.MouseEnter, PictureBox3.MouseEnter
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.FixedSingle
' Debug.WriteLine("E " & pb.Name)
End Sub
Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs) _
Handles PictureBox1.MouseLeave, PictureBox2.MouseLeave, PictureBox3.MouseLeave
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BorderStyle = BorderStyle.None
' Debug.WriteLine("X " & pb.Name)
End Sub
I could also reproduce the issue. So, expanding on the comments above about "drawing something else" instead of using the Picturebox's property, let me suggest this quick and dirty approach:
Use a RectangleShape object, the one provided by the VisualBasic Powerpack 3.0 addon. Simply put one of those in the same form your PictureBox is in, and make it invisible (visible = false).
The code is also easy:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.RectangleShape1.Location = New Point(Me.PictureBox1.Left - 1, Me.PictureBox1.Top - 1)
Me.RectangleShape1.Size = New Size(Me.PictureBox1.Width + 1, Me.PictureBox1.Height + 1)
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
Me.RectangleShape1.Visible = True
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
Me.RectangleShape1.Visible = False
End Sub
End Class
Need some help from your Form MouseEnter Event ..
Dim pb As PictureBox = New PictureBox
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
pb.BorderStyle = BorderStyle.None
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
pb = PictureBox1
pb.BorderStyle = BorderStyle.FixedSingle
End Sub
I followed KalaNag idea by putting my picturebox inside a panel and handled the event on the pciturebox by doing like this
private void PictureBox_MouseEnter(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 20;
(control.Parent as Panel).Height = 20;
(control.Parent as Panel).BorderStyle = BorderStyle.Fixed3D;
}
private void PictureBox_MouseLeave(object sender, EventArgs e)
{
PictureBox control = sender as PictureBox;
(control.Parent as Panel).Width = 18;
(control.Parent as Panel).Height = 18;
(control.Parent as Panel).BorderStyle = BorderStyle.None;
}
I changed the control's size because otherwise, the picturebox keeps flickering when the mouse hovers the borders as the cursor is entering and leaving indefinitely since the borders change the size of the control.
Works like a charm !