sender ?

Antoine

Regular
Joined
Aug 12, 2003
Messages
58
Location
The Netherlands
Dear,

Does anyone know what "sender" stands for in

Code:
(ByVal sender As Object, ByVal e As System.EventArgs)

?

Where can I use it for ?

Regards,
Antoine
 
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
 
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.

Visual Basic:
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

Visual Basic:
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.
 
sender represents the object that is passing the information, here's an example of casting a sender ( Listview1 ) as a Listview...
Visual Basic:
    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
 
OOO, didnt know about DirectCast, slightly different use from Ctype but has a speed advantage if you know what your doing.

Code:
DirectCast( Sender,TextBox ).Text = "Hello World"
 
I always put Option Strict on, just to make sure I dont accidently miss something, in my line of business its critical :).
 
PlausiblyDamp said:
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.
 
Bucky said:
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.
 
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
 
Back
Top