I’ve recently gotten into using xojo on my mac, and im currently stuck on adding values together using different buttons that add to a value in a textfield.
button1 = 10
button2 = 20
example: button1 is pressed 3 times and button2 is pressed once, the value in the textfield is 50
so everytime a button is pressed the value with it is added into the textfield, but everytime i attempt this it doesn’t add the value and just stays at the base value set in the button, or it keeps adding endlessly when using static rather than dim.
Here is a thought. Create a method, say "AddToTextField (Adder as integer) " and call it from the action event of your buttons, passing the adder value.
The method reads the textfield.text and converts to a number with Val. Then it adds Adder to the value and assigns the str(result) to textfield.text.
There are several variations on the theme. You can implement this idea many different ways.
You should instead use a property for values you need to share across events like this. In this case make “sum” a property on the Window:
Sum As Integer
This will serve as the total sum to display in the TextField.
Now your button code can simply add its own value to the sum and then update the TextField. Here’s the Action event code for the button that shows “10”:
Dim myButtonValue As Integer = 10
Sum = Sum + myButtonValue
TextField1.Text = Str(sum)
On the other button you can change myButtonValue to default to 20.
You could even tweak this a bit so that it gets the value from the button caption, if that’s how you are naming them. Then you won’t have to change the code for each button. In that case you can do this:
Dim myButtonValue As Integer = Val(Me.Caption)
Sum = Sum + myButtonValue
TextField1.Text = Str(sum)
[quote=344626:@Paul Lefebvre]You should instead use a property for values you need to share across events like this. In this case make “sum” a property on the Window:
Sum As Integer
This will serve as the total sum to display in the TextField.
Now your button code can simply add its own value to the sum and then update the TextField. Here’s the Action event code for the button that shows “10”:
Dim myButtonValue As Integer = 10
Sum = Sum + myButtonValue
TextField1.Text = Str(sum)
On the other button you can change myButtonValue to default to 20.
You could even tweak this a bit so that it gets the value from the button caption, if that’s how you are naming them. Then you won’t have to change the code for each button. In that case you can do this:
Dim myButtonValue As Integer = Val(Me.Caption)
Sum = Sum + myButtonValue
TextField1.Text = Str(sum)[/quote]
Hey that worked great, but im stuck on a similar problem but with checkboxes
Dim myButtonValue As Integer = 1
if CheckBox1.value = true then
Sum = Sum + myButtonValue
tfield_value.Text = "$" + Str(sum)
else
Sum = Sum - myButtonValue
tfield_value.Text = "$" + Str(sum)
end if
thats my code and i have multiple checkboxes, but when i deselect one the value changes and keeps adding / subtracting rather being removed once its deselected / selected.
i essentially want it to add a value and then remove that value once selected while having multiple checkboxes
Of course, you can use RadioButton, CheckBox or PushButtons to make maths, but using Up - Down Arrows may be a better UI (MAY BE).
Also, using a TextField to report a value may not be the good Control. Maybe a Label is the better object.
That is the result of the code you put in the CheckBox. If you do not share it, how can a reader make a guess on what is wrong ?
At last, after telling people you got an answer, youd better create a different Conversation.
Last advice:
Read Human Interface Guidelines to understand the Interface objects and have an idea on their use / how to use it (them) / whats best depending on what you want to do.
Heres the description Buttons, etc. related to your questions.
Nota: Linux and Windows also (with time) have the same kind of document(s). Thsi also exists for iOS
Where is this code? It looks fine to me and in a quick test it seemed to work. I put it in a Button Action event. When the CheckBox is checked, clicking the button adds 1 to the total. When the CheckBox is not checked, clicking the button subtracts one from the total.