Define individual events with sub handles - vb.net

I have a sub routine that handles multiple labels being clicked on.
Private Sub Label_click(sender As Object, e As EventArgs) Handles Label1.Click, Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click
checkCards()
End Sub
While this works, the problem is that I want each individual label to have a slightly different action each time, such as
checkCards(labelClicked)
I want each label to be able to be clicked on and call the same checkCards() sub, however with slightly different values of the parentheses. How would I go about this?
I am somewhat new to Visual Basic, so please try to keep the answer basic.

The sender parameter of your event is the clicked Label:
Private Sub Label_click(sender As Object, e As EventArgs) Handles Label1.Click, Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click
checkCards(sender)
End Sub
So if you click on the Label1 the sender parameter contains the Label1 object.
So you can implement the sub checkCards like the following:
Private Sub checkCards(ByVal labelClicked As Label)
Select Case labelClicked.Name
Case Label1.Name
'do some stuff if Label1 was clicked.
Case Label2.Name
'do some stuff if Label2 was clicked.
Case Label3.Name, Label4.Name
'do some stuff if Label3 or Label4 was clicked.
Case Else
'do some stuff if none of the above labels was clicked.
End Select
End Sub

Related

Unselect a listbox item if it contains a specific character (don't allow its selection)

I have a listbox1 with a list of items. If a user has to select an item with a single mouse click and if that selected list item has an equal sign in it, it must immediately display a message stating this and deselect the item.
I have looked at various solutions all of which does not work as I want it to:-
Below is my code which works ummmm most of the time but not when two or more items have already been previously selected. I need a always will work solution.
Private Sub ListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseClick
For I = 0 To ListBox1.SelectedItems.Count - 1
If Not ListBox1.Items(I).ToString.Contains("=") Then
ListBox1.SetSelected(I, False)
' MsgBox("Please only select items that have = in description ! ! ! Edit item if you want to include . . .", MsgBoxStyle.Critical)
End If
Next
ListBox1.Refresh()
End Sub
I think it's better using SelectedIndexChangedEvent:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
With CType(sender, ListBox)
For i As Integer = .SelectedItems.Count - 1 To 0 Step -1
If Not IsNothing(.SelectedItems(i)) AndAlso Not .SelectedItems(i).ToString.Contains("=") Then
MsgBox("Invalid selection.")
.SelectedItems.Remove(.SelectedItems(i))
End If
Next i
End With
End Sub
One solution for this problem is to get - in the SelectedIndexChanged event - the indices of the invalid selections if any to deselect them and show the message. Handling the SelectedIndexChanged event in particular is to make it work with the mouse and keyboard inputs.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles ListBox1.SelectedIndexChanged
With ListBox1
Dim invalidSel = .SelectedIndices.Cast(Of Integer).
Where(Function(i) Not .GetItemText(.Items(i)).Contains("="))
If invalidSel.Any() Then
RemoveHandler ListBox1.SelectedIndexChanged,
AddressOf ListBox1_SelectedIndexChanged
For Each i In invalidSel
.SetSelected(i, False)
Next
MessageBox.Show("Please....")
AddHandler ListBox1.SelectedIndexChanged,
AddressOf ListBox1_SelectedIndexChanged
End If
End With
End Sub
Note, removing then adding the handler again is to avoid showing the message box repeatedly in the multi-selection modes. Without it, the event fires for each .SetSelected(i, False) call.

Triggering an event in VB based on a change in a control in a group box

I have a group box with multiple Checkboxes(food item) and each one has a corresponding NumericUpDown control(quantity). For context, it is for a project based on a restaurant menu. I want to hide a button called btnSave whenever either a checkbox is unchecked or the quantity (NumericUpDown) is changed. I currently have btnSave.Hide under the CheckBox1_CheckedChanged and NumericUpDown1_CheckedChanged SubProcedures but I want to know if there's a way to do this when anything within this group box is changed instead of putting the code under each SubProcedure. Thanks
I think you meant .ValueChanged for the NumericUpDown control. (There is no .CheckedChanged) Although this doesn't matter much in this case, this is a good pattern for future reference. Instead of calling an event call a Sub from your events.
When you have several controls responding to a single Event handler, you can find out which control triggered the event by checking the sender parameter. Since, as you can see, sender is an Object you will have to cast it to the appropriate type to get the properties of a CheckBox.
Private Sub HideSaveButton()
btnSave.Hide
End Sub
Private Sub CheckBoxInGroupBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
HideSaveButton()
Dim WhichCheckBox As CheckBox = DirectCast(sender, CheckBox)
Select Case WhichCheckBox.Name
Case "CheckBox1"
MessageBox.Show("CheckBox1 has changed")
Case "CheckBox2"
MessageBox.Show("CheckBox2 has changed")
End Select
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
HideSaveButton()
End Sub

How to update a form based on a checkbox status

I'm currently trying to code some macros for a software (Revit), and I ran into an issue I do not know how to solve.
So I have a windows form with two checkboxes and a list of elements, and I would like that list of elements to be updated depending on what the status of the checkboxes is.
Here's my checkbox status' code:
If StructcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.EngineeringPlan
vpList.Add(tmpVP)
End Select
End If
If LegcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.Legend
vpList.Add(tmpVP)
End Select
End If
Now the problem with that code is that it only checks the initial status of the checkboxes, but do not update the list when the checkboxes are checked/unchecked.
How to make it so that the list VpList is updated everytime a checkbox status is changed?
Thanks!
The key here is to add a sub that does the checkboxes checking. You will need that sub because it will called multiple times depending on the user's actions.
Because you are not providing any insights about tmpView and vpList I will stick with your code, however you should note that depending on what exactly are you trying to do your code can be simplified or rewritten to be a bit more efficient. For example you don't specify if you want the tmpVP value to be unique in the list or to be more than one times, I assume that you want to be unique, so here is the sub's code (please read the comments in the code):
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Now that you have the sub you can call it whenever you want. "Whenever you want" means on various user's actions, like checking or un-checking a checkbox and in various other places like form's initialization (loading) these are events. According to your question you need to call it from 3 different events.
1.When the form is loading in order to get the initial status:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
2.When the user changes the StructcheckBox status:
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
3.When the user changes the LegcheckBox status:
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Here is the full form's code:
Public Class Form1
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
End Class
Hope this helps.

How to share events between forms

So i have a tray icon that should behave the same way between 3 forms. I then created this code:
Private Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
TrayForm.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
Which is used to handle the tray icon. How can i do to share this event between the forms so i don't have to place this same code on every form?
How are event handlers working exactly? I looked online and on MSDN and it is not clear to me.
Thanks
Are you sure that you want to share the event, and not juste the code that will handle the event?
If you don't want to copy and paste your code, which you need to handle the events of more than one form, here's a way to do it:
Declare the sub which contains the code needed to handle the event as a public shared sub. Like this:
Public Shared Sub TrayForm_MouseClick(sender As Object, e As MouseEventArgs)
So, now you have a Sub which can handle the event you want to handle from all three forms.
Now, when you initialize those forms, add a line to make the shared Sub handle the event you want it to handle:
AddHandler NotifyIcon1.MouseClick, AddressOf ProjectName.FileName.TrayForm_MouseClick
ProjectName.FileName is meant here to be the path to refer to the shares Sub inside the file where you put it. I usually name it like ProjectNameUtils.vb or something like that.
If you just want to avoid copy and pasting your Sub so you don't have to modify it at several places every time you change something, this could be a way to achieve that.
As Stipulated by Hans Passant:
Sub Eclass_EventHandler(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Right Then
If Not Application.OpenForms().OfType(Of TrayForm).Any = 1 Then
Me.ContextMenuStrip1.Show(Cursor.Position)
End If
End If
End Sub
On the Trayform.VB just did the trick.
But about the shared event. i Have one that would have to be:
Private Sub FormClosingEVENT(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromMenu Then e.Cancel = True
Me.WindowState = FormWindowState.Minimized
'Application.Exit()
End Sub
How should i handle this?

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.