Mousewheel Event - vb.net

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

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

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()

MouseMove in a panel containing a Label

I would like to move a panel in reaction to the MouseMove Event, but this event isn't triggered if the mouse is over the label inside the panel. (It is triggered if the mouse is inside the panel but outside of the label). Is there a simple way to fix this ?
I'm using Visual Basic 2010.
You could make your own class derive from the Panel and use this http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousemove.aspx
Protected Overridable Sub OnMouseMove ( e As MouseEventArgs )
this will give you access to all mouse move events over the panel
Found a way to do that :
Private Sub MyControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
'Something it does when clicked
End Sub
Private Sub MyControl_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
AddHandler e.Control.Click, AddressOf MyControl_Click
AddHandler e.Control.ControlAdded, AddressOf MyControl_ControlAdded
End Sub
I just have to do the same with MouseOver.