Pvt Sub X_Click(...) Handles btn1.Click, btn2.Click - multi contro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

If I have a sub that handles multiple controls - such as

Private Sub X_Click(ByVal sender As System.Object, ByVal e As EventArgs)
Handles btn1.Click, btn2.Click

If btn1.Click = true then messagebox.show "Btn1"
If btn2.Click = true then messagebox.Show "Btn2"

End Sub

How does Sub X_click know which button was clicked? How do I use sender or
e in this situation?

Thanks,
Rich
 
Rich said:
Hello,

If I have a sub that handles multiple controls - such as

Private Sub X_Click(ByVal sender As System.Object, ByVal e As EventArgs)
Handles btn1.Click, btn2.Click

If btn1.Click = true then messagebox.show "Btn1"
If btn2.Click = true then messagebox.Show "Btn2"

End Sub

How does Sub X_click know which button was clicked? How do I use sender or
e in this situation?

Thanks,
Rich

one way:

Private Sub X_Click(ByVal sender As System.Object, ByVal e As EventArgs)
Handles btn1.Click, btn2.Click

select case directcast(sender, button).name
case btn1.name
messagebox.show "Btn1"
case btn2.name
messagebox.Show "Btn2"
end if
End Sub

There is a way doing it with "Is" I think, but not sure exactly.

chris
 
Rich,
Private Sub X_Click(ByVal sender As System.Object, ByVal e As EventArgs)
Handles btn1.Click, btn2.Click

Roughly typed
\\\\
Select case directcast(sender,button).name
case "btn1"
'do for btn1
case "btn2"
'do for btn2
End select
///

I hope this helps,

Cor
 
Chris,

Before you think it, I did not copy it from you. I had not seen your message
before I send it.

:-)

Cor
 
Rich said:
If I have a sub that handles multiple controls - such as

Private Sub X_Click(ByVal sender As System.Object, ByVal e As EventArgs)
Handles btn1.Click, btn2.Click

If btn1.Click = true then messagebox.show "Btn1"
If btn2.Click = true then messagebox.Show "Btn2"

End Sub

How does Sub X_click know which button was clicked? How do I use sender
or
e in this situation?


\\\
Dim SourceControl As Button = DirectCast(sender, Button)
Select Case True
Case SourceControl Is Me.Button1
...
Case SourceControl Is Me.Button2
...
...
End Select
///
 
Herfried,
\\\
Dim SourceControl As Button = DirectCast(sender, Button)
Select Case True
Case SourceControl Is Me.Button1
...
Case SourceControl Is Me.Button2
...

You are to much active in the C# Entwickler newsgroup the last times in my
opinion.

:-)))

Cor
 
Cor,

Cor Ligthert said:
You are to much active in the C# Entwickler newsgroup the last times in my
opinion.

:-)))

Mhm... Why? C#'s "crippled" 'switch' statement doesn't support this sort
of selection...
 
Thanks all for your replies. Everything works great!

Thanks again,
Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top