mouseenter makes button visible but button click event then doesn't fire - vb.net

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)

Related

How to make one-click buttons?

I have several buttons in which all of them have come codes. I want the user to only can make click the buttons once. How can I do it without having a long disable buttons code?
Dim oneClick As Integer
Private Sub btnPro_Click(sender As Object, e As EventArgs) Handles btnPro.Click
oneClick += 1
If oneClick = 1 Then
Dim ucP As New ucPro
fillMenu(ucP)
End If
End Sub
This is the code I come up with.
EDIT : The point is that I want to click a button once, but the others can be clicked. For example, I have a form with 6 buttons, every button has some codes. If I click Button1, it'll do such code only once, therefore if I click it again, it will not do that code, because it already did it. Then, if I click Button2, it'll do the code once. And what about if I click Button1 again? well, it'll be able to do that code because I clicked to another button.
Sorry for the way I explain myself. I hope you get it.
Keep track of last clicked button and you can use one handler for all involved buttons
Private _lastClickedButton As Button = Nothing
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button = DirectCast(sender, Button)
If button Is _lastClickedButton Then Exit Sub
_lastClickedButton = button
' Now based on button instance you can execute corresponding logic/method
End Sub
Based on your rather poor explanation, it sounds like what you actually mean is that you only want each button to act once in a row, i.e. once you click a button you need to click another button before the first button does anything again. In that case, the proper way to handle this is to not use Button controls.
Instead of using Button controls and handling their Click events, you should be using RadioButton controls and handling their CheckedChanged events. You can set the Appearance property to Button and they will look just like regular Buttons. When checked, they will appear depressed and that will indicate to the user that they can't be used again.
Here's an example of a form using such controls:
And here's what the appropriate code might look like:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
'Do something.
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton1.Checked Then
'Do something else.
End If
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton1.Checked Then
'Do yet something else.
End If
End Sub
Here's what the form might look like when a RadioButton has been checked:
As you can see, it's nice and clear to the user which one they cannot use.
EDIT:
If you are really determined to use Button controls then you absolutely should be disabling them as that is the standard way that feedback has been provided to the user for decades. It's very simple to disable a Button if and only if it is the last one clicked. Here's an example:
Private Sub AllButtons_Click(sender As Object, e As EventArgs) Handles Button3.Click,
Button2.Click,
Button1.Click
For Each btn In Controls.OfType(Of Button)
btn.Enabled = (btn IsNot sender)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do something.
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Do something else.
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Do yet something else.
End Sub
Note that you can handle multiple events with a single method and multiple methods can handle a single event, both of which are demonstrated here. All four of those methods were generated automatically by the designer, so I just had to add the body code. Note that the first method assumes that all Button controls on the form will be treated this way. If that's not the case then would need a slight adjustment but nothing too major.

VB.NET hide NotifiyIcon ContextMenuStrip when click somewhere else

My Form include Notify icon with ContexMenu. ContextMenu is automatically closed when click on any item (AutoClose=True). But if I click anywhere else (away from context menu), ContextMenu remains visible. How to hide it in this case?
Found one answer, that hiding context menu when click somewhere else is standard behavior of context menu. In my case it doesnt work :).
I tried to use ContextMenuStrip Leave or LostFocus event to hide but this didnt work.
To show context menu:
Private Sub NotifyIcon1_DoubleClick(sender As Object, e As EventArgs)
Handles NotifyIcon1.DoubleClick
NotifyIcon1.ContextMenuStrip.Show(MousePosition)
End Sub
To hide context menu I tried:
Private Sub ContextMenuStrip_Leave(sender As Object, e As EventArgs)
Handles ContextMenuStrip.Leave
NotifyIcon1.ContextMenuStrip.Hide()
End Sub
Private Sub ContextMenuStrip_LostFocus(sender As Object, e As EventArgs)
Handles ContextMenuStrip.LostFocus
NotifyIcon1.ContextMenuStrip.Hide()
End Sub
UPDATE: This works:
Private Sub ContextMenuStrip_MouseLeave(sender As Object, e As EventArgs)
Handles ContextMenuStrip.MouseLeave
NotifyIcon1.ContextMenuStrip.Hide()
End Sub
Could be used as temp solution, but original question still open.

Get text of self object in vb.net

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

Mimic MouseIn/Out

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

How to stop space bar from clicking button

I'm using VS 2008. In either a Windows or Smart Device project, pressing Space bar key when a button currently has the focus will cause the button to be clicked and it will raise the Click event. Is there any property or setting which will cause it to skip it so that the space bar does not cause a button click?
You can use the Form's KeyPreview property along with the KeyDown event to catch the space bar before it is passed to the button and if the button has focus, then you can mark the key press handled to prevent it from bubbling up:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Button Clicked")
End Sub
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
If e.KeyCode = Keys.Space And Button1.Focused Then
e.Handled = True
End If
End Sub