Easy way to pass a sender to a subroutine? Maybe I didn't word that right...

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

Hello - I'm working on a project to have 6 labels, each with a value of 0 -
9. Each label has a 2 buttons on the side, one labeled up, one labeled down.
I want the up button to increment til it hits 9, the down button to
decriment til it hits 0. That part I can do, but it's very ugly.

What I want to do is have a routine that handles the Up Buttons, a routine
that handles the Down Buttons, both of them then call a subroutine called
ChangeRange that does the work. The way I have it is by doing a clumsy if
then else for every set of buttons, as below:

private handles down buttons
intModifier = -1
call ChangeRange

private handles up buttons
intModfier = 1
call ChangeRange

Sub ChangeRange
if sender is up1button or sender is down1button then....
intNumber1 = intNumber1 + (intModifier * 1)
if sender is up2button or sender is down2button then....
intNumber2 = intNumber2 + (intModifier * 1)
if sender up2button then...
End if

me.number1label.text = intNumber1
me.number2label.text = intNumber2
me.number3... etc

The problem is I know there has to be a way to have one calc that applies to
whatever button was sent and it's corresponding variable (up1button and
down1button both apply to intNumber1), but my knowledge is very limited
right now. Any help would be appreciated!
 
Hi Patrick

You might want to start by creating a UserControl (in Visual Studio .NET a
Windows Control Library project). Put 1 label, and your two buttons on that.
Then put six of the user controls onto your form in your application.

HTH

Nigel Armstrong
 
Patrick,

Seeing this do I think that you are mixing a lot of things.

When you have one button event it is probably more easier and you do not
need more than that.

You can just make that by creating a button1.click event and than to add in
the end after the handler button1.click, the text , button2.click

Or when you want it even more easier when all the buttons are direct on the
form, than you can set your form load event (you have to make that button1
click event again)
\\\
for each btn as control on me.controls
if typeof btn Is Button then
AddHandler btn, AddressOf Button1_Click
end if
next
///
Than you can set in that event Button1 click event
\\\
Select case directcast(sender, button).name
case "button1"
'do your stuff for button1
case "button2"
'do your stuff for button2
etc
End select
///

I typed everything in this message so keep track of typos

I hope this helps?

Cor
 
Thanks for the help both of you, and just out of curriousity, is there a way
to ask the system what button has the focus, and return the button name or
act on it somehow?

Thanks,

Patrick
 
Hello - I'm working on a project to have 6 labels, each with a value of 0 -
9. Each label has a 2 buttons on the side, one labeled up, one labeled down.
I want the up button to increment til it hits 9, the down button to
decriment til it hits 0. That part I can do, but it's very ugly.

You might also look at the NumericUpDown control which does precisely as
you want.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

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