How do I check if my mouse cursor enters any component in a panel .
I stared to write this code to check when the mouse enters any check box in my panel then I realised that I had no idea how to actually check if the mouse enters the components I the panel .
Private Sub GenCheck()
For Each CheckBox In datapanel1.Controls
Next
End Sub
how do i go about doing this ?
Edit
I have an Idea but i'm not too sure about it
I could say
Private Sub GenCheck()
Dim cb As CheckBox
For Each cb In datapanel1.Controls
AddHandler cb.MouseEnter, AddressOf cb_MouseEnter
AddHandler cb.MouseLeave, AddressOf cb_MouseLeave
Next
End Sub
Private Sub cb_MouseEnter(sender As Object, e As EventArgs)
End Sub
Private Sub cb_MouseLeave(sender As Object, e As EventArgs)
End Sub
You can use MouseHover to determine when the cursor hovers above the checkbox like this.
Private Sub CheckBox1_MouseHover(sender As Object, e As System.EventArgs) Handles CheckBox1.MouseHover
MsgBox("Mouse over!")
End Sub`
Edit:
I have put a panel on a form with two checkboxes to mimic your requirements, here is what you're looking for:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each pnlCheckBox As CheckBox In Panel1.Controls
AddHandler pnlCheckBox.MouseHover, AddressOf Me.CheckBoxMouseOver
Next
End Sub
Private Sub CheckBoxMouseOver()
MsgBox("Mouse over!")
End Sub
*PERFECT WAY TO ACCOMPLISH THIS *
Dim con As Control
For Each con In datapanel1.Controls
AddHandler con.MouseEnter, AddressOf con_MouseEnter
AddHandler con.MouseLeave, AddressOf con_MouseLeave
Next
End Sub
Private Sub con_MouseEnter(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub
Private Sub con_MouseLeave(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub
Related
I want to disable the mousewheel to prevent scrolling in the ComboBoxes.
For one ComboBox this works:
Private Sub CmbDienst_MouseWheel(sender As Object, e As MouseEventArgs) Handles CmbDienst.MouseWheel
Dim HMEA As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
HMEA.Handled = True
End Sub
But how can I add this to ALL ComboBoxes? There are a lot of them in the form.
I was looking for something like
Private Sub Combo_Mouse()
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'And then...?
Next
End Sub
Thanks!
It works. The problem I had was that the comboboxes are in several containers, such as Panels and Datagridviews. Then is "me.controls, etc" not enough.
So I finally made this out of it:
In the Form load:
EnumControls(Me)
In the Programm:
Private Sub ComboBoxes_MouseWheel(sender As Object, e As MouseEventArgs)
Dim hmea = DirectCast(e, HandledMouseEventArgs)
hmea.Handled = True
End Sub
Private Sub EnumControls(ByVal ctl As Control)
If ctl.HasChildren Then
For Each c As Control In ctl.Controls
For Each comboBox In c.Controls.OfType(Of ComboBox)()
AddHandler comboBox.MouseWheel, AddressOf ComboBoxes_MouseWheel
Next
EnumControls(c)
Next
End If
End Sub
It works. Suggestions are welcome!
Try this
Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
and in FormLoad
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'You cann acces to ComboBox her by c
AddHandler c.MouseWheel, AddressOf buttonHandler
Next
End Sub
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
Here is my code that does not work. What am I doing wrong?
You're looping over all the Controls contained in Me (probably the Form)
In that collection there is more than just the PictureBoxes so you need to filter to only get those :
(see OfType)
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
I was wondering is there was a way to use an array when declaring a sub.
Example:
Public Class Form1
Private PictureBox(2) as PictureBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox(0) = PictureBox1
PictureBox(1) = PictureBox2
PictureBox(2) = PictureBox3
Private Sub PictureBox_mouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox(1).MouseDown
'do stuff here
end sub
This is the method Plutonix suggested in the comments applied to your code. Note that no array is needed.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is PictureBox Then
AddHandler DirectCast(ctrl, PictureBox).MouseDown, AddressOf PictureBox_MouseDown
End If
Next
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
'Code here will apply to every PictureBox in your form
End Sub
End Class
Also note that instead of DirectCast(ctrl, PictureBox).MouseDown, you could just use ctrl.MouseDown. Using DirectCast simply helps you avoid making mistakes by cluing IntelliSense and the compiler in on what you're doing.
I've created a button, but can't get it to run the msgbox("click") when I click.. What am I doing wrong?
Thanks.
Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With mybut
mybut.AutoSize = True
mybut.Name = "delete-btn" & btn_number
mybut.Location = New System.Drawing.Point(500, 20)
mybut.Text = "Delete"
End With
End Sub
Private Sub mybut_Click(sender As Object, e As EventArgs)
MsgBox("Click")
End Sub
You need a Handles for your button click
Private Sub mybut_Click(sender As Object, e As EventArgs) Handles mybut.Click
MsgBox("Click")
End Sub
If it is a dynamic button you need to add an event handler
AddHandler mybut.Click, AddressOf Me.mybut_Click
I have created a User Control and would like to be able to detect when the user clicks on the Form.
I have seen this question which is related but the suggestion to use the the Leave event doesn't always do what I want because the focus doesn't necessarily change when the user clicks the Form (my control could be the only control on the Form in which case focus stays with my control).
Any ideas?
I want to be able to do something like this from within the User Control:
Private Sub ParentForm_Click(sender As Object, e As System.EventArgs) _
Handles Me.Parent.Click
End Sub
I would do it slightly differently:
Private _form As Control
Private Sub UserControl_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged
If _form IsNot Nothing Then
RemoveHandler _form.Click, AddressOf ParentOnClick
End If
_form = Me.FindForm()
If _form IsNot Nothing Then
AddHandler _form.Click, AddressOf ParentOnClick
End If
End Sub
Private Sub ParentOnClick(ByVal sender As Object, ByVal e As EventArgs)
'...
End Sub
This gives it a little more resillience - if it is not a direct child of a Form, if its parent changes etc.
I figured out how to do this myself - for anyone interested I am doing the following:
Private _parentForm As Form
Private Sub UserControl_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
_parentForm = CType(Parent, Form)
AddHandler _parentForm.Click, AddressOf ParentForm_Click
End Sub
Private Sub ParentForm_Click(sender As Object, e As System.EventArgs)
debug.writeline("Parent form clicked")
End Sub