use the same method for two different events? - vb.net

So basically I've got three listboxes containing items. I want the items to be deletable, so for this I've got a ContextMenuStrip with only one item : Delete. Though, I'd like the items to be deletable, too, via a press on the Delete key. So i've got my code, that you can see here :
Dim TempList As New List(Of String)
For Each Trigger In ListBoxTriggers.SelectedItems
TempList.Add(Trigger)
Next
For Each Trigger In TempList
ListBoxTriggers.Items.Remove(Trigger)
Next
It's a little longer because there is data related stuff but now this is the part concerning the removing from the ListBox. Now, for this I've been using
Private Sub ToolStripMenuItemTriggers_Click(sender As Object, e As EventArgs) Handles SupprimerToolStripMenuItemTriggers.Click
(supprimer means delete in French). But the thing is I'd like to process the
Private Sub ListBoxDescription_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBoxDescription.KeyDown
in the same method. But I can't since e is not the same type... I of course can copy the same code in both handlers but that's not really... clean. I can, too, just create another method that I'll call in both ases like
Private Sub ListBoxDescription_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBoxDescription.KeyDown
Delete()
End Sub
Private Sub ToolStripMenuItemTriggers_Click(sender As Object, e As EventArgs) Handles SupprimerToolStripMenuItemTriggers.Click
Delete()
End Sub
But I don't really like it neither... doesn't look like the most efficient solution...
Is there anything I can do for this ?
Thank you in advance

KeyEventArgs derives from EventArgs, so you can declare
Private Sub ListBoxDescription_KeyDown(sender As Object, e As EventArgs) Handles ListBoxDescription.KeyDown
and if you actually need e as KeyEventArgs then you can use
Dim kea = DirectCast(e, KeyEventArgs)
Also, if your delete method has a signature like
Sub DeleteThings(sender As Object, e As EventArgs)
then you can do
AddHandler ListBoxDescription.KeyDown, AddressOf DeleteThings
AddHandler ToolStripButton1.Click, AddressOf DeleteThings
Note that you do not need a Handles clause when using AddHandler.

You'll have to write the common event handler similar to this:
Private Sub CommonEventHandler(sender As Object, e As EventArgs) _
Handles ToolStripMenuItemTriggers.Click, ListBoxDescription.KeyDown
If sender Is ListBoxDescription Then
Dim kea = DirectCast(e, KeyEventArgs)
If kea.KeyData <> Keys.F2 Then Exit Sub
End If
'' Common code
''...
End Sub
Works fine, pretty hard to win elegance points with it however. You might as well move the Common code into a separate private method. The usual advice is to treat whomever is going to maintain your code some day as a homicidal maniac that knows where you live.

Related

VB.net One sub for many events

