button release after mouse hovering out - vb.net

I have this six button
After i press one button, for example i press settings it will open an new windows after i close that windows the button remain pressed.
Here are the settings button code:
Private Sub btn_SETTINGS_MouseEnter(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseEnter
btn_SETTINGS.ForeColor = Color.White
End Sub
Private Sub btn_SETTINGS_MouseLeave(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseLeave
btn_SETTINGS.ForeColor = SystemColors.HotTrack
End Sub
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
End Sub
Any suggestion what can i do to fix this problem.

Okay, this problem seems related to Focus that got set to the button when you pressed it. Very easy and quick fix of this problem is, change the focus of control to some other control. On the same side, add one label control which is of Hidden type say lblHidden. So when you're doing following code then change focus.
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
Me.ActiveControl = lblHidden
End Sub
This will change the focus to hidden control. However if you are doing any formatting change of button on click event then revert it back to original in above even.t

Related

VB:mouse click not work

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

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

How do I stop a button from triggering when the enter key is pressed? (focused)

(I would say hello here, but Stackoverflow always removes it for some reason)
My problem is that on my Visual Basic form the button that I use to close the form can be triggered by the enter key. This is a problem because my form opens a webpage that frequently uses forms, meaning that the enter key on a form will close the window!
Therefore, I need to find a way of stopping the focus on this button. It is a custom-ish button and is its own class but inherits from a normal button. Here's some diagrams:
Stage 1: Before the user focuses on the WebKitBrowser
Stage 2: After the user focuses on the WebKitBrowser
As you can see, the button is "focused" with a white border, similar to a normal Windows button where the border is instead blue. If I was to press enter at this stage, the form would close.
In conclusion, or TL;DR, how do I stop my close button from being focused?
Your button by default is included in the tab order which results in the focus effect.
Try removing the button from the tab order. You can do by setting the TabStop of your button to False when your form loads. I tried the example below and it worked for me.
Before
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
After
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.TabStop = False
End Sub
End Class

button click not firing it's method VB.NET

Hello I'm having a problem with my button. When I click it, the button's not firing the method:
Private Sub button1_Click(sender As System.Object, e As System.EventArgs)
'Initialize the capture device
grabber = New Capture()
grabber.QueryFrame()
'Initialize the FrameGraber event
AddHandler Application.Idle, New EventHandler(AddressOf FrameGrabber)
button1.Enabled = False
End Sub
What am I missing in here?
There should be something like
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
or
AddHandler button1.Click, AddressOf button1_Click
I suppose its vb.net and winforms. With VB6 or WPF is a solution little bit different.
Go to the Forms designer.
Select the Button.
In the Properties Pane, bottom right by default, click the "Lightning Bolt" Icon.
Find the "Click" entry.
Click the small dropdown arrow to the right and select "button1_click".
The handler should now be again associated with your buttons click event.
Change the declaration like this:
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Mousewheel Event

I have got as form with a vertical scroll bar, I want to use the mouse wheel to scroll up and down the form but I cant seem to get to get the mousewheel event to fire.
I just use the standard mousewheel event
Private Sub frmTest_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
'code here
End Sub
Any help would be appreciated.
Try to place a one Panel in your Form set to AutoScroll = true, Dock = Fill and from the Panel place all your control like TextBox, label, listView etc..
Private Sub Panel1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles Panel1.MouseEnter
Panel1.Focus()
End Sub
Private Sub Panel1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseWheel
MessageBox.Show("okay")
End Sub