How can I use string in AddHandler? - vb.net

I have a situation where I am creating radiobutton dynamically and I need to assign a handler on CheckedChanged. Here I don't have any specific function or procedure. So I wrote this statement:
AddHandler rb.CheckedChanged, AddressOf rb.Tag
But it gives error. How can I achieve this?
EDITED:
Actually I am writing a DLL for other programmers in the company. So they can design a particular form at run time. For now I have a procedure which creates all the controls. So for now if they want to create a radio button they call the procedure
AddRadioButton(Text as string, Checked as boolean)
Now on request,I want to add another parameter in this where they can give Procedure name as string and I can add handler to address of ProcedureName. SO now procedure will become like this
AddRadioButton(Text as string, Checked as boolean, OnCheckedProcedure as string)
And in my code I will use
AddHandler rb.CheckedChanged, AddressOf OnCheckedProcedure

The method you add has a handler has to have the same parameters as the event you are trying to handle. As far as the RadioButton, here is what the handler might look like for what you are trying to do:
Private Sub CheckedChanged(sender As Object, e As EventArgs)
Dim tempRB As RadioButton= CType(sender, RadioButton)
tempRB.Tag = tempRB.Checked.ToString
End Sub
Then you can add your handler like this:
AddHandler rb.CheckedChanged, AddressOf CheckedChanged

Related

Is there a way to have a variable be `Handles Rdb1.Move, Rdb2.Move` for a sub

I'm new to visual studio 2017. I want to have a variable that acts as a part of the code for a sub so instead of writing the whole thing I want to just put the variable.
I'm making a menu for a school project. I haven't really tried anything because I don't know how the code works.
Private Sub ckb1_CheckedChanged(sender As Object, e As EventArgs) Handles ckb1.CheckedChanged, rdbSuper.CheckedChanged, rdbPong.CheckedChanged
How do I make the rdbSuper.CheckedChanged, rdbPong.CheckedChanged into just a variable so I can just put that after the Handles?
Instead of using a Sub for each CheckedChanged-Event with the same code, you can create one Sub and bind that function to the events with AddHandler:
Sub FunctionForEventCheckedChanged(sender As Object, e As EventArgs)
'Your code for the CheckedChanged-Event.
End Sub
'bind the events with the FunctionForEventCheckedChanged function.
AddHandler rdbSuper.CheckedChanged, AddressOf FunctionForEventCheckedChanged
AddHandler rdbPong.CheckedChanged, AddressOf FunctionForEventCheckedChanged
You can also check for a specific checkbox inside the FunctionForEventCheckedChanged function with sender. So you can also create some code for specific checkboxes.
How this can help you?
It looks like you want to assign one function to multiple events. Since you can't group the events in a variable to use this variable on Handles you can do something like the following (based on the above explanation):
Dim arrCheckBoxes() As Control = {rdbSuper, ckb1}
For Each ctrlCheckBox As Control In arrCheckBoxes
AddHandler ctrlCheckBox.CheckedChanged, AddressOf FunctionForEventCheckedChanged
Next

AddHandlers in VB.NET

I'm trying to dynamically creating dropdownList boxes, and I want trying to add AddHandlers to them so that when an item is selected in them, it fires an event, but also need to pass another variable, and I don't know what to put as the parameter for system.EventArgs. Please look at the code below to see the problem I'm having.
AddHandler inputDrop.SelectedIndexChanged, AddressOf selOption(inputDrop, ???, var1)
Protected Sub selOption(ByVal sender As Object, ByVal e As System.EventArgs, ByVal tableCount As String)
End Sub
What do I put (???) right here.
The error:
is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
In addition what Mike C already explained, if the signature of the event handler does not match the event, you can always wrap the event handler in another method, for example an anonymous one:
Protected Sub selOption(ender As Object, e As System.EventArgs, somestring As String)
End Sub
...
For i = 1 To 10
Dim cbox = new ComboBox()
Dim number = i ' local copy to prevent capturing of i '
AddHandler cbox.SelectedIndexChanged, Sub(s, e) selOption(s, e, "Hi! I am Number " & number)
Next
Now, when the index of the last ComboBox changes, the somestring parameter passed to selOption will be Hi! I am Number 10, while it will be Hi! I am Number 1 for the first ComboBox etc.
When you register an event handler, you don't specify the arguments at that time. You're basically just setting a reference to a delegate that will handle the event when it is raised.
AddHandler inputDrop.SelectedIndexChanged, AddressOf selOption
The most important thing is that the method signature of the event handler matches up exactly with the method signature defined by the event. I'm not sure that your method would work because you have that extra tableCount parameter specified. You will need to modify your method signature to be:
Protected Sub selOption(ByVal sender As Object, ByVal e As System.EventArgs)
I'm basing that off the definition of SelectedIndexChanged for Winforms. This event could be defined differently in another technology, such as ASP.net or WPF. Or if this is some custom class, it could be an entirely different signature altogether. However, typically most event handlers have a similar structure of a sender (the instance that raises the event) and some event arguments.
Then when inputDrop fires it's event (when the selected item changes), your code will get automatically called. The arguments passed to this method will be passed directly from inputDrop, you do not have to specify them.
Also, your AddHandler statement must exist inside a method or code block, it can't just live in the class definition. It's a statement that must be executed like any other piece of code, it's not a declaration.
And there is yet another way of doing it. Inherit the control in question and add a property like this:
Public Class MyComboBox : Inherits ComboBox
Public Property tableCount As String
End Class
Then set your custom value and add a handler as you would for a regular ComboBox:
combo.tableCount = tableCount
AddHandler combo.Click, AddressOf combo_Click
Inside combo_Click, CType sender to your inherited type, and get the value you stored previously:
Private Sub combo_Click(sender As Object, e As System.EventArgs)
Debug.WriteLine(CType(sender, WorkflowActionBox).tableCount)
End Sub
You will need to replace current usages of ComboBox with those of MyComboBox, where you want the new property to be available. Simple as opening your designer file and doing find/replace.

