CF VB .NET Programming Question

  • Thread starter Thread starter Beebs
  • Start date Start date
B

Beebs

I have a more general question using VB .NET for the Compact
Framework. How can I create a routine that calls all buttons on a
form...for instance I want:

If one of the form's button's is pressed Then
set the text to bold
change the text color
save the button's text to a global
End If

It's probably pretty simple but if someone could provide me with an
example similar to what I'm looking for I'll try to take it from
there.

Thanks
 
In the Form Load

AddHandler Button1.Click, New EventHandler(AddressOf Button_Click)

AddHandler Button2.Click, New EventHandler(AddressOf Button_Click)

....
AddHandler Button99.Click, New EventHandler(AddressOf Button_Click) ' Don't
do this. Having 99 buttons is a very bad idea


Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim btn As Button = CType(sender, Button)

btn.Font = BoldFont

myLabel.Text = btn.Text

End Sub
 
You can use the for..each loop to achieve what you are wanting to do.

for each ctrl as control in myfrm.Controls
ctrl.font.setstyle.bold
...
next

Hope this gets you going in the right direction.

nb
 
Thanks for the help but I was more interested in having a number of
things happen only when a button was clicked, so I was more interested
in how to carry out the same task without having to have an OnClick
routine for each button and have to copy and paste the exact same code
into each routine.
 
You are missing the point. There is just one OnClick routine in my sample.
It is set as the event handler for every button. Internally it uses "Sender"
parameter to retrieve the properties of the button that fired the event
 
I wasn't commenting on your post Alex, what you posted worked
perfectly...I was simply commenting to the post by Noble Bell who
posted after you. Alex, thanks again for the great example.
 
Back
Top