Catch/Handle Keydown Event From All Control Inside Groupbox - vb.net

Is there any way to catch all keydown event from any controls like textboxes inside a groupbox using one event handler.?
I try groupbox_keydown, but it's not triggered. I try to make some research in msdn and they say groupbox cannot get focus, so the keydown event will never raised. Is there any workaround for it.?
KeyPreview already set to True.
Edit:
i have some textboxes inside a groupbox. most of them have default or predefined value. what i want is when user have filled the required data and want to skip some textboxes, he just need to press some key like ctrl+enter to excecuting a click on a button in that groupbox or calling a procedure.

This seems to work ok It iterates through the controls collection in the groupbox and adds the .KeyDown event to the example handler.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctl As Control In GroupBox1.Controls
AddHandler ctl.KeyDown, AddressOf GroupBox1Controls_KeyDown
Next
End Sub
Private Sub GroupBox1Controls_KeyDown(sender As Object, e As KeyEventArgs)
MessageBox.Show(DirectCast(sender, Control).Name)
End Sub
It may be that there are controls that don't have a .KeyDown event, so you might want to add an empty Try..Catch..End Try block into the code like this, but I wouldn't recommend it as it could hide other problems with the code.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctl As Control In GroupBox1.Controls
Try
AddHandler ctl.KeyDown, AddressOf GroupBox1Controls_KeyDown
Catch
End Try
Next
End Sub

Related

ResizeEnd event on pressing Maximize button

I am trying to show some hidden columns in a telerik RadGridView when the Form Window is Maximized.
I am aware of Resize Event but since Resize event is fired even before the Form is loaded, I am not using it.
I found that ResizeEnd suits my need but it is not reacting to Maximize button.
Is it possible to fire it with Maximize button.
Thanks.
since Resize event is fired even before the Form is loaded
Then add the handler in FormLoad instead of the designer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Control.ResizeBegin, AddressOf controlResizeBegin
End Sub
Private Sub controlResizeBegin(sender As Object, e As EventArgs)
End Sub

VB.NET Button with both MouseMove and Click/MouseClick handlers

I'm attempting to to add both a MouseMove and a Click (or MouseClick) to a button. The end user needs the ability to click the button to add an element, but also the ability to drag the button to a specific location to add an element as well. I have both functions working independently. However, when I add both Handlers, the MouseMove takes precedence, and doesn't allow the Click to happen (attempting to click shows the DragDropEffects.None effect).
Button:
Dim Button As New Button()
'Button Attributes
AddHandler Button.Click, AddressOf Button_Click
AddHandler Button.MouseMove, AddressOf Button_MouseMove
Handlers:
Private Sub Button_MouseMove(sender As Object, e As MouseEventArgs)
Button.DoDragDrop("Button", DragDropEffects.Move)
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs)
'Add Element Function
End Sub
I've also tried it with the MouseClick handler, with the same results:
AddHandler Button.MouseClick, AddressOf Button_MouseClick
Private Sub Button_MouseClick(sender As Object, e As MouseEventArgs)
'Add Element Function
End Sub
1.) What is the reasoning behind the Click/MouseClick not able to fire when a MouseMove is implemented as well?
2.) What is the proper workaround to get both functions usable? Do I need to do MouseDown and MouseUp handlers, and track the distance that the mouse has moved to determine if I clicked or if I am dragging?

Labels' Click events

