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?
Related
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.
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
Is it possible to Call a Sub with events?
I have the following sub, and when I press a button, I will trigger a Msgbox.
Public Class SelectLevel
Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
MsgBox("Test")
End Sub
End Class
Can I Call this sub in another Class? Something along the lines of:
Public Class TestClass
Private Sub Testing
Call Button_Start_Click ' I tried this first, but it didn't work.
Call SelectLevel.Button_Start_Click ' I tried this aswell but didn't work.
End Sub
End Class
Thank you!
You need to pass the parameters too.
This should work:
Public Class TestClass
Private Sub Testing
Call SelectLevel.Button_Start_Click(Nothing, System.EventArgs.Empty)
End Sub
End Class
Your code is correct except in one place. While calling, you have to give the required parameters
Your Sub Procedure is : Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
But you're calling as :Call SelectLevel.Button_Start_Click (without any values to be sent)
Call like this:
Private Sub Testing()
SelectLevel.Button_Start_Click(Button_Start, Nothing) ' I tried this aswell but didn't work.
End Sub
This will do your work
If the event you need is only click event then there is an easy way PerformClick function
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.PerformClick()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox("pressed")
End Sub
End Class
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.
So putting it as simply as possible, I have one class which opens a form from inside sub like so:
Public Sub LoadExtension()
'various code...
Dim form as new frmMain
frmMain.ShowDialog()
'various code...
End Sub
And inside this form I have two buttons, one will just close the form, so the LoadExtension() sub will continue. The other button I want to use to 'exit' the LoadExtension() sub inside the main class so that loading stops completely. The button event inside the form module is like so:
Private Sub btnStopLoad_click(sender as object, e as eventargs) handles btnStopLoad.click
'exit the LoadExtension sub somehow
End sub
What is the simplest way to achieve this? I thought I could do something like this in the LoadExtension() sub (after the .ShowDialog):
If frmMain.btnStopLoad.clicked then
Exit Sub
End If
But it won't let me do that from the class module, apparently I need to use 'raise event' or something? I'm not very familiar with the way that events work. Can someone offer me an easy solution? I'd be very grateful. I've been looking around the web for a solution but haven't had any success. Thanks for your time.
You can achieve this by setting the DialogResult of the frmMain.
Public Class frmMain
Inherits Form
Private Sub _ButtonContinueClick(sender As Object, e As EventArgs) Handles ButtonContinue.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub ButtonExitClick(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
End Class
Then change the LoadExtension method to something like this:
Public Sub LoadExtension()
'various code...
Using form As New frmMain()
If (form.ShowDialog() <> Windows.Forms.DialogResult.OK) Then
Exit Sub
End If
End Using
'various code...
End Sub
The simplest way is to use dialogresult to let the LoadExtension function know which button was pressed.
DialogResult is what you are looking for - the standard way to do it.
You can, however, subscribe to events of another form's controls, like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim frm As New Form2
AddHandler frm.Button1.Click, AddressOf OtherFormButton_Click
frm.ShowDialog()
End Sub
Private Sub OtherFormButton_Click(sender As System.Object, e As System.EventArgs)
MessageBox.Show("hello")
End Sub
This is assuming Form2 has a button named Button1.