Will
Will

Reputation: 1164

Button Control event handler

Im new to vb.net and was wondering this question. When you click on a button control on a web form visual studio automatically put in this code for you in the code behind.

     Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFlightInfo.Click
     end Sub

Say for example, I wanted to call this method from another method. How would I do that? I know that the sender is the actual button object, but what is e? Here is what I have so far inside another sub. Just trying to get a better understanding of how this method works.

btnSave_Click(btnSave, ??what would you put there??)

Upvotes: 1

Views: 266

Answers (4)

Sam
Sam

Reputation: 46

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)    Handles       btnSave.Click,btnsecond.Click

dim btn as button=ctype(sender,button) ifenter code here btn.text="save" else if btn.text="Second" end if ' Call your private method End Sub

'see Based On Sender ,You can Find Out Which Button You Have Click 'if you have to call from another method you jaus call ' btnsave_click(nothing,nothing)

'as For WhaT Is event args is that 'EventArgs represents the data related to that event, and can be used to pass parameters, 'state information whatsoever to the event handler for the handling code to decide what to 'do. In this case it is just EventArgs which represents empty event arguments but for 'example if you have ImageButton's Click handler, you'll note that it's event arguments 'provide you access to the X and Y coordinates. In other words, Event args can be 'represented by more specific classes with event related data, depending on the control

Upvotes: 0

Kiru
Kiru

Reputation: 3565

Not a good idea to call event, move you code from the event to another private method and call this in both places.

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFlightInfo.Click
    ' Call your private method
End Sub

Protected Sub AnotherMethod
    ' Call your private method again here
End Sub

Upvotes: 2

skhurams
skhurams

Reputation: 2145

you will use delegate sender is the button and e is the event fired sender is the listener and event is catcher

Upvotes: 0

Ry-
Ry-

Reputation: 224913

Just pass EventArgs.Empty, nothing important is in there for a button click. You might consider reversing it, though: have another Sub that contains the common logic, and call that from both the Click event handler and your other method.

Upvotes: 1

Related Questions