Multiple event handlers for the same event in VB.NET

I've written two event handlers for the TextBox.Leave event for a TextBox1
The reason for this is that the first handler is a common one for multiple TextBox.Leave events which validates the values, and the second one is specific for the above TextBox1 which does some calculation of values.
My query is that can I know which of the two handlers will execute first when TextBox1.Leave happens?
(I know I can remove the code from the common handler to the specific one for TextBox1, but still I wish to know if there is a way.)
Thanks
As long as the event handlers are added using the AddHandler statement, the event handlers are guaranteed to be called in the same order that they were added. If, on the other hand, you are using the Handles modifier on the event handler methods, I don't think there is any way to be sure what the order will be.
Here's a simple example that demonstrates the order as determined by the order in which AddHandler is called:
Public Class FormVb1
Public Class Test
Public Event TestEvent()
Public Sub RaiseTest()
RaiseEvent TestEvent()
End Sub
End Class
Private _myTest As New Test()
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AddHandler _myTest.TestEvent, AddressOf Handler1
AddHandler _myTest.TestEvent, AddressOf Handler2
_myTest.RaiseTest()
RemoveHandler _myTest.TestEvent, AddressOf Handler1
RemoveHandler _myTest.TestEvent, AddressOf Handler2
End Sub
Private Sub Handler1()
MessageBox.Show("Called first")
End Sub
Private Sub Handler2()
MessageBox.Show("Called second")
End Sub
End Class
I'd recommend you change to having a single handler, and detect which textbox is being left:
Private Sub txt_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave, TextBox2.Leave
Dim txt As TextBox = DirectCast(sender, TextBox)
If txt Is TextBox1 Then
txt.Text = "Very important textbox!"
Else
txt.Text = "Boring textbox ho hum."
End If
End Sub
This is just additional information not related to the order of execution of multiple event handlers with the same "Handles" clause.
It may be of interest, though, to those wondering about when to use a "Handles" clause and when to use the "AddHandler" statement.
Addhandler is more useful when using a UDC (User Defined Control) to execute procedures/functions or manipulate data within a form.
UDCs know nothing about the form on which they have been attached or referenced.
They are generic and written for reuse in many projects.
Take (for example) the standard "TextBox" control.
Does TextBox know anything about the form on which it rests? No.
All its properties are available to the form but none of the form's properties is available to the Textbox.
(The Textbox is a pre-supplied "UDC", and Microsoft is the User defining it)
If you want your own UDC to operate on data in the main form, you define a public event within the UDC (let's call this event "UC_Update")
It is placed on a single line at the top of the code in the UDC:
Public Event UC_Update ' UC stands for "User Control"
When your UDC (let's call it "MyControl") wants to work with data on its parent form, it can call this event (within the UDC code) with the line:
RaiseEvent UC_Update
Within the code of the form on which this control has been placed or referenced, you attach this event belonging to your UDC to an instance of your control and "point" that event to another routine written in the form.
To do this, you use the "AddHandler" directive and an associated "AddressOf" operator.
Say the process you have written to manipulate data/controls in the form along with data/methods in your UDC, is called "MyControlUpdater".
It will be in your form and look like this:
Private Sub MyControlsUpdater()
......(code).......
end sub
The "(code)" may be lines that use data in your UDC (via public properties or public Subs/Functions) or use data or controls within your form. Your UDC can now do something with items on your form.``
You would place within your form's code the following:
AddHandler MyControl.UC_Update, AddressOf MyControlsUpdater
This directive is placed somewhere after the UDC has been instantiated:
Dim oMyControl as New MyControl
AddHandler oMyControl.UC_Update, AddressOf MyControlsUpdater
If, however, the UDC is physically on your form as "MyControl1", placed there via the Visual Studio Toolbox, then you would add the Handler in your form's "Load" procedure:
Private Sub Form1_Load(sender as object, e as eventargs) Handles Me.Load
AddHandler MyControl1.UC_Update, AddressOf MyControlsUpdater
It is important to note that you cannot pass parameters via this process (when using "AddressOf"). That is why there is no "sender" in the "MyControlsUpdater" subroutine. Values that relate to your UDC
must be obtained by way of public properties in that UDC. Public UDC functions and subroutines (subs) are also available.
If you are NOT working with a UDC (often the case) but with controls created by others such as Microsoft (and therefore the internal code of the control is not available), then you use the "Handles" clause to establish how the control is handled when a certain event arises (such as a "Click" on the control).
Private sub UpdateData(sender as object, e as eventargs) handles Textbox1.Click
........(code)......
End Sub
Of course, if your own UDC doesn't need to know anything about data on your form and you are referencing it rather than adding it physically to the form, you can just use the "Handles" delegation on your own UDC as per normal:
Dim MyControl1 as New MyControl
Private Sub UpdataData(sender as object, e as eventargs) _
handles MyControl1.Text.Leave
Here the form uses data in the control (via public properties) rather than the control using data within the form.

Using events for a dimmed variable

How can I use the event for a dimmed variable that is NOT a control.
This is my dimmed variable:
Dim engine As New Speech.Recognition.SpeechRecognitionEngine
I want to use the event "engine.SpeechRecognized".
You do it the same way you would for anything else where you wanted to add handlers explicitly:
AddHandler engine.SpeechRecognized, AddressOf HandleSpeechRecognized
See the documentation for the AddHandler statement for more information.
There are two ways to add error handlers in VB.NET. You can do so "manually" by using the AddHandler statement, such as:
Dim engine As New SpeechRecognitionEngine()
AddHandler engine.SpeechDetected, AddressOf OnSpeechDetected
With this approach, you would then need to manually implement the OnSpeechDetected event handler method, such as:
Private Sub OnSpeechDetected(ByVal sender As Object, ByVal e As SpeechDetectedEventArgs)
' Do something
End Sub
However, the second method is often easier. This second method is the way that events for controls are handled. However, it is only possible if your object variable is declared as a field (at the class level, outside of any method). All you need to do is add the keyword WithEvents before the variable name, such as:
Dim WithEvents engine As New SpeechRecognitionEngine()
Then, that variable name will show up in the left-side drop-down box at the top of your code window along with all your controls. When you select it in that drop-down box, you can then select any of its events in the right-side drop-down box and it will automatically create the event handler method for you:
Private Sub engine_SpeechDetected(ByVal sender As Object, ByVal e As SpeechDetectedEventArgs) Handles engine.SpeechDetected
End Sub

How to add event handler to local variable in VB.NET

I have a form in VB.NET that is used as a dialog in a mainform. Its instances are always locally defined, there's no field for it. When the user clicks the OK button in the dialog, it will fire an event with exactly one argument, an instance of one of my classes.
Since it is always a local variable, how can I add an event handler for that event? I've searched for myself and found something but I can't really figure it out...
Code for the event, a field in MyDialog:
public Event ObjectCreated(ByRef newMyObject as MyObject)
Code for the main form to call dialog : (never mind the syntax)
Dim dialog As New MyDialog()
dialog.ShowDialog(Me)
AddHandler ObjectCreated, (what do I put here?) //Or how do I add a handler?
As you can see I'm stuck on how to add a handler for my event. Can anyone help me? Preferrably with the best way to do it...
It's recommended, for consistency, that you use the same source and event args model as all system event handlers.
Create your own class inheriting from EventArgs, as:
Public Class MyObjectEventArgs
Inherits EventArgs
Public Property EventObject As MyObject
End Class
Then declare your event, and a handler method, like:
Public Event ObjectCreated As EventHandler(Of MyObjectEventArgs)
Private Sub Container_ObjectCreated(ByVal sender As Object, ByVal e As MyObjectEventArgs)
' Handler code here
End Sub
Then attach the handler to your event using:
AddHandler ObjectCreated, AddressOf Container_ObjectCreated
Additionally, you can use the Handles to attach to the event raised from your main form (assuming the name MainForm), as below:
Private Sub MainForm_ObjectCreated(ByVal sender As Object, ByVal e As MyObjectEventArgs) Handles MainForm.ObjectCreated
' Handler code here
End Sub
You need to write the subroutine that actually executes when the event is generated:
public Sub OnObjectCreated(ByRef newMyObject as MyObject)
...
End Sub
Then the handler is added:
AddHandler ObjectCreated, AddressOf OnObjectCreated
As a side note, ByRef does nothing here. All objects in VB are passed by reference. Only primitave variables (string, int, etc) by default use ByVal and can be set to ByRef