English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Any programmers here?

2007-05-05 04:28:08 · 2 answers · asked by He D 1 in Computers & Internet Programming & Design

Hmmm... Thanks for that, but why wouldn't this work
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim tb1 As TextBox = TextBox1
Dim tb2 As TextBox = TextBox2
Dim tb3 As TextBox = TextBox3
Dim add As Button = Button1
Dim subtract As Button = Button2
Dim multiply As Button = Button3
Dim divide As Button = Button4
If Button1 Then tb1(+tb2 = tb3)



End Sub

2007-05-05 07:23:32 · update #1

2 answers

In VB you can use a SELECT statement which will execute different sections of code depending upon the results of a case.

In your 4 button calculator (+,-,/,*) when ever the user enters a number and presses a function button you would save the entered number in to a variable for later use and set a second variable to identify what function button was pressed (add, sub,multply,divide)

To aid your code writting youate your own customized datatype by using the ENUM at the begining of your form.

Public Class Form1
Private m_function As myCalcFunction
Private m_FirstNum as double
Private m_SecondNumber as double

Private Enum myCalcFunction
addition = 0
subtract = 1
multiply = 2
divide = 3
End Enum

You would place buttons on the form to serve as function buttons (+,-,*,/), the equals ,and numbers 0 to 9 and a label to serve as a display (lblDisplay)

Whenever a function button is clicked the m_FirstNumber is saved and the m_function is set to the type of function

Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
m_firstNum = Val(lblDisplay.Text)
m_function = myCalcFunction.addition
lblDisplay.Text = "" ' Clear display for next number to be entered
End Sub

This proceedure uses the Val function to capture the contents of the display label and convert it into a number. The function is captured (Addition) and the display is cleared for the next number to be entered.

Then when the equal button is pressed the following code is triggered

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim answer As Double

Select Case m_function
Case myCalcFunction.addition
answer = m_FirstNum + m_SecondNum
Case myCalcFunction.subtract
'your code to subtract goes here
Case myCalcFunction.multiply
'your code
Case myCalcFunction.divide
'your code
End Select
lblDisplay.Text = answer

End Sub

This uses the select statment to determine what code to runs based upon the function (+,-,*,/). Then displays the answer

2007-05-05 06:25:02 · answer #1 · answered by MarkG 7 · 0 0

you declare some variables valueA ,valueB,result
and one to store the operator(I would use a number to represent each operator e.g "1 for +").
Then when the user clicks on a number you store the number in value A if its the first number to be clicked he then click on an operator say "+" you store that in the operator variable. He then clicks the second value store in valueB.

Then you have a switch statement which pics the operation to persorm in the 2 numbers based on the oprator variable e.g

Switch(operator)

case 1: "Add the two variables and store then in result variable"
.......etc

// Youy will later want to account for the user doing strange things like clicking the operator first and then a value or something like that.

EDIT
I havent done much VB but a lot of VB.net what is
"tb1(+tb2 = tb3)" in your code? doesn't make sense to me, are you trying to store the result of tb1 plus tb2 in tb3? It would be more like tb3= tb1+ tb2.

Also in VB,net it would be tb1.text + tb2.text, you have to get the text property of the text box (Might be different in VB)

2007-05-05 04:40:12 · answer #2 · answered by tru_story 4 · 0 0

fedest.com, questions and answers