473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

button tag for many buttons on same sub

hello, i want to have 10 buttons on a form and all will do almost
exactly the same thing except for one differnece. can i use the tag
property of each button in the same sub?

private sub allbuttonsclick (ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click, Button7.Click,
Button8.Click, Button9.Click, Button10.Click

select case "thebutton's tag"
case 1

case 2

case 3

etc....

end sub

if i can't do this is there a betters way?

thanks!
AP
Nov 20 '05 #1
5 8023
Hi,

You can use the tag property or Use the is clause in an if then
statement.

Dim btn As Button = sender

Dim strTag As String

strTag = CType(btn.Tag, String)

Select Case strTag

Case "A"

' do something

Case "B"

' do something

Case "C"

' do something

End Select

' or Method 2

If btn Is Button1 Then

' do something

ElseIf btn Is Button2 Then

' do something else

ElseIf btn Is Button3 Then

' etc.....

End If

Ken

--------------------

"Antonio Policelli" <bl*****@yahoo.com> wrote in message
news:62**************************@posting.google.c om...
hello, i want to have 10 buttons on a form and all will do almost
exactly the same thing except for one differnece. can i use the tag
property of each button in the same sub?

private sub allbuttonsclick (ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click, Button7.Click,
Button8.Click, Button9.Click, Button10.Click

select case "thebutton's tag"
case 1

case 2

case 3

etc....

end sub

if i can't do this is there a betters way?

thanks!
AP

Nov 20 '05 #2
Hi Antonio, yes.

VB.NET now provides a more robust event model, you can use the sender
property to determine which button control sent the click event:

Private Sub AllButtonsClick(ByVal sender As Object, ByVal e As EventArgs)
Handles ...
If sender Is Button1 Then
...
ElseIf sender Is Button2 Then
...
ElseIf sender Is Button3 Then
...
Else
...
End If
End Sub

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Antonio Policelli" <bl*****@yahoo.com> wrote in message
news:62**************************@posting.google.c om...
hello, i want to have 10 buttons on a form and all will do almost
exactly the same thing except for one differnece. can i use the tag
property of each button in the same sub?

private sub allbuttonsclick (ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click, Button7.Click,
Button8.Click, Button9.Click, Button10.Click

select case "thebutton's tag"
case 1

case 2

case 3

etc....

end sub

if i can't do this is there a betters way?

thanks!
AP

Nov 20 '05 #3
Hi Ken, Have you enabled Option Strict?
Dim btn As Button = sender
Should be:

Dim btn As Button = CType(sender, Button)
or
Dim btn As Button = DirectCast(sender, Button)

(Personally I prefer DirectCast, as I know that sender is going to be a
Button)

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:e9**************@tk2msftngp13.phx.gbl... Hi,

You can use the tag property or Use the is clause in an if then
statement.

Dim btn As Button = sender

Dim strTag As String

strTag = CType(btn.Tag, String)

Select Case strTag

Case "A"

' do something

Case "B"

' do something

Case "C"

' do something

End Select

' or Method 2

If btn Is Button1 Then

' do something

ElseIf btn Is Button2 Then

' do something else

ElseIf btn Is Button3 Then

' etc.....

End If

Ken

--------------------

"Antonio Policelli" <bl*****@yahoo.com> wrote in message
news:62**************************@posting.google.c om...
hello, i want to have 10 buttons on a form and all will do almost
exactly the same thing except for one differnece. can i use the tag
property of each button in the same sub?

private sub allbuttonsclick (ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click, Button7.Click,
Button8.Click, Button9.Click, Button10.Click

select case "thebutton's tag"
case 1

case 2

case 3

etc....

end sub

if i can't do this is there a betters way?

thanks!
AP


Nov 20 '05 #4
Cor
Hi Antonio,
This I have sended yesterday to Dino.b. B.
It is not exact the same as you ask, but it has the same solution for your
problem.
(it is a calender that has as much buttons as the month, you have only 10)
It builds buttons dynamicly and you can catch the events if you set the
right handles.
In this example it is only the click event.
\\\
Private mybutton(31) As Button
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
For i = 0 To System.DateTime.DaysInMonth(2003, 10) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim month As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & month.Text)
End Sub
End Class
///
I hope this helps a little bit
Cor

Nov 20 '05 #5
* bl*****@yahoo.com (Antonio Policelli) scripsit:
hello, i want to have 10 buttons on a form and all will do almost
exactly the same thing except for one differnece. can i use the tag
property of each button in the same sub?

private sub allbuttonsclick (ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click, Button7.Click,
Button8.Click, Button9.Click, Button10.Click


Why not this:

\\\
Select Case True
Case sender Is Button1
...
Case sender IS Button2
...
...
End Select
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: KK | last post by:
Hi, I am using history.go(-1) for implementing the back button functionality. Its working fine but with this exception. 1. The page which is having back button has some hyperlinks on it. ...
7
by: opt_inf_env | last post by:
Hello, I would like to create a button which should be put in the end of a sentence (on the same line). I do like this: <html> This is my sentence. <form action=file.php> <input...
1
by: vulgate | last post by:
i use arg.button==( MouseButtons.Left+MouseButtons.Right) but the system promote the the type cant + who can help me ~
2
by: Terry | last post by:
Hi all, I have been googling this problem for many hours... I have the following greasemonkey script; (function() { if (true) { // Change these to match your setup var sabcomputer =...
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
3
by: Al Biheiri | last post by:
i forgot how to do this someone help how do i make this into one private void mnuTray_Click(object sender, System.EventArgs e) { notifyIcon1.Visible = true; WindowState =...
3
by: xtremebass | last post by:
Hi Bytes. i have 3 submit buttons namely click1,click2 and click3 . if i press the any of one submit button page submitted appropriate page correctly, But the problem is display the all...
1
by: abcdriver | last post by:
Later edit: Too complex: The only thing I should know is: The submit button's NAME (and value) will only be sent if that particular button was used to submit the form, so you should be able to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.