I have an application where I have around 50 labels. In those labels a number is visible.
When the user clicks on the label the number needs to be written to an edit box.
This works fine, the only problem is that I have added 50 functions like below, and every time it’s the same. I was wondering if there is a common function for this
Remark: The labels have different names. So if its possible that this will work for all the labels on the form.
Private Sub LI_L_Click(sender As Object, e As EventArgs) Handles LI_L.Click
cmbOBJID.Text = LI_L.Text
End Sub
In the form designer, you should be able to set the handler for every label to the same function. Then you can use the "sender" parameter to determine which label is raising the event.
Notice also how all the controls that the function is linked to are listed after the "Handles" keyword. This is another way you could connect the code to all the labels if you prefer this over using the Visual Studio UI properties grid.
Private Sub LI_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
cmdOBJID.Text = DirectCast(sender, Label).Text
End Sub
While it is easy to add several events to one handler in the style of
Private Sub LI_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
It will be tedious for more than just a few labels.
You can add handlers programatically if you can find a way to refer to the labels you need to add handlers to. In this example, I put all the labels in a groupbox named "GroupBoxOptions":
Option Infer On
Option Strict On
Public Class Form1
Sub TransferDataToEditBox(sender As Object, e As EventArgs)
Dim lbl = DirectCast(sender, Label)
tbEditThis.Text = lbl.Text
End Sub
Sub InitLabelHandlers()
For Each lbl In GroupBoxOptions.Controls.OfType(Of Label)
AddHandler lbl.Click, AddressOf TransferDataToEditBox
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitLabelHandlers()
End Sub
End Class
You may have some other way of selecting the labels which use the handler.
A pretty nice and quick solution is to traverse all the label controls on a form, assigning through the AddHandler function the event to run when a user clicks a label.
In code:
For Each c As Control In Me.Controls.OfType(Of Label)
AddHandler c.Click, AddressOf myLabelClick
Next
With the prevous snippet, we loop onto all the winform controls of type Label. A loop like that is useful when we have a lot of labels for which an event must be assigned. For each of them, we associate the event Click of the control with a customized Sub named myLabelClick. That subroutine will look like the following:
Private Sub myLabelClick(sender As Object, e As EventArgs)
cmdObjId.Text = DirectCast(sender, Label).Text
End Sub
Here we use the sender variable (which represents the control for which the click has been done) to access its Text property, and change the cmdObjId.Text accordingly.
Just to complement the solution from BlueMonkMN:
If you are using the DevExpress Tools, you need to import DevExpress.XtraEditors and change Label to LabelControl:
DirectCast(sender, LabelControl).Text
This worked for me.

How can I change the CheckChanged when clicking the same object in VB 2010?

I want to know if it is possible in VB 2010 to changed the CheckChanged of a CheckBox when clicking the same object. For Example:
I have a 1 picturebox named pic1 and a checkbox named chck1, If I click the pic1, the chck1 must be checked but If I click again the pic1, chck1 must be unchecked and I click again the pic1, chck1 must be check again and so on..
I really don't have an idea if it is working or impossible in VB 2010, I hope someone can help me. Thank you very much.
If it's a WinForm, then you just have to implement something like this:
you have your PictureBox and your Checkbox, and you just have to add a clickhandler to your picturebox like this:
private void pictureBox1_Click(object sender, EventArgs e)
{
checkBox1.Checked = !checkBox1.Checked;
}
This Method always negates the Checked-State of the Checkbox (it's way simpler than a if/else)
checkbox1.Checked contains the checked-state, so that is how you can uncheck/check it.
Edit: i did it in c#, sorry,
in VB.NET it would be something like
Private Sub pictureBox1_Click(sender As Object, e As EventArgs)
checkBox1.Checked = Not checkBox1.Checked;
End Sub
In a WinForms application just add the event handler for the click event of your PictureBox.
You could that easily with the Form Designer or, if using code, then write
' In the form constructor
Public Sub Form1()
' First initialize your form controls'
InitializeComponent()
' then add the event handler for the picturebox click event'
AddHandler pic1.Click, AddressOf pic1_Click
End Sub
Private Sub pic1_Click(sender As Object, e As EventArgs)
' toogle the checked state of the checkbox'
chk1.Checked = Not chk1.Checked
End Sub
As said below from Mr Neolisk you could also shorten this code simply adding the Handles clause to the pic1_Click event thus removing the code in the Form constructor
Private Sub pic1_Click(sender As Object, e As EventArgs) Handles pic1.Click
' toogle the checked state of the checkbox'
chk1.Checked = Not chk1.Checked
End Sub

Detecting a radiobutton change

I have about 50 radiobuttons on one form and I don't want to create an if statement for each one to detect when one changes, they are not part of a group box. How would I detect if any radiobutton changed and then write the name to another variable?
Put all the radio buttons on a panel and loop through the panels radio button controls, programmatically adding the same event handler for each as described by #Steve. One way I like to handle the event is to assign each radio button an index into its tag property. Then just store any relevant data in a list of objects and access the data for that radio button by pulling out it's corresponding object from the list using its tag. Much easier than doing it by hand.
Edit: Good catch #Neolisk. Updated answer.
You could Always set the CheckedChanged event for all 50 radiobuttons to the same event handler.
This is an example done via code:
Private Sub OnChange(sender As System.Object, e As System.EventArgs)
Dim rb = CType(sender, RadioButton)
Console.WriteLine(rb.Name + " " + rb.Checked.ToString)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddHandler Me.RadioButton1.CheckedChanged, AddressOf OnChange
AddHandler Me.RadioButton2.CheckedChanged, AddressOf OnChange
' and so on .....'
End Sub
I have done this via code and not using the designer to avoid the long add of Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged .......