Sign in to follow this  
Followers 0
StungStang

Bug? See this function

7 posts in this topic

I have write a script to compare 2 number. If the first number is higher than the second *Do What You Want*, but if the first number is smaller than the second *Do not continue".

All work good, but if i have

n1 = 100 and n2= x (where x is any number smaller than 100)

It's say to me that n2 is high than first!

See an example foto :

Posted Image

As you can see 100 is higher than 3, but for autoit not :huh2:

This is the code :

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1_1 = GUICreate("Number Compare", 194, 113, 454, 418)
$Label2 = GUICtrlCreateLabel("First:", 8, 48, 31, 17)
$FirstNumber = GUICtrlCreateInput("", 56, 48, 41, 21)
$Label3 = GUICtrlCreateLabel("Second:", 112, 48, 27, 17)
$SecondNumber = GUICtrlCreateInput("", 144, 48, 33, 21)
$Compare = GUICtrlCreateButton("Compare", 64, 80, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Compare
            If GUICtrlRead($FirstNumber) > GUICtrlRead($SecondNumber) Then
                MsgBox (0,"Ok","Your first number is higher than second")
            Else
                MsgBox (0,"Error","First number should be higher than second!")
            EndIf

    EndSwitch
WEnd

Where is the error?

Hi!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you are evaluating strings, when you want to perform the action on numbers

Case $Compare
            $numFirst = GUICtrlRead($FirstNumber)
            $numSecond = GUICtrlRead($SecondNumber)
            If  number($numFirst) > number($numSecond) Then
                MsgBox (0,"Ok","Your first number is higher than second")
            Else
                MsgBox (0,"Error","First number should be higher than second!")
            EndIf
Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Bug? No!

According to the help file GUICtrlRead returns "The text entered". Which means a string is returned. If you compare string "100" with string "3" then the latter is "higher".

Convert the strings to numbers and then do the compare :

If Number(GUICtrlRead($FirstNumber)) > Number(GUICtrlRead($SecondNumber)) Then ...

Make sure that the user enters valid numeric data.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Ok, thanks...

Yes i set the imputbox as number only...

There is a way to set the range of number that the user can write (0 to 100)?

Thanks :huh2:

Share this post


Link to post
Share on other sites

There is a way to set the range of number that the user can write (0 to 100)?

No, you have to check it yourself. You can only limit the number of characters a user can enter with GUICtrlSetLimit.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Ok...no realtime check :huh2:

Thanks

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Ok...no realtime check :huh2:

Thanks

O you can check!

See comments in script:(Just a demo)

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $number[2]
$Form1_1 = GUICreate('Number Compare', 194, 113, 454, 418)
$label1 = GUICtrlCreateLabel('- - - - - - - - - - - - - - - - - - - - - - - - - - - - -', 10, 10, 174, 27)
$Label2 = GUICtrlCreateLabel('First:', 8, 48, 31, 17)
$number[0] = GUICtrlCreateInput('0', 56, 48, 41, 21, $ES_NUMBER);<<<< ==== This helps
GUICtrlSetLimit(-1, 3);<<<< ==== Limit to Maximum three Characters
$Label3 = GUICtrlCreateLabel('Second:', 110, 48, 27, 17)
$number[1] = GUICtrlCreateInput('0', 144, 48, 33, 21, $ES_NUMBER);<<<< ==== This helps
GUICtrlSetLimit(-1, 3, 1)
$Compare = GUICtrlCreateButton('Compare', 64, 80, 75, 25, $WS_GROUP)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND");<<< === See "Help" for detail === >>>
GUISetState(@SW_SHOW)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Compare
            If Number(GUICtrlRead($number[0])) > Number(GUICtrlRead($number[1])) Then
                MsgBox(0, 'Ok', 'Your first number is higher than second')
            Else
                MsgBox(0, 'Error', 'First number should be higher than second!')
            EndIf
        Case $number[0], $number[1]
            If Number(GUICtrlRead($number[0])) > 100 Or Number(GUICtrlRead($number[0])) < 1 Then
                MsgBox(0, 'Attention:', 'Remember' & @CRLF & 'Only numbers between one and One Hundered allowed', 1)
                GUICtrlSetData($number[0], '')
                GUICtrlSetData($number[1], '')
                GUICtrlSetData($label1, 'Only numbers between 1 and 100 allowed!!!')
                WinActivate("Number")
            Else
                GUICtrlSetData($label1, '')
            EndIf
    EndSwitch
WEnd
Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam);<<< === See "Help" for detail === >>>
    Local $iCode = BitShift($wParam, 16)
    If $iCode = $EN_CHANGE Then
        For $i = 0 To 1
            If Number(GUICtrlRead($number[$i])) > 100 Then
                MsgBox(0, 'Attention:', 'Only numbers between one and One Hundered allowed', 1)
                GUICtrlSetData($number[$i], '')
                GUICtrlSetData($label1, 'Only numbers between 1 and 100 allowed!!!')
            EndIf
            GUICtrlSetData($label1, '')
        Next
    EndIf
EndFunc   ;==>MY_WM_COMMAND

Added some coments!

Edited by JoHanatCent

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