Sign in to follow this  
Followers 0
darkxraver

calculate a value in an inputbox

30 posts in this topic

heya thx for the answer of my other topic it worked

but now im stuck again..

so i now have the inputbox which the user can change and it saves the new value - close and reopen the script and the new value will be shown

now i have a new feature where im stuck at:

another inputbox where the user can add a value, select from the combobox what element it is, hit the add button and it should add the value of the 2nd inputbox to the 1st inputbox

i.e.

inputbox1= 637

inputbox2= 378

combobox= Earth

addbutton

---------------------

inputbox1 + inputbox2 = inputbox1 <-- inputbox1 because it should write the new value in the inputbox1

637 + 378 = 1015 <-- should write the total value to the inputbox1

hope you understand my problem and could help me out

thx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Are you talking about in a GUI where you created this input via GUICtrlCreateInput() or are you actually talking about the InputBox() function? or something else entirely...

Hard to answer you as I dont know what other thread you mean and you havent posted any code to look at.

[edit] typo

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

oh yeah its all via gui

so its the GUICtrlCreateInput

uhm this is the first inputbox

$earth1 = GUICtrlCreateInput($e1ini, 45, 70, 55, 20)

this the second inputbox

GUICtrlCreateInput("0", 375, 70, 55, 20)

this the add button

GUICtrlCreateButton("Add", 325, 69,42,20)

and this is the combobox

GUICtrlCreateCombo("Earth", 440, 70, 55, 15)
    GUICtrlSetData(-1, "Fire|Water|Wind", "")

so yeah what id like it to do is that a user can add a random value into the second inputbox, select earth, fire, water or wind from the dropdown menu, hit the add button and it should add the value of the second inputbox to the first inputbox

thx

Share this post


Link to post
Share on other sites

Cast the string to a number then add.

Number($sString1)+Number($sString2)

Share this post


Link to post
Share on other sites

ugh dun know what you mean x:

but if somebody is interrested and could help me out i could send the whole code via pm

would really appreciate it

thx

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

kinda figured it out myself x: but still got 1 little problem..

$earth1 = GUICtrlCreateInput($e1ini, 45, 70, 55, 20)

$add1 = GUICtrlCreateButton("Add", 325, 69,42,20)

$addinput1 = GUICtrlCreateInput("0", 375, 70, 55, 20)

$combo1 = GUICtrlCreateCombo("Earth", 440, 70, 55, 15)
    GUICtrlSetData(-1, "Fire|Water|Wind", "")

select
        Case $nMsg = $add1  
            $data = GUICtrlRead($addinput1)
            $data2 = GUICtrlRead($earth1)
            GUICtrlSetData($earth1, ($data + $data2))
            GUICtrlSetData($addinput1, "0")
                
        EndSelect

this is what i got so far

what happens is i.e.:

inputbox1($earth1) = value of 100

inputbox2($addinput1) = user enters 5

user hits the add button ($add1)

it calculates the current value of inputbox1($earth1) and the new inputbox2($addinput1)

and enters the new value which is 105 in inputbox1($earth1)

and the value of inputbox2($addinput1) becomes "0" again

so it works fine xept for that i dont have the

$combo1 = GUICtrlCreateCombo("Earth", 440, 70, 55, 15)

GUICtrlSetData(-1, "Fire|Water|Wind", "")

added.

so what it should do is if "Earth" from the combobox is selected which is $combo1 and the user enters a value in inputbox2 which is $addinput and clicks the add button which is $add1 it should calculate the values only for "Earth" from the combobox

heres a screenshot from the interface with a little description

Posted Image

Edited by darkxraver

Share this post


Link to post
Share on other sites

lol silkroad alchemy bot xD

u can use switch and case with that

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

yeah that i know..

$earth1 = GUICtrlCreateInput($e1ini, 45, 70, 55, 20)

$add1 = GUICtrlCreateButton("Add", 325, 69,42,20)

$addinput1 = GUICtrlCreateInput("0", 375, 70, 55, 20)

$combo1 = GUICtrlCreateCombo("Earth", 440, 70, 55, 15)
    GUICtrlSetData(-1, "Fire|Water|Wind", "")

select
        Case $nMsg = $add1  
            $data = GUICtrlRead($addinput1)
            $data2 = GUICtrlRead($earth1)
            GUICtrlSetData($earth1, ($data + $data2))
            GUICtrlSetData($addinput1, "0")
                
        EndSelect

this is the code i have so far but theres 1 line im missing that tells it to calculate Earth, Fire, Water or Wind but i cant figure it out..

already tried something like

$earth1 = GUICtrlCreateInput($e1ini, 45, 70, 55, 20)

$add1 = GUICtrlCreateButton("Add", 325, 69,42,20)

$addinput1 = GUICtrlCreateInput("0", 375, 70, 55, 20)

