Newbie Procedure Question

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

Guest

Hello All!
I havethe following code written when as buton click event

If rb50c.Checked = True Then
mybet50c += 0.5
txbxCPwage.Text = mybet50c.ToString.Format("{0:C}", mybet50c)
ElseIf rb1.Checked = True Then
mybet1 += 1.0
txbxCPwage.Text = mybet1.ToString.Format("{0:C}", mybet1)
ElseIf rb5.Checked = True Then
mybet5 += 5.0
txbxCPwage.Text = mybet5.ToString.Format("{0:C}", mybet5)
ElseIf rb10.Checked = True Then
mybet10 += 10.0
txbxCPwage.Text = mybet10.ToString.Format("{0:C}", mybet10)
ElseIf rb25.Checked = True Then
mybet25 += 25.0
txbxCPwage.Text = mybet25.ToString.Format("{0:C}", mybet25)

End If

I have 4 more buttons where this same code is used, except the only variable
that changes is "txbxCPwage". I have 4 diffrent textboxs where this would go
to. What is the best way to keep this code, change that variable, without
writing all of this code 4 more times.
As always, thanks in advance!

Rudy
 
Either use multiple handles to associate the sub, or use the addhandler
keyword to specify the delegate. If only textboxes will call the sub below
will work.

Private sub MySub_SomeEvent(byval sender as object, byval e as eventargs)
handles txbxCPwage.LostFocus, txbxCPWage2.LostFocus

dim t as textbox = ctype(sender, Textbox)

If rb50c.Checked = True Then
mybet50c += 0.5
t .Text = mybet50c.ToString.Format("{0:C}", mybet50c)
ElseIf rb1.Checked = True Then
mybet1 += 1.0
t .Text = mybet1.ToString.Format("{0:C}", mybet1)
ElseIf rb5.Checked = True Then
mybet5 += 5.0
t .Text = mybet5.ToString.Format("{0:C}", mybet5)
ElseIf rb10.Checked = True Then
mybet10 += 10.0
t .Text = mybet10.ToString.Format("{0:C}", mybet10)
ElseIf rb25.Checked = True Then
mybet25 += 25.0
t .Text = mybet25.ToString.Format("{0:C}", mybet25)
End If
End Sub
 
Thanks Jim, this is great. How would I call this? This is the proc i created.

Private Sub AddBet(ByVal sender As Object, ByVal e As EventArgs) Handles
txbxCPwage.LostFocus, txbxFUMwage.LostFocus, _
txbxINTwage.LostFocus, txbxIPwage.LostFocus, txbxTDwage.LostFocus
Dim T As TextBox = CType(sender, TextBox)
 
Rudy,
The handles statement are used to listen for a specific event, when that
event fires the code will execute. In this case the code will fire each time
the text box looses focus. I read your original post too quickly and missed
the part where you noted the button click event.

You can keep the code in your click event and use the following inside that
event:

Dim t As TextBox
For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
' In case you have more textboxes than the ones listed below
Select Case t.Name
Case Is = "txbxCPwage", "txbxFUMwage", "txbxINTwage",
"txbxIPwage", "txbxTDwage"
If rb50c.Checked = True Then
mybet50c += 0.5
t.Text = mybet50c.ToString.Format("{0:C}", mybet50c)
ElseIf rb1.Checked = True Then
mybet1 += 1.0
t.Text = mybet1.ToString.Format("{0:C}", mybet1)
ElseIf rb5.Checked = True Then
mybet5 += 5.0
t.Text = mybet5.ToString.Format("{0:C}", mybet5)
ElseIf rb10.Checked = True Then
mybet10 += 10.0
t.Text = mybet10.ToString.Format("{0:C}", mybet10)
ElseIf rb25.Checked = True Then
mybet25 += 25.0
t.Text = mybet25.ToString.Format("{0:C}", mybet25)
End If
End Select
End If
Next
 
Jim,
This is great!! Thank you for your help!

Jim M said:
Rudy,
The handles statement are used to listen for a specific event, when that
event fires the code will execute. In this case the code will fire each time
the text box looses focus. I read your original post too quickly and missed
the part where you noted the button click event.

You can keep the code in your click event and use the following inside that
event:

Dim t As TextBox
For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
' In case you have more textboxes than the ones listed below
Select Case t.Name
Case Is = "txbxCPwage", "txbxFUMwage", "txbxINTwage",
"txbxIPwage", "txbxTDwage"
If rb50c.Checked = True Then
mybet50c += 0.5
t.Text = mybet50c.ToString.Format("{0:C}", mybet50c)
ElseIf rb1.Checked = True Then
mybet1 += 1.0
t.Text = mybet1.ToString.Format("{0:C}", mybet1)
ElseIf rb5.Checked = True Then
mybet5 += 5.0
t.Text = mybet5.ToString.Format("{0:C}", mybet5)
ElseIf rb10.Checked = True Then
mybet10 += 10.0
t.Text = mybet10.ToString.Format("{0:C}", mybet10)
ElseIf rb25.Checked = True Then
mybet25 += 25.0
t.Text = mybet25.ToString.Format("{0:C}", mybet25)
End If
End Select
End If
Next
 

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