button click not firing it's method VB.NET - 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

Related

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

button release after mouse hovering out

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

Setting focus to a textbox control

If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.
If I want to achieve the same result at run-time, using code, how should I proceed?
Are there alternatives to using tabOrder?
I assume any run-time code will be in the form's constructor or its onload event handler?
EDIT
In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.
Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form
Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Show()
TextBox1.Select()
End Sub
I think what you're looking for is:
textBox1.Select();
in the constructor. (This is in C#. Maybe in VB that would be the same but without the semicolon.)
From http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx :
Focus is a low-level method intended primarily for custom control
authors. Instead, application programmers should use the Select method
or the ActiveControl property for child controls, or the Activate
method for forms.
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TextBox1.Select()
End Sub
Using Focus method
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
yourControl.Focus()
End Sub
To set focus,
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Focus()
End Sub
Set the TabIndex by
Me.TextBox1.TabIndex = 0
Quite simple :
For the tab control, you need to handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
I think the appropriate event handler to use is "Shown".
And you only need to focus the appropriate text box.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
myTextbox.Focus()
End Sub
create a textbox:
<TextBox Name="tb">
..hello..
</TextBox>
focus() ---> it is used to set input focus to the textbox control
tb.focus()

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

How do I go create a button from Formx.vb to Formx.vb [Design]

My question is in fact this: I know how to manipulate the code from a button (by double clicking it) from the design tab but how do I create a button from the code section wich will appear in the design tab? If I simply put the code for a new button it will do nothing:
Private Sub Button1_click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button1.click
If you have the answer please help me.
With great respect for your work, Alex
Private btnAdd As New Button()
Have a look at How to programmatically add controls to Windows Forms
Why don't you want to use the designer?
There is a hidden file named Formx.Designer.vb in your project. If you click the Show All Files button in the Solution Explorer tool pane, you'll see it collapsed in the Formx.vb file.
This file ultimately controls what the designer shows. You can edit it by hand but you risk the problem of creating code that the designer cannot read and when you use the designer after hand-editing the file, it may throw away your changes.
Here is some code that creates a button and hooks the click event. It runs on form.load but you can do it wherever you want.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Button2 As Button
Button2 = New Button()
Button2.Location = New System.Drawing.Point(80, 80)
Button2.Name = "Button2"
Button2.Size = New System.Drawing.Size(75, 23)
Button2.TabIndex = 0
Button2.Text = "Button2"
Button2.UseVisualStyleBackColor = True
AddHandler Button2.Click, AddressOf MyButton_Click
Controls.Add(Button2)
End Sub
Sub MyButton_Click(sender As Object, e As EventArgs)
MsgBox("yes")
End Sub