$combo1 = GUICtrlCreateCombo("Earth", 440, 70, 55, 15)

GUICtrlSetData(-1, "Fire|Water|Wind", "")

select

Case $nMsg = $add1 & $combo1

$data = GUICtrlRead($addinput1)

$data2 = GUICtrlRead($earth1)

GUICtrlSetData($earth1, ($data + $data2))

GUICtrlSetData($addinput1, "0")

EndSelect

but didnt work x: so idk im kinda stuck

Edited by darkxraver

Share this post


Link to post
Share on other sites

I suspect there's some sort of language barrier here, as I'm not fully following what you are saying or doing.. but this line:

Case $nMsg = $add1 & $combo1

should be:

Case $nMsg=$add1 Or $nMsg=$combo1

And this must be cast properly

GUICtrlSetData($earth1, ($data + $data2))

to:

GUICtrlSetData($earth1, String(Number($data) + Number($data2)))

Share this post


Link to post
Share on other sites

sry about not beeing clear enough

i just changed it to what youve posted still works but as you can see in the picture a few posts above that i posted i have a combobox, inputbox and add button

what i want it to do is if i enter a value in the inputbox and select "Fire" from the dropdown menu/combobox menu and click on "add" it should calculate and enter the new calculated value into the fire slot/fire inputbox

and if i select water from the dropdown menu/combobox menu and click on "add" it should calculate and enter the new calculated value into the water slot/water inputbox

same goes for earth and wind

hope i explained it a bit better

thx

Share this post


Link to post
Share on other sites

Does something like this help?

Cause its what I thought you wanted.

#include <GUIConstantsEx.au3>

