MouseMove in a panel containing a Label - vb.net

I would like to move a panel in reaction to the MouseMove Event, but this event isn't triggered if the mouse is over the label inside the panel. (It is triggered if the mouse is inside the panel but outside of the label). Is there a simple way to fix this ?
I'm using Visual Basic 2010.

You could make your own class derive from the Panel and use this http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousemove.aspx
Protected Overridable Sub OnMouseMove ( e As MouseEventArgs )
this will give you access to all mouse move events over the panel

Found a way to do that :
Private Sub MyControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
'Something it does when clicked
End Sub
Private Sub MyControl_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
AddHandler e.Control.Click, AddressOf MyControl_Click
AddHandler e.Control.ControlAdded, AddressOf MyControl_ControlAdded
End Sub
I just have to do the same with MouseOver.

Related

How to utilize the button click event from all buttons to call a function?

Let me explain further. Let's say I have 20 buttons on a form, and in the all of the button's click event I want to call a specific function, instead of placing the calling to that function in each click event, is there a way to call it from any of the click events without having to place the code in each click event?
Hope that makes sense.
Yes, just add the click event for each button to the end of the Handles clause.
Private Sub My_Sub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
' do stuff
End Sub
If you have a lot of buttons or a dynamic number of buttons, you could use a recursive method to add the same event handler for each. This will take care of all buttons, even those inside group boxes or other containers.
First, create the method you want each button click to call.
Private Sub bt_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'
' Your code goes here
'
End Sub
Second, create a method to recursively find all the button controls and add the event handler.
Private Sub AddEventHandler(RootControl As Control)
For Each c As Control In RootControl
If c.HasChildren Then
AddEventHandler(c)
End If
If TypeOf c Is Button Then
AddHandler c.Click, AddressOf bt_ButtonClick
End If
Next
End Sub
Lastly, in your form load event, add this line:
AddEventHandler(Me)
Wire the one sub to the click event for every button.
Private Sub AnyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
End Sub

How do i pass keyUp/keyDown events to panel in visual basic?

I have created a game in visual basic, and have decided to expand by adding multiple rooms on the game. Each room will be a panel that shows when the user enters a certain area. To start, I created a class that inherits panel properties, then added it to my main Form. I then added all the items to the panel controls. My issue is that i cannot control the player when it is in the panel controls. I have tried all sorts, but couldn't get it to work.
I did some reading and found that panel does not come with keyUp() and keyDown() events. However, I would like to know if there is a way round this.
Thanks in advance!
Code of mainRoom() class (my panel):
Public Class mainRoom
Inherits Panel
Sub New()
With Me
.SetBounds(0, 0, Form1.Width, Form1.Height)
.SendToBack()
End With
End Sub
Public Sub mainRoom_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
e.Graphics.FillRectangle(Brushes.DarkOrange, block)
End Sub
End Class
In main form class:
Sub setMap()
main = New mainRoom()
Me.Controls.Add(main)
End Sub
You can set the KeyPreview=True on the main form, and then handle the key up/down events from the form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
'key down code.
e.Handled = True
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
'key up code.
e.Handled = True
End Sub
Note: if you don't want other controls' key up/down event handlers to fire after your form event handlers do, then set e.Handled=True as I have in the above example.
See MSDN for more info on KeyPreview.
BEWARE - when moving (cut/paste) existing controls via the IDE from a form into a new panel control, the event handlers are not carried across as they become children of the parent control (the new panel). So this happens:
Private Sub Button1_Click(sender As Object, e As EventArgs)
Add the event handlers back and don't spend an age googling why keypress events suddenly stop firing in panels ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

button click not firing it's method VB.NET

Hello I'm having a problem with my button. When I click it, the button's not firing the method:
Private Sub button1_Click(sender As System.Object, e As System.EventArgs)
'Initialize the capture device
grabber = New Capture()
grabber.QueryFrame()
'Initialize the FrameGraber event
AddHandler Application.Idle, New EventHandler(AddressOf FrameGrabber)
button1.Enabled = False
End Sub
What am I missing in here?
There should be something like
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
or
AddHandler button1.Click, AddressOf button1_Click
I suppose its vb.net and winforms. With VB6 or WPF is a solution little bit different.
Go to the Forms designer.
Select the Button.
In the Properties Pane, bottom right by default, click the "Lightning Bolt" Icon.
Find the "Click" entry.
Click the small dropdown arrow to the right and select "button1_click".
The handler should now be again associated with your buttons click event.
Change the declaration like this:
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Mousewheel Event

I have got as form with a vertical scroll bar, I want to use the mouse wheel to scroll up and down the form but I cant seem to get to get the mousewheel event to fire.
I just use the standard mousewheel event
Private Sub frmTest_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
'code here
End Sub
Any help would be appreciated.
Try to place a one Panel in your Form set to AutoScroll = true, Dock = Fill and from the Panel place all your control like TextBox, label, listView etc..
Private Sub Panel1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles Panel1.MouseEnter
Panel1.Focus()
End Sub
Private Sub Panel1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseWheel
MessageBox.Show("okay")
End Sub

redirect an event in VB.NET

I havea a UserControl1 (in witch I have an Label1) in Form1. I want to catch the MouseDown event from Label and send it like it was from UserControl.
I do:
Public Class UserControl1
Shadows Custom Event MouseDown As MouseEventHandler
AddHandler(ByVal value As MouseEventHandler)
AddHandler Label1.MouseDown, value
End AddHandler
RemoveHandler(ByVal value As MouseEventHandler)
RemoveHandler Label1.MouseDown, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As MouseEventArgs)
'RaiseMouseEvent(Me, e) ??? '
End RaiseEvent
End Event
End Class
However, when I set in the Form1 the UserControl
Private Sub UserControl11_MouseDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles UserControl11.MouseDown
' here I have "Label", BUT want "UserControl" '
MessageBox.Show(sender.GetType.Name)
End Sub
One detail.. I want that the event should be only on the label, not on the whole userControl.
Why don’t you just handle the event “old school” and delegate it, instead of creating a custom event? Like so:
' In the user control: '
Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) _
Handles Label1.MouseDown
OnMouseDown(e)
End Sub
Now when you handle the UserControl.MouseDown event in your form, the sender of the event will be the user control instance.
If you only want to capture clicks on the label (instead of on the whole user control) then you can override OnMouseDown to test from where the click originates:
Private m_MouseDownFromLabel As Boolean = False
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles Label1.MouseDown
m_MouseDownFromLabel = True
OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
If m_MouseDownFromLabel Then
m_MouseDownFromLabel = False
MyBase.OnMouseDown(e)
End If
End Sub
This should be safe in the face of race conditions since there’s only one UI thread.
By the way: RaiseMouseEvent cannot be used here since the first argument would have be the MouseDown event property. However, this property cannot be accessed from derived classes, only inside the Control class itself. I don’t know why RaiseMouseEvent isn’t private itself, instead of being protected, when it can’t be used from derived classes anyway.