PC Review


Reply
Thread Tools Rate Thread

button tag for many buttons on same sub

 
 
Antonio Policelli
Guest
Posts: n/a
 
      9th Oct 2003
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
 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      9th Oct 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      9th Oct 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      9th Oct 2003
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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

>
>



 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      9th Oct 2003
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



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Oct 2003
* (E-Mail Removed) (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>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 buttons change to 1 button for one go Frank Situmorang Microsoft Access Form Coding 6 5th Jan 2010 11:09 PM
master button for all option buttons Jesse Microsoft Access Forms 1 20th Nov 2006 05:43 AM
How to get Focus on one Button,if two buttons exists on the same f =?Utf-8?B?UmF2aW5kcmE=?= Microsoft ASP .NET 4 3rd Oct 2006 04:38 PM
Two buttons, same form VBA overwriting first button somethings.amiss@gmail.com Microsoft Access Forms 1 10th Aug 2006 08:21 PM
Is it possible to have Min/Max buttons AND Help button on a form's title bar? And more... Keith Microsoft Dot NET Framework Forms 2 10th Aug 2005 10:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:46 PM.