Show on hover issue - vb.net

So I have a simple form with a button on it. On the form's MouseEnter event I am setting the button to visible. On the form's MouseLeave event I'm setting the button to hidden. In effect, only when you hover over the form should the button be seen. The problem is that when you put the cursor over the button it disappears. Even if the button is directly in the center of the form it still exhibits this same behavior.
Is there a solution other than putting MouseEnter/Exit events on the button and everything else inside the form?
Public Class VerticalStrip
Private Sub VerticalStrip_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
btnAdd.Visible = False
End Sub
Private Sub VerticalStrip_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
btnAdd.Visible = True
End Sub
Private Sub VerticalStrip_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
btnAdd.Visible = False
End Sub
End Class

Yes; in the MouseLeave event, first check if the mouse has in fact left the form:
Public Class VerticalStrip
Private Sub VerticalStrip_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
btnAdd.Hide()
End Sub
Private Sub VerticalStrip_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
btnAdd.Show()
End Sub
Private Sub VerticalStrip_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
If Not Me.ClientRectangle.Contains(Me.PointToClient(Windows.Forms.Cursor.Position)) Then
btnAdd.Hide()
End If
End Sub
End Class

Related

How can I use the VB.NET Key press

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

Button Visible like relay

hi can you help me how to put 6 button in in a vb form but 1 private sub or whatever you need and make it hide and show each other when you move your mouse on it it is possible ? example when i point my mouse on button 1 the button 2 appear then when i point on button 2 the 3rd button appear and so on the other buttons
im new at vb.net
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseMove,
Button2.MouseMove, Button3.MouseMove, Button4.MouseMove, Button5.MouseMove, Button6.MouseMove
Button1.Hide()
Button2.Show()
Button2.Hide()
Button3.Show()
Button3.Hide()
Button4.Show()
Button4.Hide()
Button5.Show()
Button5.Hide()
Button6.Show()
Button6.Hide()
Button1.Show()
End Sub
End Class
In the form load
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Button1.Show()
Button2.Hide()
Button3.Hide()
Button4.Hide()
Button5.Hide()
Button6.Hide()
End Sub
Then handle button mouse move event in one function call for all buttons as shown below. and write logic as shown below.
I wrote it for 3 buttons. You can plan for rest.
SCROLL below code to left see all button mouse move events handled.
Private Sub Button_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove, Button2.MouseMove, Button3.MouseMove, Button4.MouseMove, Button5.MouseMove, Button6.MouseMove
If (Button1.Visible) Then
Button1.Hide()
Button2.Show()
Exit Sub
End If
If (Button2.Visible) Then
Button2.Hide()
Button3.Show()
Exit Sub
End If
End Sub

Mouse Over picture on VB and make it stay when clicked

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.

Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling Show

I need to show multiple labels to another form then it gives me an error here is my codes i guess I must handle the forms visibility to false thanks in advance
Public Class Form2
Public lbl As New Label
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbl)
Me.Visible = False
End Sub
End Class
for loading form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm2.Visible = True
If frm2 IsNot Nothing Then
frm2.Show(Me) 'Show Second Form
End If
End Sub

Changing the background color for Form2

i have two RadioButtons one for Light Blue and the other for Ghost White and a button to show the next Form (Form2)
i want to be able to check on a Radio Button and the backcolor of form2 changes to the checked Radio Button
this is what i have on my coding so far
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
Private Sub rbLightBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbLightBlue.CheckedChanged
If rbLightBlue.Checked Then
SecondForm.BackColor = (Color.LightBlue)
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGhostWhite.CheckedChanged
If rbGhostWhite.Checked Then
SecondForm.BackColor = (Color.GhostWhite)
End If
End Sub
The problem am having is making the background color change on Form2.
Any answer for this question will be very helpful.
I am not sure what you are doing, it probably has to do with how you are creating your SecondForm, this code does work, see if it helps you narrow it down.
Public Class Form1
Dim SecondForm As Form2 = New Form2
Private Sub rbLightBlue_CheckedChanged(sender As Object, e As EventArgs) Handles rbLightBlue.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.LightBlue
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(sender As Object, e As EventArgs) Handles rbGhostWhite.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.GhostWhite
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
End Class