Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Button code using list box

Status
Not open for further replies.

VbAche

Programmer
Nov 28, 1999
8
US
I am setting up a calculator using a list box. The user will enter two numbers in text boxes and then select from the list box and then press a command button to perform the calculation.In the list box are all the operations that can be performed. (ie - add, subtract, multiply, divide, etc). I am trying to write the code for when the user selects only one choice from the list box and clicks on the command button,how is the code written in the command button click procedure ?

Please Help [sig][/sig]
 
What-ho,

there's a lot of people doing calculators atm...is it a school project or something?

Still - here's how I'd do your way of doing things (which is, er, a unique way of implementing a calculator):

Set up a form with the following on it:

3 x TextBox controls: txtArgOne txtArgTwo txtResult
1 x ListBox control: lstActions
1 x CommandButton: cmdCalculate

Now put the following in your form code:

Private Sub cmdCalculate_Click()
Select Case Me.lstActions.List(Me.lstActions.ListIndex)
Case "Add"
Me.txtResult.Text = Val(Me.txtArgOne.Text) + Val(Me.txtArgTwo.Text)
Case "Subtract"
Me.txtResult.Text = Val(Me.txtArgOne.Text) - Val(Me.txtArgTwo.Text)
Case "Multiply"
Me.txtResult.Text = Val(Me.txtArgOne.Text) * Val(Me.txtArgTwo.Text)
Case "Divide"
Me.txtResult.Text = Val(Me.txtArgOne.Text) / Val(Me.txtArgTwo.Text)
Case "Modulus"
Me.txtResult.Text = Val(Me.txtArgOne.Text) Mod Val(Me.txtArgTwo.Text)
End Select
End Sub

Private Sub Form_Load()

Me.lstActions.AddItem "Add"
Me.lstActions.AddItem "Subtract"
Me.lstActions.AddItem "Multiply"
Me.lstActions.AddItem "Divide"
Me.lstActions.AddItem "Modulus"

End Sub


Bear in mind that there's no error checking here. so if yu put words in instead of numbers, it'll probably go wrong and kill everyone...well, probably not, but you never know.

This is a "simple" way of doing things - que postings of "clever" ways :)


Cheerio,

Paul [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top