Here, I have two side-by-side panel the first_pnl and second_pnl, the second panel is not visible by default. Initial thoughts of what I need:
If my cursor is over the the first one (MouseEnter)
It's BackColor would change to Black
Second panel would be visible
If my cursor leaves first one (MouseLeave)
It's BackColor would change back to Gray
Second panel would not be visible
Which is simple as:
Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
Handles first_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
Handles first_pnl.MouseLeave
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
But what I want to happen is:
When the cursor moves to the second panel (which by now is visible)
second_pnl would remain visible unless the cursor leave its area.
It sustains first_pnl property as if it where on a MouseEnter event
And here's the scenario to be clear:
And here's my logic of making that possible: (Giving their same events with same code)
Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
Handles first_pnl.MouseEnter, second_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
Handles first_pnl.MouseLeave, second_pnl.MouseLeave
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
Looks reasonable, but I think the system consider first the MouseLeave of first_pnl before it even consider the MouseEnter of second_pnl.
Any way to do it?
jmcilhinney's comment solves this easily.
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
Private Sub first_pnl_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles first_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Related
I created a mouse position program that can be used to save your mouse position {X, Y}
I realised that this is not going to be effective unless I implement a method where for example pressing "5" will save that position
The only way i can save the position is by pressing the button, although that does work, there is no way to save the position without clicking the btn.
Can anyone help? I would be very grateful
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub XYbtn_Click(sender As Object, e As EventArgs) Handles XYbtn.Click
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
TimeCo.Start()
End Sub
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
LabelX.Text = "X"
LabelY.Text = "Y"
X2.Text = "X2"
Y2.Text = "Y2"
End Sub
Private Sub TimeCo_Tick(sender As Object, e As EventArgs) Handles TimeCo.Tick
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
End Sub
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
LabelX.Text = Cursor.Position.X
LabelY.Text = Cursor.Position.Y
End Sub
Private Sub save2_Click(sender As Object, e As EventArgs) Handles save2.Click
X2.Text = Cursor.Position.X
Y2.Text = Cursor.Position.Y
End Sub
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
End Sub
End Class
If your form will have focus, you can set the AcceptButton property of the FORM to saveBtn. This will make it so that when you press ENTER on the keyboard while your form has focus then that button will be pressed.
If you'd rather use the key approach then set the KeyPreview property of the Form to True and handle the KeyPress event:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "5" Then
Console.WriteLine("5")
End If
End Sub
Hi I want to show a Label for hint so if mouse hover then show Label and mouse leave then hide Label.
But if mouse click then show label and don't execute leave event, because leave event means hide mouse. So how can I perform it? My code is here.
Click Event
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
control("set")
End Sub
Hover Event
Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
control("show")
End Sub
Leave Event
Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
control("remove")
End Sub
Control Sub
Public Sub control(ByVal c As String)
If c = "set" Then
Label3.Visible = True
ElseIf c = "show" Then
Label3.Visible = True
ElseIf c = "remove" Then
Label3.Visible = False
End If
End Sub
You can remove the EventHandler when Label2 is clicked:
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub
Not sure what exaclty is the purpose of the control-method...but the code could be reduced to this:
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub
Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
Label3.Visible = True
End Sub
Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
Label3.Visible = False
End Sub
Guys, say I have 3 labels named "lblTest1", "lblTest2", and "lblTest3". All labels have same backcolor, which is black.
lblTest1.BackColor = Color.Black
lblTest2.BackColor = Color.Black
lblTest3.BackColor = Color.Black
Now I want to change the backcolor of the labels to red when clicked so I need 3 different procedures for each event.
Private Sub lblTest1_Click(sender As Object, e As EventArgs) Handles lblTest1.Click
lblTest1.BackColor = Color.Red
End Sub
Private Sub lblTest2_Click(sender As Object, e As EventArgs) Handles lblTest2.Click
lblTest2.BackColor = Color.Red
End Sub
Private Sub lblTest3_Click(sender As Object, e As EventArgs) Handles lblTest3.Click
lblTest3.BackColor = Color.Red
End Sub
What I'm trying to do is to create only one subfunction that change the backcolor of whatever label that clicked to colors.red. Is it possible to do this? Can you explain it to me, please? Because currently I'm working with at least 300 labels right now, and not only change its color but other actions as well, so the solutions for this will be so much helpful. :)
You can handle multiple events in the same sub, like this:
Private Sub label_Click(sender As Object, e As EventArgs) Handles
lblTest1.Click, lblTest2.Click, lblTest3.Click
To get the label that was clicked, use the sender passed to the sub:
Private Sub label_Click(sender As Object, e As EventArgs) Handles
lblTest1.Click, lblTest2.Click, lblTest3.Click
cType(sender, Label).BackColor = Color.Red
End Sub
Same sub can handle events for multiple controls, (Labels) in this case.
So proceed like below:
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Label1.Click, Label2.Click, Label3.Click ...
Dim myLabel = DirectCast(sender, Label)
myLabel.ForeColor = Color.Red
End Sub
So I make a form like this
Form1
The picture box will show neutral.png when form is loaded, so
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub
I make the image on picturebox1 change to x.png while mouse over and disappear when mouse leave, so
Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub
My question is how I make the x.png image stay on the picturebox1 when I click the picturebox1. Doing this
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub
Doesn't seem work since the mouseleave event still have an effect.
There is example, from my comment
Public Class Form1
Private isClicked As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("images\neutral.jpg")
End Sub
Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
If isClicked = False Then isClicked = True Else isClicked = False 'if You click again, everything back
'isClicked = True 'in this case, when You click "x.jpg" will stay always
PictureBox1.Image = Image.FromFile("images\x.jpg")
End Sub
Private Sub PictureBox1_MouseEnter(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
If isClicked = False Then 'picturebox isn't clicked, so show x.jpg ... otherwise x.jpg will be showed always
PictureBox1.Image = Image.FromFile("images\x.jpg")
End If
End Sub
Private Sub PictureBox1_MouseLeave(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseLeave
If isClicked = False Then 'picturebox isn't clicked, so show neutral image ... otherwise x.jpg will be showed always
PictureBox1.Image = Image.FromFile("images\neutral.jpg")
End If
End Sub
End Class
Use some variable, in this example is isClicked.
Always make checking under MouseEnter and MouseLeave is picturebox clicked or not.
By this example, You can click again on picturebox to, let's say, reset that isClicked, so You can have again neutral and x images.
I added a Button (name: btn_exit) with the action: If the cursor leaves the button it should turn red, just as a signal. In my application that doesn't work like i thought. The Console says there no errors. So what do I to adjust to change the color ?
My Code:
Private Sub btn_exit_Leave(sender As Object, e As EventArgs) Handles btn_exit.Leave
btn_exit.BackColor = Color.Red
End Sub
You can not do this with the 'Leave' event, you have to use the MouseEnter, MouseHover, MouseLeave events.
Example Code (just tested): The button is "normal" until you move the mouse first time over it, then turns to blue and when you leave the mouse then red Background.
Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter
Button1.BackColor = Color.Blue
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
Button1.BackColor = Color.Red
End Sub
Try this:
Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter
Button1.BackColor = Color.Blue
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
Button1.BackColor = Color.Red
End Sub