Antoine Posted September 2, 2003 Posted September 2, 2003 Dear, Does anyone know what "sender" stands for in (ByVal sender As Object, ByVal e As System.EventArgs) ? Where can I use it for ? Regards, Antoine Quote
Grimfort Posted September 2, 2003 Posted September 2, 2003 Its usually the control that raised the event, like a button, or a timer etc etc. You have to ctype it into the relevant object, then you can use it straight off. Its very usefull if you use 1 event to cover say 10 textboxes, as it will pass you the textbox as the Sender. e.g CType(Sender, Button).Enabled = False btw: to handle more then one function just add it at the end of the procedures like so: Private Sub mnuEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEdit.Click, mnuPopupEdit.Click Quote
Administrators PlausiblyDamp Posted September 2, 2003 Administrators Posted September 2, 2003 sender is the object that triggered the event. i.e. If this was a button click event sender would be the button clicked by the user. This can be useful if more than one button shared the same event handler. Best way to use it is to cast it too the correct type and then you will have access to it's properties. Protected Sub TestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TestButton.Click dim b as button b= DirectCast(sender, button) 'Can now access things like b.Text = "OK" messagebox.Show (b.Name ) 'etc. this can be useful in a scenario like below where more than one button shares the handler Protected Sub TestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TestButton.Click, TestButton2.Click 'notice handling bothe TestButton1 and 2 in the same function. dim b as button b= DirectCast(sender, button) 'Can now access things like b.Text = "OK" messagebox.Show (b.Name ) 'etc. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders dynamic_sysop Posted September 2, 2003 Leaders Posted September 2, 2003 sender represents the object that is passing the information, here's an example of casting a sender ( Listview1 ) as a Listview... Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click Dim lvi As ListView = DirectCast(sender, ListView) '/// sender is Listview1 '/// lvi is now the same as listview1 MessageBox.Show(lvi.SelectedItems(0).Text) End Sub Quote
Leaders dynamic_sysop Posted September 2, 2003 Leaders Posted September 2, 2003 grrrr , beat me again :p Quote
Antoine Posted September 2, 2003 Author Posted September 2, 2003 Hmm, nice and how do I use it then something like sender.text = "Hello world for instance ? Quote
Grimfort Posted September 2, 2003 Posted September 2, 2003 OOO, didnt know about DirectCast, slightly different use from Ctype but has a speed advantage if you know what your doing. DirectCast( Sender,TextBox ).Text = "Hello World" Quote
Administrators PlausiblyDamp Posted September 2, 2003 Administrators Posted September 2, 2003 sender.text = "Hello world" would work but more reliable if you cast sender to the correct object type. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Grimfort Posted September 2, 2003 Posted September 2, 2003 I always put Option Strict on, just to make sure I dont accidently miss something, in my line of business its critical :). Quote
*Experts* Bucky Posted September 2, 2003 *Experts* Posted September 2, 2003 sender.text = "Hello world" would work but more reliable if you cast sender to the correct object type. Er, no it wouldn't... the sender argument is of the Object type, and the Object does not have a Text property. The compiler would not know how to cast it correctly, so it'd throw an error at you. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Administrators PlausiblyDamp Posted September 2, 2003 Administrators Posted September 2, 2003 Er, no it wouldn't... the sender argument is of the Object type, and the Object does not have a Text property. The compiler would not know how to cast it correctly, so it'd throw an error at you. It would work if Option Strict wasn't set - however I would personally never consider writing any production code without Setting both Option Explicit and Option Strict At runtime it could definately cause an error if sender didin't have a Text property - why I recomended casting to the correct type. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Antoine Posted September 3, 2003 Author Posted September 3, 2003 Okay, As Grimfort told, I could use the Directcast, should I need more ? Do I have to declare something ? Cos' now I use AddHandler etc etc, AddressOf MyEvent If I want to use the "sender", do I need to set more then that ? Because I have 12 buttons, which all need (with a slight different though), the same routine, so I created those at run-time in an array. So I can let the program jump to that specific part in the code, but I don't know which button created the event ;). And that could make the whole thing work a lot better :D. Is that possible with sender ? Or should I do that in a whole different way ? REgards, Antoine Quote
Administrators PlausiblyDamp Posted September 3, 2003 Administrators Posted September 3, 2003 http://www.xtremedotnettalk.com/showthread.php?s=&threadid=75448&highlight=collection Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
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.