I'm working on a function in VB.Net where a user can select a supplier from a list.
The idea is that the user will filter the list until the right supplier is visible in a datagridview
the user can then either double click on the row header, the cell content or select a supplier and then click an OK button
I am wondering though, how do I avoid building one Sub for each of the above three events, Can I create one sub that catches all three events?
Private Sub supplierSearchOkButton_Click(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles supplierSearchDataGridView.CellContentDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Personally, I would go with the extra method option mentioned in the comments but, if you want to, you should be able to do this:
Private Sub SetSupplier(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
or even this:
Private Sub SetSupplier() Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
If you're not using any properties of the other e parameters then you can use the most general EventArgs for all three events and if you're not using the parameters at all then you can ditch them altogether. I didn't test this specifically but I'm fairly sure both will work.

Visual Studio 2019 (vb) - issue with Reading/Writing My.Settings

I'm just starting to develop and so there are some concepts that are not clear to me, I need you to try to understand what I'm doing wrong.
My goal is to have a series of ComboBoxes that contain some strings, for example a string is this one, the ComboBox is called TYPE:
I am storing these strings in My.Settings for my convenience and to edit them directly from the app.exe.config file (these information are stored there right?).
I'm using this code
Private Sub AdminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.TYPE
ComboBoxType.Items.Add(item)
Next
End Sub
My issue is that, I'm unable to read from My.Settings.TYPE and when I try to write into it with the following code I doesn't find any strings added into My.Settings menu neither into app.exe.config.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonTYPEAddValue.Click
ComboBoxType.Items.Add(TextBoxType.Text)
ComboBoxType.Text = TextBoxType.Text
TextBoxType.Clear()
MsgBox("Item added!")
End Sub
what is wrong?
In your code, it seems like you've used String to add/remove the ComboBoxType items. Follow the steps to achieve your requirement:
In this screenshot, you can see I've set the Type as System.Collection.Specialized.StringCollection to separate each time for future use.
Now, you may use the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.tests.Add(TextBox1.Text)
My.Settings.Save()
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub AdminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.tests
ComboBox1.Items.Add(item)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Settings.tests.Remove(TextBox1.Text)
My.Settings.Save()
ComboBox1.Items.Remove(TextBox1.Text)
End
It's noticeable that you can change the variable names, they're differ a bit than yours in my code.
Form structure for the code:
On the MyBase.Load event, all the items containing in My.Settings.tests will be added using For Each loop (benefit of StringCollection).
Working example of the form [adding - removing using My.Settings]:
I hope you've got your answer.

Working sample of Control.VisibleChanged Event in vb.net

I'm struggling to make the MSDN code sample for the Control.VisibleChanged event work: I don't see the MsgBox.
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs)
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub AddVisibleChangedEventHandler()
AddHandler myLabel.VisibleChanged, AddressOf Label_VisibleChanged
End Sub 'AddVisibleChangedEventHandler
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
You need to "wire up" the events to the event handlers.
To start with, to get the code in HideLabel_Click to be called you need it to respond to a click on the button named "HideLabel".
There are two ways to do that: you can use AddHandler or the Handles clause.
To demonstrate the latter:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub myLabel_VisibleChanged(sender As Object, e As EventArgs) Handles myLabel.VisibleChanged
MessageBox.Show("Visible change event raised!!!")
End Sub
End Class
However, you will notice that the message is shown even before the form appears. That is because of what goes on behind the scenes to create the form.
To avoid that happening, you can add the handler after the form has been shown:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub myLabel_VisibleChanged(sender As Object, e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler myLabel.VisibleChanged, AddressOf myLabel_VisibleChanged
End Sub
End Class
Another way, in VB2015 and later, is to use a "lambda expression" instead of a separate method, although then you cannot disassociate the handler from the event with RemoveHandler:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler myLabel.VisibleChanged, Sub() MessageBox.Show("Visible change event raised!!!")
End Sub
End Class
Craig was kind enough to [and I quote verbatim] call attention to the importance of Option Strict when you add handlers manually using AddHandler. Without it, the "relaxed delegate convention" may allow adding handlers which don't exactly match the event signature that you won't be able to remove later.
Having said that, Option Strict On isn't a complete safeguard: notice how my last example compiles and works even with the wrong method signature for the handler.
[I suspect that the MSDN code sample was first created in C# as part of a larger example, so some parts have been lost in the translation and excerption.]
I get this is old but came across this post when looking for more information on VisibleChanged and couldn't help but notice that the accept answer may be misleading. If you are using a designer to create your Form and place objects on it, then the accepted answer will be fine. In fact you can get rid of the addHandler because the designer handles that for you. All you would need to do is use a handles clause with your label.
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myLabel.VisibleChanged
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Where the issue lies with the accepted answer is if you arn't using a designer. Adding handle clauses to "wire up" simply won't work (we can make it work and if anyone is interested in that I'll be happy to post a code snippet of that, but it's not how the accepted answer lays it out). In your case all you need to do is call AddVisibleChangedEventHandler() to set up the handler. that's it. you could have done this by calling it in MyBase.Load
Private Sub Load_Form(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddVisibleChangedEventHandler()
End Sub
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs)
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Private Sub AddVisibleChangedEventHandler()
AddHandler myLabel.VisibleChanged, AddressOf Label_VisibleChanged
End Sub
Once again I know this is dated but couldn't help but notice that (more or less assuming) that you are trying to get a msgBox to appear when you click a label. That is you click a label and then toggled the visibility of another label. The other label is the one where the event handler is on for visibility change. So that inevitably gets called when clicking the original label. IF you only want this msgBox to appear when clicking that label and not when the form loads as well, you should change the addHandler statement so that you are adding a handler on the click event.
Private Sub Load_Form(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddVisibleChangedEventHandler()
End Sub
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Private Sub AddVisibleChangedEventHandler()
AddHandler otherLabel.Click, AddressOf Label_VisibleChanged
End Sub 'AddVisibleChangedEventHandler
Also Option Strict On has nothing to do with addhandler (From my understanding, could be wrong. please enlighten me if that is the case). Option Strict On is only checking to see that you arn't implicitly typecasting. So for example:
Dim a As Double
Dim b As Integer
a = 10
b = a
results in an error when Option Strict is On but is totally legal if it is off. So in the case of you leaving off the handles clause, you'll never be implicitly typecasting and therefore is not needed.
Hope this helps anyone who sees this question

How to know what control and event raised

I have a sub like this:
Private Sub mysub (parameters) handles control1.mousemove, control1.mousedown, control2.mousemove, control2.mousedown
'Statements
End Sub
I know what control activate sub but I want to know what event activated the sub (for example control2 activate sub but I want to know the event mousemove or mousedown)
Thanks
You cannot directly tell why your event handler was called. The best you're going to be able to do is a scenario like this:
Private Sub MouseMove_Handler(sender As Object, e As MouseEventArgs) Handles control1.mousemove, control2.mousemove
MouseCommonCode(sender, e, "MouseMove")
End Sub
Private Sub MouseDown_Handler(sender As Object, e As MouseEventArgs) Handles control1.mousedown, control2.mousedown
MouseCommonCode(sender, e, "MouseDown")
End Sub
Private Sub MouseCommonCode(sender As Object, e As MouseEventArgs, caller As String)
'Common Statements
End Sub
Though, I would say that this is a pretty odd request, and it would seem like there's probably a better way to do whatever you're trying to do.
I am c# kind of guy, so I am going on a limb here..
Can't you do something like:
Private Sub mysub1 (parameters) handles control1.mousemove
'Statements ...
End Sub
Private Sub mysub2 ( parameters) handles control1.mousedown
'Statements ...
End Sub
And so on?

Continuous update feature/event

Is there some kind of update feature like in C# for Visual Studio 2012 in vb.net? (so that it will continuously check to see if an if statement is fulfilled)
Try something like this:
Dim iClicks As Integer = 0
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
iClicks += 1
Label1.Text = iClicks.ToString()
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
It's better to have a integer counter than checking the string value and performing conversion each time. As you can see, the code checks the click counter each time you click the label.
If you want to check the value periodically, add a Timer and perform the check in its event:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
But consider the need to do this, because maybe it's not necessary and will decrease the performance comparing it with the other way.
There is probably a better way of doing this, but what I've done when I need a constant "update" function, is just use a Timer, then handle the Ticks.
Easiest way of implementing it is to go to the form designer, in the Toolbox, under Components, drag a timer onto the form. Double-click it to add a handle for its Tick event.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If WhateverYoureLookingFor = True
'Do stuff
End If
End Sub
This is obviously VB, but easy to convert to C#.