$Form1 = GUICreate("Form1", 429, 127, 193, 125)
$Input1 = GUICtrlCreateInput("", 16, 24, 121, 21)
$Label1 = GUICtrlCreateLabel("+", 144, 26, 10, 17)
$Input2 = GUICtrlCreateInput("", 160, 24, 121, 21)
$Label2 = GUICtrlCreateLabel("=", 288, 26, 10, 17)
$Input3 = GUICtrlCreateInput("", 304, 24, 121, 21)
$Input4 = GUICtrlCreateInput("", 304, 48, 121, 21)
$Input5 = GUICtrlCreateInput("", 304, 72, 121, 21)
$Input6 = GUICtrlCreateInput("", 304, 96, 121, 21)
$Combo1 = GUICtrlCreateCombo("1", 16, 56, 145, 25)
GUICtrlSetData (-1, "2|3|4")
$Button1 = GUICtrlCreateButton("Calculate", 168, 50, 123, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $out = GUICtrlRead ($Input1) + GUICtrlRead ($Input2)
            Switch GUICtrlRead ($Combo1)
                Case "1"
                    GUICtrlSetData ($Input3, $out)
                Case "2"
                    GUICtrlSetData ($Input4, $out)
                Case "3"
                    GUICtrlSetData ($Input5, $out)
                Case "4"
                    GUICtrlSetData ($Input6, $out)
            EndSwitch
    EndSwitch
WEnd

Brett

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

yeah thats what i was looking for

now i just need to take a better look at your code and add/change it in my script

edit: just noticed that with your code it doesnt store the value in the inputbox after the "="

so i.e. the value of the first inputbox would be 100 and the value of the second inputbox (after the "+") would be 5 the value in the inputbox after the "=" will always be 105 but i need it to stay 105 and if i would enter 10 in the second inputbox (after the "+") it should add the 10 to the 105 :S

thx

Edited by darkxraver

Share this post


Link to post
Share on other sites

Ahh, good job BrettF, now I understand.

darkxraver, you can clear out the 2 add boxes and retain the values by changing this part of the code:

Switch GUICtrlRead ($Combo1)
                Case "1"
                    GUICtrlSetData ($Input3, GUICtrlRead($Input3)+$out)
            GUICtrlSetData($Input1,"")
            GUICtrlSetData($Input2,"")
                Case "2"
                    GUICtrlSetData ($Input4, GUICtrlRead($Input4)+$out)
            GUICtrlSetData($Input1,"")
            GUICtrlSetData($Input2,"")
                Case "3"
                    GUICtrlSetData ($Input5, GUICtrlRead($Input5)+$out)
            GUICtrlSetData($Input1,"")
            GUICtrlSetData($Input2,"")
                Case "4"
                    GUICtrlSetData ($Input6, GUICtrlRead($Input6)+$out)
            GUICtrlSetData($Input1,"")
            GUICtrlSetData($Input2,"")
            EndSwitch

Also, you can test if the user entered a valid #.. (otherwise if they enter 1z or somesuch, it will give you a zero for that field).

Here's a test for a valid whole #

If not StringRegExp(GUICtrlRead($Input1),"\A\d+\z",0) Then ConsoleWrite("NOT A NUMBER!" & @CRLF)

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>

$Form1 = GUICreate("Form1", 429, 127, 193, 125)
$Input1 = GUICtrlCreateInput("", 16, 24, 121, 21)
$Label1 = GUICtrlCreateLabel("+", 144, 26, 10, 17)
$Input2 = GUICtrlCreateInput("", 160, 24, 121, 21)
$Label2 = GUICtrlCreateLabel("=", 288, 26, 10, 17)
$Input3 = GUICtrlCreateInput("", 304, 24, 121, 21)
$Input4 = GUICtrlCreateInput("", 304, 48, 121, 21)
$Input5 = GUICtrlCreateInput("", 304, 72, 121, 21)
$Input6 = GUICtrlCreateInput("", 304, 96, 121, 21)
$Combo1 = GUICtrlCreateCombo("1", 16, 56, 145, 25)
GUICtrlSetData (-1, "2|3|4")
$Button1 = GUICtrlCreateButton("Calculate", 168, 50, 123, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $out = GUICtrlRead ($Input1) + GUICtrlRead ($Input2)
            Switch GUICtrlRead ($Combo1)
                Case "1"
                    GUICtrlSetData ($Input3, $out + GUICtrlRead ($Input3))
                Case "2"
                    GUICtrlSetData ($Input4, $out + GUICtrlRead ($Input4))
                Case "3"
                    GUICtrlSetData ($Input5, $out + GUICtrlRead ($Input5))
                Case "4"
                    GUICtrlSetData ($Input6, $out + GUICtrlRead ($Input6))
            EndSwitch
    EndSwitch
WEnd

Cheers,

Brett

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

woot woot thank you guys very much its working :)

only question i have is where do i put the code that ascendant posted?

If not StringRegExp(GUICtrlRead($Input1),"\A\d+\z",0) Then ConsoleWrite("NOT A NUMBER!" & @CRLF)
Edited by darkxraver

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>

$Form1 = GUICreate("Form1", 429, 127, 193, 125)
$Input1 = GUICtrlCreateInput("", 16, 24, 121, 21)
$Label1 = GUICtrlCreateLabel("+", 144, 26, 10, 17)
$Input2 = GUICtrlCreateInput("", 160, 24, 121, 21)
$Label2 = GUICtrlCreateLabel("=", 288, 26, 10, 17)
$Input3 = GUICtrlCreateInput("", 304, 24, 121, 21)
$Input4 = GUICtrlCreateInput("", 304, 48, 121, 21)
$Input5 = GUICtrlCreateInput("", 304, 72, 121, 21)
$Input6 = GUICtrlCreateInput("", 304, 96, 121, 21)
$Combo1 = GUICtrlCreateCombo("1", 16, 56, 145, 25)
GUICtrlSetData (-1, "2|3|4")
$Button1 = GUICtrlCreateButton("Calculate", 168, 50, 123, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $i1 = StringRegExp (GUICtrlRead($Input1), "[0-9]", 0)
            If $i1 Then $i1 = GUICtrlRead ($Input1)
            $i2 = StringRegExp (GUICtrlRead($Input1), "[0-9]")
            If $i2 Then $i2 = GUICtrlRead ($Input2)
            $out = $i1 + $i2
            Switch GUICtrlRead ($Combo1)
                Case "1"
                    GUICtrlSetData ($Input3, $out + GUICtrlRead ($Input3))
                Case "2"
                    GUICtrlSetData ($Input4, $out + GUICtrlRead ($Input4))
                Case "3"
                    GUICtrlSetData ($Input5, $out + GUICtrlRead ($Input5))
                Case "4"
                    GUICtrlSetData ($Input6, $out + GUICtrlRead ($Input6))
            EndSwitch
    EndSwitch
WEnd

Cheers,

Brett

Share this post


Link to post
Share on other sites

Boy I guess we're all bored tonight (or tomorrow in Aussie! =)

BrettF's test will unfortunately ring true if there are any numbers in the string (rings true for 'asdlkj1sdkj'). Change it to read like mine ("\A\d+\z"), or if you prefer, change his to ("\A[0-9]+\z"), and the test will only ring true for complete # strings.

I would also do something like this if it was false (contains non-digits):

If Not StringRegExp(GUICtrlRead($Input1),"\A\d+\z",0)
    MsgBox(48,"Invalid Input Type","Please enter only #'s in the boxe(s) provided")
    GUICtrlSetData($Input1,"")
    ContinueLoop
Endif

Share this post


Link to post
Share on other sites

Brett, yeah because you are only testing for the existence of that group anywhere in the string. By surrounding it with \A (beginning of string) and \z (end of string) and adding + (1 or more occurrences), you ensure that nothing else can be on the line. :)

Share this post


Link to post
Share on other sites

I somewhat understand what you are doing, but everything I chuck at that input that is not 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9, it makes it 0, and continues... But that extra checking can't hurt... can it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0