Jump to content

Simple question from a beginner


tykkimies
 Share

Recommended Posts

Im trying to just make a simple calculator with a GUI. I got the GUI setup, but when i click for example 1 i want a 1 to show up in the text property of my input box. What can i use to change the text property of something like that? I tried jsut simply doing $Input1 = 1 but no luck. I know in Visual basic you could do things like Input1.text = 1, but doesn't seem like i can do that here. Thanks for all help.

EDIT: Ignore previous part i figured that out. But now i have a question as to actual functionality. May need to move this into the general help forum.

I have the calculator working, But suppose i do a problem like this. 5 + 5 = 10. And that is what ends up being in the input box. If i wanted to then take that 10 and start a new problem how would i go about doing that.

Example. 5 + 5 = 10 + 2 = 12

This is my code which i found some of online. I know my GUI buttons are all out of order, but this was just a test and not too worried about that.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=g:\autoitcalc.kxf
$Form1_1 = GUICreate("Form1", 196, 245, 191, 120)
$Input1 = GUICtrlCreateInput("", 8, 16, 185, 21, BitOr($ES_RIGHT, $GUI_SS_DEFAULT_INPUT))
$Button2 = GUICtrlCreateButton("Clear", 8, 192, 33, 33)
$Button3 = GUICtrlCreateButton("1", 8, 72, 34, 33)
$Button4 = GUICtrlCreateButton("4", 8, 112, 34, 33)
$Button5 = GUICtrlCreateButton("9", 88, 152, 34, 33)
$Button6 = GUICtrlCreateButton("8", 48, 152, 34, 33)
$Button7 = GUICtrlCreateButton("0", 48, 192, 34, 33)
$Button8 = GUICtrlCreateButton("7", 8, 152, 34, 33)
$Button9 = GUICtrlCreateButton("5", 48, 112, 34, 33)
$Button10 = GUICtrlCreateButton("3", 88, 72, 34, 33)
$Button11 = GUICtrlCreateButton("2", 48, 72, 34, 33)
$Button12 = GUICtrlCreateButton("6", 88, 112, 34, 33)
$Button1 = GUICtrlCreateButton("=", 88, 192, 35, 33)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Button13 = GUICtrlCreateButton("/", 144, 72, 41, 33)
$Button14 = GUICtrlCreateButton("*", 144, 112, 41, 33)
$Button15 = GUICtrlCreateButton("+", 144, 152, 41, 33)
$Button16 = GUICtrlCreateButton("-", 144, 192, 41, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Main()
Func Main()
While 1
$nMsg = GUIGetMsg()
    Switch $nMsg
   Case -3
   Exit
   Case $Button7
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "0")
   Case $Button3
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "1")
   Case $Button11
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "2")
   Case $Button10
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "3")
   Case $Button4
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "4")
   Case $Button9
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "5")
   Case $Button12
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "6")
   Case $Button8
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "7")
   Case $Button6
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "8")
   Case $Button5
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & "9")
   Case $Button2
    GUICtrlSetData($Input1, "")
   Case $Button15
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & " + ")
   Case $Button16
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & " - ")
   Case $Button13
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & " / ")
   Case $Button14
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & " * ")
   Case $Button1
    $Ans = Execute(GUICtrlRead($Input1))
    GUICtrlSetData($Input1, GUICtrlRead($Input1) & " = " &$Ans)
   EndSwitch
  
   WEnd
EndFunc
Edited by tykkimies
Link to comment
Share on other sites

you can use switch statements to simplify

and a variable to store the answer to be executed in the following commands

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

this probably works

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1_1 = GUICreate("Form1", 196, 245, 191, 120)

$Input1 = GUICtrlCreateInput("", 8, 16, 185, 21, BitOR($ES_RIGHT, $GUI_SS_DEFAULT_INPUT))

$Button_Clear = GUICtrlCreateButton("Clear", 8, 192, 33, 33)

GUICtrlCreateButton("0", 48, 192, 34, 33)
GUICtrlCreateButton("1", 8, 72, 34, 33)
GUICtrlCreateButton("2", 48, 72, 34, 33)
GUICtrlCreateButton("3", 88, 72, 34, 33)
GUICtrlCreateButton("4", 8, 112, 34, 33)
GUICtrlCreateButton("5", 48, 112, 34, 33)
GUICtrlCreateButton("6", 88, 112, 34, 33)
GUICtrlCreateButton("7", 8, 152, 34, 33)
GUICtrlCreateButton("8", 48, 152, 34, 33)
GUICtrlCreateButton("9", 88, 152, 34, 33)

$Button_Equal = GUICtrlCreateButton("=", 88, 192, 35, 33)

;GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Button13 = GUICtrlCreateButton("/", 144, 72, 41, 33)
$Button14 = GUICtrlCreateButton("*", 144, 112, 41, 33)
$Button15 = GUICtrlCreateButton("+", 144, 152, 41, 33)
$Button16 = GUICtrlCreateButton("-", 144, 192, 41, 33)

GUISetState(@SW_SHOW)

Local $nAnswered
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button_Clear + 1 To $Button_Equal - 1
If $nAnswered Then
GUICtrlSetData($Input1, '')
$nAnswered = False
EndIf

GUICtrlSetData($Input1, GUICtrlRead($Input1) & GUICtrlRead($nMsg))

Case $Button_Clear
$nAnswered = False
GUICtrlSetData($Input1, "")

Case $Button_Equal
If $nAnswered Then ContinueLoop
$Ans = GUICtrlSetData($Input1, GUICtrlRead($Input1) & '=' & Execute(GUICtrlRead($Input1)))
$nAnswered = True

Case $Button_Equal + 1 To $Button_Equal + 4
If $nAnswered Then
$Data = GUICtrlRead($Input1)
GUICtrlSetData($Input1, '')
$iRead = StringMid($Data, StringInStr($Data, '=', 2, -1) + 1)
GUICtrlSetData($Input1, $iRead & GUICtrlRead($nMsg))
$nAnswered=False
ContinueLoop
EndIf

GUICtrlSetData($Input1, GUICtrlRead($Input1) & GUICtrlRead($nMsg))
EndSwitch
WEnd

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

oh thats very nice, exactly what i wanted. But could i ask a question about soemthing i don't understand.

What does the + 1 and -1 do in this? Case $Button_Clear + 1 To $Button_Equal - 1

Its also there for the equals button like this Case $Button_Equal + 1 To $Button_Equal + 4. Im not understanding what those do. Thanks for your help.

Link to comment
Share on other sites

Control IDs are just the numbers assigned to the controls

the message loop fires the same number when the control is clicked or pressed

Hence Case $Button_Clear + 1 To $Button_Equal - 1

refers all the controls after $Button_Clear and before $Button_Equal

It helps in reducing the usage of variables,

in my example if you eliminate $Button13, 14, 15 and 16,

then too the example works

please ask if any questions :graduated:

Br,

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...