Jump to content

Numbers and Functions Only


Recommended Posts

Hey,

I am wandering how I would only let my input allow numbers: 1234567890 and functions: +-*/ only.

I tried:

Func Acceptions($io_control)
      $io_read = GuiCtrlRead($io_control)
      If StringIsAlpha($io_read) Then
             GuiCtrlSetTip($io_control, "1234567890 and +-*/ only!")
      EndIf
EndFunc

I also tried using $ES_Numbers but that wont allow me to us +-*/

Any thoughts?

-James

Link to comment
Share on other sites

Here's an idea:

$test = "23456-"
MsgBox(0, "Test", $test & " returns " & _IsNumAndSymbols($test))

Func _IsNumAndSymbols($strInput)
    $aInput = StringSplit($strInput, "")
    For $i = 1 to $aInput[0] 
        If Not StringInStr("1234567890+-*", $aInput[$i]) Then
            Return False
        EndIf
    Next
    Return True
EndFunc
BlueBearrOddly enough, this is what I do for fun.
Link to comment
Share on other sites

Based on your example in your OP, the following should work:

$io_read = GuiCtrlRead($io_control)
If Not _IsNumAndSymbols($io_read) Then GuiCtrlSetTip($io_control, "1234567890 and +-*/ only!")

Func _IsNumAndSymbols($strInput)
    $aInput = StringSplit($strInput, "")
    For $i = 1 to $aInput[0]
        If Not StringInStr("1234567890+-*", $aInput[$i]) Then
            Return False
        EndIf
    Next
    Return True
EndFuncoÝ÷ ØêÞ½éÚºÚ"µÍÌÍÚ[×ÜXYHÝZPÝXY
    ÌÍÚ[×ØÛÛÛ
BYÝ[ÔYÑ^
    ÌÍÚ[×ÜXY   ][ÝÖ×NIÌLËIÌLÊÉÌLÊÌLË×I][ÝÊH ÉÝÈ[ÝZPÝÙ]
    ÌÍÚ[×ØÛÛÛ   ][ÝÌLÍ
MÎL[
ËJÈÛIÌÌÎÉ][ÝÊ

What is failing? Are you getting an error?

BlueBearrOddly enough, this is what I do for fun.
Link to comment
Share on other sites

Is this kind of what you are trying to do?

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Test", 410, 193, 193, 115)
$Label1 = GUICtrlCreateLabel("Enter data:", 32, 48, 56, 17)
$io_control = GUICtrlCreateInput("", 104, 48, 201, 21)
$Button1 = GUICtrlCreateButton("OK", 168, 136, 65, 25, $BS_DEFPUSHBUTTON)
$Label2 = GUICtrlCreateLabel("", 104, 88, 235, 41)
GUICtrlSetColor ( -1, 0xff0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If _GoodData() Then ExitLoop
        Case $Button1
            If _GoodData() Then ExitLoop

    EndSwitch
WEnd

GUIDelete($Form1)
MsgBox(0, "End", "End of script")

Func _GoodData()
    $io_read = GuiCtrlRead($io_control)
    If $io_read = "" Or StringRegExp( $io_read, "[^0-9\-\+\*\/]") <> 0 Then 
        GuiCtrlSetTip($io_control, "1234567890 and +-*/ only!")
        GUICtrlSetData($Label2, "1234567890 and +-*/ only!")
        Return False
    Else
        GUICtrlSetData($Label2, "")
        Return True
    EndIf
    
EndFunc
BlueBearrOddly enough, this is what I do for fun.
Link to comment
Share on other sites

#include <GUIConstants.au3>

GUICreate('')

$input = GUICtrlCreateInput('', 10, 10, 100, 20)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    _checkinput()
WEnd

Func _checkinput()
    Local $filter = '1234567890/*+-', $str = GUICtrlRead($input), $split
    If $str <> '' Then
        $split = StringSplit($str, '')
        If Not StringInStr($filter, $split[$split[0]]) Then GUICtrlSetData($input, StringTrimRight($str, 1))
    EndIf
EndFunc

Edited by xcal
Link to comment
Share on other sites

#include <GUIConstants.au3>

GUICreate('')

$input = GUICtrlCreateInput('', 10, 10, 100, 20,Bitor($GUI_SS_DEFAULT_INPUT ,$ES_NUMBER))

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
WEnd

I also tried using $ES_Numbers but that wont allow me to us +-*/

Link to comment
Share on other sites

My way is kind of hackerish, though. I did a little searching on "masked edit controls" and did find some info, but couldn't figure how to translate it in to something Autoit would understand. Maybe someone with some more brains can figure out the proper way.

Don't go that way. Your way is perfect, and is commonly used by a lot of programs.

Also, I made this a lot faster using a regular expression.

#include <GUIConstants.au3>

GUICreate('')

$input = GUICtrlCreateInput('', 10, 10, 100, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    _checkinput()
WEnd

Func _checkinput()
    Local $str = GUICtrlRead($input)
    $strmod = StringRegExpReplace( $str, "[^0-9\-\+\*\/]","")
    If $str <> $strmod Then GUICtrlSetData($input,$strmod)
EndFunc
Edited by Manadar
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...