I am trying to hide my cursor when a user enters a button, and show it when they leave the button.
So far I am using the MouseEnter and MouseLeave Events
Private Sub btnbeis_MouseEnter(sender As System.Object, e As System.EventArgs) Handles btnBeis.MouseEnter
Dim btn As Button = DirectCast(sender, Button)
btn.FlatStyle = FlatStyle.Flat
Cursor.Hide()
End Sub
Private Sub btnbeis_mouseLeave(sender As System.Object, e as system.EventArgs) Handles btnbeis.MouseLeave
Dim btn As Button = DirectCast(sender, Button)
btn.FlatStyle = FlatStyle.Standard
cursor.Show()
End Sub
However because MouseEnter is hit every time the user moves the mouse, it keeps on hiding the mouse
I found the MouseOut event but it does not work for vb, is there anything that mimics mouseout?
Meaning I only want it to hit when the mouse leave the button, and enters for the first time
EDIT
I tried this on a new form and it works perfectly
But on this form it keeps on hitting the mouseEnter and MouseLeave, when I debug it keeps on going from mouseEnter to MouseLeave and back to MouseEnter
I Do Not have any code under mouseMove, so that is not the problem
Thank You!
Sub Button1MouseLeave(sender As Object, e As EventArgs)
Cursor.Show()
End Sub
Sub Button1MouseEnter(sender As Object, e As EventArgs)
Cursor.Hide()
End Sub
Related
I'm very new to coding and currently I am coding a VB.net Windows Form Hangman game. I have 26 letter buttons that when pressed I would like the text of them (A,B,C,Etc.) to be put into a Text box so the player knows what letter they have inputted and they can then submit their guess. However, so far the only way I have figured out how to detect when any of the buttons is pressed is by writing the code individually for each button which looks very messy and inefficient. I was wondering if it was possible to detect when any of the buttons is pressed (And know which one) without writing code for each individual button?
This is only for 4 of the buttons so I have to do this 22 more times for what I want to do and more for any additional buttons:
Current Code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = Button3.Text
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox2.Text = Button4.Text
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox2.Text = Button5.Text
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox2.Text = Button6.Text
End Sub`
This is the game when run. When any of the letter buttons are pressed I would like them to be detected. The text box on the right shows the button that is pressed:(https://hi.stack.imgur.com/92sDy.png)
I hope someone can help,
thank you in advance,
Georgitzu
Here's the basic idea ...
' assign all button click events to the same routine
Private Sub buttonHandler(sender As Object, e As EventArgs) _
Handles Button1.Click, Button1.Click .... Button26.Click
' create a generic button object to handle the button clicked
Dim obtn as Button = CType(sender, Button)
' display the text
TextBox2.Text = obtn.Text
Hope this helps,
Mike
Q- When a 'Mouse Hover action' is used for a button (N.B: the button has a name-Text it shows under properties- we wish to change that name when the mouse hovers over it as well as change the font color of the textfield (textbox) from blue to red). Please can you`ll help me to change the name of the button component and color issue.
Code Attempt: (Visual Basic 10 Express)
Public Class Form1
Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.MouseHover
Button1.Name("About to go Red")
End Sub
End Class
When you click buttton in Properties, there is an icon looking like a lightning bolt which is for events.
Click that icon, and search for the event MouseHover
As I don't know if you actually mean the design name, or the text it shows.
For the text:
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Button1.text = R.Next(0, 10)
End Sub
For the name:
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Button1.Name = R.Next(0, 10)
End Sub
Ofcourse you can edit R.Next to any thing you want.
My codes:
Me.KeyPreview = True
...
Private Sub Form_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right Mouse clicked.")
End If
End Sub
Try to capture mouse right click, but not work.
Any suggestions welcomed. Thanks
As other mention in comments your code seems right, but will only work on plain form. To overcome that you can join events.
Private Sub Form_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick, Button1.MouseClick, Control1.MouseClick, AnyOtherControl.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right Mouse clicked.")
End If
End Sub
Please replace Control names you want your event to fire on in this code:
Handles MyBase.MouseClick, Button1.MouseClick, Control1.MouseClick, AnyOtherControl.MouseClick
I'm guessing you are using any of container controls which fill up most of your form. If you want your event to work with them you need to add them to your event.
Finally there is also matter of DoubleClick which won't fire above event. To overcome it all you need to do is change MouseClick to MouseDown
Let's say I have a button with the text 'A'.
When I click this button, I want a messagebox to appear with the text of what name of the button that was clicked.
I tried setting it to MessageBox.Show(Me.Text) but that just gives me the form name.
How can I refer to the text of button I just clicked?
If you have to handle multiple button click try following method
Private Sub btn_a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_a.Click _
,btn_b.Click 'you can add other buttons click event here(ex. btn_c,btn_d etc)
Dim objButton As Button = DirectCast(sender, Button)
MessageBox.Show(objButton.Text)
End Sub
DirectCast() vs. CType()
The reason why MessageBox.Show(Me.Text) didn't work is that Me refers the class containing the code, in this case, the Form).
If your button's event handler only handles one button, you can just hard code the buttons name like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(Button1.Text)
End Sub
If the event handler can handle more than one button, you can use the sender argument to reference the button being clicked:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim but as Button = CType(sender, Button)
MessageBox.Show(but.Text)
End Sub
I have made a button (btnImageRemove) get shown and hidden based on if the mouse is inside the component, but when i then click that button the event doesnt fire.
Private Sub btnImageRemove_Click(sender As Object, e As EventArgs) Handles btnImageRemove.Click
lblAvatar.Image = Nothing
lblAvatar.Tag = Nothing
End Sub
Private Sub lblAvatar_MouseLeave(sender As Object, e As EventArgs) Handles lblAvatar.MouseLeave
btnImageRemove.Visible = False
End Sub
Private Sub lblAvatar_MouseEnter(sender As Object, e As EventArgs) Handles lblAvatar.MouseEnter
btnImageRemove.Visible = True
btnImageRemove.BringToFront()
End Sub
Private Sub btnImageRemove_MouseClick(sender As Object, e As MouseEventArgs) Handles btnImageRemove.MouseClick
lblAvatar.Image = Nothing
lblAvatar.Tag = Nothing
End Sub
I have tried all the click and mouseclick events on the button, and all components that take its area, but i cannot get an event to fire for that button
Narrowed down cause a little more:
I have narrowed down to if i add anything to show / hide the button at runtime, during another event, the button doens't fire a click event, nor turn orange (default dotnetbar button action)