Calculation ..... field type ... designer entries

devjet

Member
Licensed User
Longtime User
Can some one shed some light in to this, I need to do calculation where I some number in to a Edit field . result should be shown in a label view

Dim edtNumber As EditText
Dim lblVis1 As Label
Dim lblVis2 As Label
Dim btnCalc As Button


Sub btnCalc_Click
lblVis1 = root(2*6368*edtNumber/0.8279,2)/100
lblVis2 = 1.17*root(edtNumber,2)
End Sub

Question:
- On the above the number field is declare as a Text or should this be decleared as Int what do I set on the designer view of this contrl?

- could some explain how such a calculation is done, I am a bit confused how to set this up and what has to be configured in the Designer and what in code.

- if I like the calculation is performed without btnCalc_Click what would it be


What way is the best path to follow ?

Thanks for any input!


Excuse Me ...! I guess I posted this in the wrong Forum.. how do I move it ?
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Your declarations are OK.

The code could look like this:
B4X:
Sub btnCalc_Click
  Dim Numb1, Numb2 as Double
 
  Numb1 = edtNumber.Text
  Numb2 = Sqrt(2*6368*Numb1/0.8279)/100
  lblVis1.Text = Numb2
 
  Numb2 = 1.17*Sqrt(Numb1)
  lblVis2.Text = Numb2
End Sub
In the routine two variables are declared as Double
Numb1 gets the value from edtNumber.Text.
Even though edtNumber.Text is a String, setting its content to Numb1 converts it to a number.
You should test if edtNumber.Text is a number or set the edtNumber.InputType parameter to
EditText1.INPUT_TYPE_NUMBERS.
Numb2 is used for the result of the calculations.
You can set a number value to the text parameter of any view.

To run the calculation you need something that generates an event, a button is the best for that.

Best regards.
 
Upvote 0

devjet

Member
Licensed User
Longtime User
Thanks Klaus for your explanations!
What confuses me is the fact of adding a text field and then use it as numbers etc. In the tutorial it says somewhere that if it makes sense, B4A will convert it automatically. The tool l I used so far one could set the type ( Int TXT bool) etc directly in the control itself. When I then declare the variables it would auto complete the variables. That was kind of neat, but it had other shortfalls. So I guess I better get used to this way.

Just one more thing, I understand the way to invoke the calculation using a button. However having just one or two entire fields, it would be nice to have a instant calculation when entering numbers in the field, what is the way to do it that? Is there some onFocus onExit command?
Regards
hans
 
Upvote 0
Top