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.
Related
I have some functions that need to be active only after the Form as loaded.
I had this problem with the ComboBoxes and solved it using SelectionChangeCommitted instead of SelectedValueChanged since the first one only is fired when the user is the one making the change.
Is there any equivalent event for the DateTimepicker.ValueChanged (since this one like the Select.SelectedValueChanged is fired before everything is set)?
This is a pretty common situation.
When, after the first initialization of a Form, a Control's value is modified, the associated Event(s) is/are raises, to notify the change to the subscribers.
The ComboBox SelectionChangeCommitted event is a notable exception: it's used to discriminate between a user direct input and a more generic value change in the selection.
The DateTimepicker control, like most of the other standard controls, doesn't provide a similar event.
Since the Event Handlers subscribed to have attached code that is run when an event is raised, it's a common problem to avoid that these routines are executed when the values of the Controls on a Form are modified by the code that initializes them.
There are different methods to manage the raising of events that can cause unwanted cascading effects at the wrong time. Two of the more common:
Attach the Event Handlers only after the Controls' initialization is completed, using dedicated methods that are delegated to attach/detach the handlers
Make use a boolean Field as a flag to indicate whether the code associated to the Event Handlers should be run when the event is raised.
Both
Attach the Handlers after the initialization procedures:
Public Sub New()
InitializeComponent()
InitializeDataStuff()
InitializeEverythingElse()
AttachHandlers()
End Sub
Private Sub Form_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
RemoveHandlers()
End Sub
Protected Sub AttachHandlers()
Addhandler SomeControl.SomeEvent, AddressOf SomeHandlerMethod
Addhandler SomeOtherControl.SomeOtherEvent, AddressOf SomeOtherHandlerMethod
(...)
End Sub
Protected Sub RemoveHandlers()
Removehandler SomeControl.SomeEvent, AddressOf SomeHandlerMethod
Removehandler SomeOtherControl.SomeOtherEvent, AddressOf SomeOtherHandlerMethod
(...)
End Sub
Use a Boolean Field. The Event Handlers are attached using the Forms' designer:
private DontBotherToRunNow As Boolean = True
Public Sub New()
InitializeComponent()
InitializeDataStuff()
InitializeEverythingElse()
DontBotherToRunNow = False
End Sub
Private Sub DateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
If DontBotherToRunNow Then Return
(...)
'Do something
End Sub
I have a Button in UserControl1.
I am using UserControl1 in Form1.
I want to handle Button's Click event in Form1.
I tried to do same via:
AddHandler userControl1.Button1.Click, AddressOf Button1_Click
And:
Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs) Handles userControl1.Button1.Click
End Sub
but getting error.
Create your event on the UserControl:
Public Class UserControl1
Public Event UC_Button1Click()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent UC_Button1Click()
End Sub
End Class
And then use the new event:
AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click
Or you can simply define it like this on the UserControl and access to it from outside (not recommended):
Public WithEvents Button1 As System.Windows.Forms.Button
And then:
AddHandler uc.Button1.Click, AddressOf Button1_Click
I found this very confusing until I placed the "AddHandler" in the main program (not the UserControl).
So for clarification, the steps to ensure that you can "sense" in the main program an event which has occurred in a user control is:
Make sure any object in your user control for which you wish to alter properties (such as PictureBox.Image or TextBox.Text) has its modifier property set to "Public".
This is the only way you can allow other programs to alter properties.
For events which your want recognised (e.g. "Click","DblClick" etc.) place at the top of your User Control code one declaration line for a Public Event. As an example,
Public Event UC_MySub(.....parameters.....)
"UC_" is a prefix I use to help highlight that it is defined in a "UserControl".
The rest of the name ("MySub") can be anything and does not need to relate to the Click Event in any way. You could even call it "CreamedCheese" if you want!
Include any parameters you like the the definition of the public event, again they can be any type of name. These will be passed to the main program when you click on the User Control's object.
Now, Going to the event which runs when you "click" on a GroupBox (in this case), you kick off that public event as shown here:
Private Sub GroupBox_Click(sender As Object, e As EventArgs) Handles GroupBox1.Click
RaiseEvent UC_MySub(....Paramaters.....)
End Sub
You have to ensure the parameters passed in this call to the public event are the same in number (and type) as each of the parameters defined in the Public Event declaration itself.
NOW, you rebuild your User Object, then go to your main program.
In the "Load" routine of your main Form, add the following line FOR EACH OBJECT of the user defined object you are using.
For example, in my program I have 4 instances of my UDO (User Defined Object).
So I have added (assuming my UDO is named "MyUDO"):
AddHandler MyUDO1.UC_MySub, AddressOf SwapHands 'This is my sub accepting the values from the public event
AddHandler MyUDO2.UC_MySub, AddressOf SwapHands
AddHandler MyUDO3.UC_MySub, AddressOf SwapHands
AddHandler MyUDO4.UC_MySub, AddressOf SwapHands
The "SwapHands" routine is defined in my main program and accepts the parameters stored in the UC Public event. The "AddressOf" points your resident subroutine.
The only thing to ensure across all these definitions is that you have the same number of parameters (if you have any) in each case, in the same order, and are of the same type).
(Each parameter can be a different type but must "line up" in types declared in each
definition). (e.g. 1 Boolean, 1 String, another string). In the definitions and calls, there have to be (in this case) 3 parameters of "Boolean, String, String" - in that order.
Now when you run the program and click on the GroupBox (or whatever you have used) in your UDO (User defined object) (on any one of the four objects in this case), you will kick off the routine stored in your main program.
Hard to explain, but after taking HOURS to fathom how this works, I thought I would leave my comments in case others are experiencing the same confusion.
I'm new to custom classes. I have a class called 'game'. In the class, I have a method called 'addGame()' that creates a dynamic picture box called 'pBox'. After creating the control, I'm doing the following to register a click event:
AddHandler pBox.Click, AddressOf Me.launchGame
And here is launchGame:
Public Sub launchGame()
MsgBox(Me.name)
End Sub
The problem is, "Me.name" is always the most recently added instances name, not the one I clicked on.
Based on a suggestion, I also tried this:
Public Sub launchGame(ByVal sender As Object)
MsgBox(sender.name)
End Sub
But now "AddHandler pBox.Click, AddressOf Me.launchGame" says
Method 'Public Sub launchGame(sender As Object)' does not have a signature compatible with delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'
And "AddHandler pBox.Click, AddressOf Me.launchGame(Me)" says
AddressOf operand must be the name of a method without parentheses
Public Sub launchGame(ByVal sender As Object, ByVal sender as EventArgs)
MsgBox(sender.name)
End Sub
Now no errors, but the msgBox is blank.
I think the problem was that pBox was always the most recent pictureBox control. I created a control Array based on Creating Control Arrays in Visual Basic .NET and Visual C# .NET (MSDN).
Now I do the AddHandler in the AddNewpBox() method of my pBoxArray class.
I also created a list to handle the "game" class, as suggested by David Brunow. Then I'm setting the pictureBox "Tag" property to the index of the game in the "games" array.
So now my click handler looks like the following, and it seems to work great.
Public Sub pBoxClick(ByVal sender As Object, e As EventArgs)
MsgBox(games(sender.tag).name)
End Sub
I am trying to get a sound to play when a form is first shown (rather like a standard message box does for want of a better example). If using a standard form added through the designer I would generally do this by overriding the standard onshown event and then go on to call MyBase.OnShown(e)
The problem I've hit now is that the form is being created programmatically (Dim myForm as new Form etc) and as such I seem not to be able to use AddHandler to override this event. I don't doubt that I'm doing this in entirely the wrong way, but I'd appreciate any advice that can be offered. I would prefer advice from the perspective of VB.net, but I can just about muddle through in C#.
Form.OnShown is not an event. Rather, it is a method of the Form class which raises the form's Shown event. Here's the MSDN article that explains the OnShown method:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onshown.aspx
When you are making a derived class by using the form designer, you can override the OnShown method, but when you are simply accessing a form through its public interface, you need to use the Shown event instead. You can add an event handler for that event like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As Form1 = New Form1()
AddHandler f.Shown, AddressOf f_Shown
f.Show()
End Sub
Private Sub f_Shown(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Since the form doesn't exist in code, you would actually have to call the event then.
Try writing out the showing code first:
Public Sub form_Showing(ByVal sender As Object, e As EventArgs)
// play sound
End Sub
then when you create your form, you add the handler of the event:
Dim f As New Form
AddHandler f.Shown, AddressOf form_Showing
f.Show()
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