joe_pool_is Posted January 18, 2004 Posted January 18, 2004 Newbie Question: Could someone tell me what "sender" and "e" could be used for? Example: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' How would code use sender or e? ' If changed to Handles x.Click, is there a big difference? End Sub Thanks for the assistance. Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted January 18, 2004 Administrators Posted January 18, 2004 sender is the object that raised the event (the form in this case). e are any arguments that are relevant to the event. The form load event doesn't have any extra information passed to it so e really doesn't contain anything useful. Try it with something more useful like a mousedown and you can see the difference. Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown, Button2.MouseDown Dim b As Button b = DirectCast(sender, Button) 'convert sender to correct object type b.Text = "clicked " & e.Button.ToString() End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted January 18, 2004 Author Posted January 18, 2004 PossiblyDamp: Thanks for the reply. So this returns events from the system, not necessarily from the form itself. Right? The DirectCast() function is new to me. After playing with it just now, I see that I can also gather the FormVariable's name, which is something I have been trying to do for some time. vb: Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown, Button2.MouseDown Dim b As Button b = DirectCast(sender, Button) 'convert sender to correct object type b.Text = "clicked " & e.Button.ToString() TextBox1.Text = b.Name End Sub /vb In the example above (using your code), TextBox1 will show me the name of the button I clicked. Is there a preferred method of doing this? I am interested in learning the elegant method if it exists, rather than using brute force with it. P.S. How did you get your code to show up like that? Mine looks like garbage. Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted January 18, 2004 Administrators Posted January 18, 2004 (edited) Behind the scenes all events are based on another feature of .Net called delegates - in simple terms these define how a function looks, it's signature, (return type, number and type of parameters etc) but not what it actually does. This means any function that has the same signature can be used in place of the delegate. To keep the system flexible under .Net all the standard events are based on a delegate called System.EventHandler which has two parameters - the first of type object and the second of type EventArgs. This means events raised from different places in the system - not just the form itself (as you said) can be handled in one place. The first parameter will contain the control that raised the event but it's typed to an object due to the fact it can contain any .Net class or control. The directcast simple converts the object variable to the correct datatype (button in the case posted above). This maybe a problem and need further checking as the handler could work not only for multiple controls but multiple controls of different types i.e. Private Sub Something_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, TextBox1.Click Dim b As Button 'can only hold a button Dim t As TextBox 'can only hold a textbox Dim c As Control 'can hold any windows control 'the followin will work with either button or textbox in this sample 'but c will only allow access to the features common to all 'controls - any button or textbox specifics will be unavailable c = DirectCast(sender, Control) MessageBox.Show(c.Name) 'If the type is unkown we need to check first - as the following lines show. If TypeOf sender Is TextBox Then t = DirectCast(sender, TextBox) 'use t here End If If TypeOf sender Is Button Then b = DirectCast(sender, Button) 'use b here End If End Sub the above example only shows what can be done - if you need different processing for different control types using different event handlers is probably a better idea. Most controls also support the .Name property which should always be the name - some controls return different things for the .ToString function. Finally to display the code put it between [ vb ] and [ /vb ] tags (without the spaces between the square brackets). edit: typo Edited January 18, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted January 18, 2004 Author Posted January 18, 2004 Wow. Great information! I'm printing this out and sticking it in my favorite VB.NET book right now! ~Joe. Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.