DavidFigueroa

Automatically adding leading zero's to inputbox

3 posts in this topic

I'm trying to create an inputbox that automatically formats numbers with leading zeroes when the updown control is used.  I found an example from Melba23 (see below) that shows a step incremented inputbox, and based on that, I tried to create one that would work, but it does not.

I am creating separate inputs for hours and minutes, and I want to make sure the inputbox is always 2 digits. And right up front, I don't pretend to really understand the DLLStruct* calls.. 

Melba23's sample:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <UpDownConstants.au3>

$hGUI = GUICreate("Test", 500, 500)

$hInput = GUICtrlCreateInput("5000", 10, 10, 100, 20)
$hUpDown = GUICtrlCreateUpdown($hInput, BitOR($UDS_WRAP, $UDS_NOTHOUSANDS))
GUICtrlSetLimit($hUpDown, 6000, 5000)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    ; Is it from the UpDown?
    If BitAND($wParam, 0xFFFF) = $hUpDown Then
        ; Create NMUPDOWN structure
        Local $tStruct = DllStructCreate("hwnd;long;int;long;long", $lParam)
        ; Is it a change message?
        If DllStructGetData($tStruct, 3) = 0xFFFFFD2E Then ; $UDN_DELTAPOS
            ; Alter the change value
            DllStructSetData($tStruct, 5, 100 * DllStructGetData($tStruct, 5))
        EndIf
    EndIf

EndFunc
;~ The NMUPDOWN structure holds:

;~ 1 - Handle of UpDown
;~ 2 - ControlID of UpDown
;~ 3 - Message type sent by UpDown
;~ 4 - Current value of UpDown
;~ 5 - Change to apply to input (+/-1)

And here is my non-working sample (it runs, and the initial value shows correctly, but when you hit the updown, you lose the leading zeroes.

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

$hGUI = GUICreate("Window", 300, 100, -1, -1)

$Count = 1

Global $hEdit = GUICtrlCreateInput($Count, 15, 15, 100, 30)
GUICtrlSetFont(-1, 12, 400, "", "Tahoma")
GUICtrlSetData(-1, StringFormat("%03u",$Count))

Global $hUpDown = GUICtrlCreateUpdown(-1)
GUICtrlSetLimit(-1, 100, 0)


GUISetState()

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

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    ; Is it from the UpDown?
    If BitAND($wParam, 0xFFFF) = $hUpDown Then
        ; Create NMUPDOWN structure
        Local $tStruct = DllStructCreate("hwnd;long;int;long;long", $lParam)
        ; Is it a change message?
        If DllStructGetData($tStruct, 3) = 0xFFFFFD2E Then ; $UDN_DELTAPOS
            ; Alter the change value
            $ValueToSet = DllStructGetData($tStruct, 4)
            $ValueToSet = StringFormat("%03u", $ValueToSet)
            GUICtrlSetData($hEdit, $ValueToSet)
        EndIf
    EndIf

EndFunc

 

Share this post


Link to post
Share on other sites



DavidFigueroa,

Welcome to the AutoIt forums.

The example you found is not really relevant to what you want to do - what you need to do is look for a change to the input content and then pad it with a leading zero as required:

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

$hGUI = GUICreate("Test", 500, 500)

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)
$cUpDown = GUICtrlCreateUpdown($cInput)
GUICtrlSetLimit($cUpDown, 100, 0)

GUISetState()

; Register the WM_COMMAND message
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

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

Func _WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

     ; If it was an update message from our input
    If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $cInput Then

        ; Read the content
        $sContent = GUICtrlRead($cInput)
        ; Reset the content padded with zeroes
        GUICtrlSetData($cInput, StringFormat("%02i", $sContent))

    EndIf

EndFunc

Please ask if you have any questions.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Fantastic :)  

Thank you very much.. 

 

David F. 

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

  • Similar Content

    • akira2891
      By akira2891
      Hi,
      I was wondering is this all inputs can be done in 1 box with 3 fields and not to use another gui.
      And how can i validate data for example if i don't enter username he prompt me a message box all time until field is empty.
      I try like this but it give me a msgbox and continue on another field.
      $username = InputBox("Add new user", "Username", "", "", 200, 130, Default, Default, 0) if $username <> " " then msgbox(0, "", "Please enter username") endif $email = InputBox("Add new user", "Email", "", "", 200, 130, Default, Default, 0) $password = InputBox("Add new user", "Password", "", "*", 200, 130, Default, Default, 0)  
    • kneze
      By kneze
      Hi all,
      i would like to enter a vlaue to Inputbox 1 an press button1. Script should search value in a text file and put value after "; " to Inputbox 2.
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <file.au3> $Form1 = GUICreate("Form1", 257, 119, 192, 124) $Input1 = GUICtrlCreateInput("Input1", 15, 35, 121, 21) $Input2 = GUICtrlCreateInput("Input2", 15, 60, 121, 21) $Button1 = GUICtrlCreateButton("Button1", 145, 35, 75, 25) GUISetState(@SW_SHOW) $iLines = _FileCountLines(@ScriptDir & "\data.txt") ;MsgBox(0, "Char read:", $ilines) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 EndSwitch WEnd can anyone help?
      data.txt
    • GeorgeB
      By GeorgeB
      I'm having trouble in making an input box where I can limit the characters that can be inputted.  Anybody have an idea how this can be done in Auto IT?
      Specifically I would like to create an input field where the user would enter a MAC address.  What I would like to do is to limit the characters that can be inputted to only HEX values (A-F and 0-9 as well as the - character).  This is so that the user cannot input invalid characters. The program is going to be used by very non-technical people, so the idea is to remove as many chances for errors that we can think of.  I would also prefer to limit the field to only 17 characters, so that there is only enough space to enter the MAC address with the - character as the separator.
      Thank you all in advance for any suggestions!
    • farhadrahman2
      By farhadrahman2
      hey guys, i have a problem .
      I want to search a file in a particular folder and after inserting the name  in the input box i want to make a drop down menu of all the matches and then select it and open it.
      I need the full code .
      so please anyone help me on this one.

    • Quegga
      By Quegga
      Hey there
      I want to mark the content of an inputbox when you click on the text inside.
      Excample: I have an inputbox which has the data "a". When I click inside the inputbox I want the "a" to be highlighted blue, so I can easily copy-paste it out.
      GUICreate("",300,100) $Input1 = GUICtrlCreateInput("",10,10,50,30) $Input2 = GUICtrlCreateInput("a",100,10,50,30) GUISetState() While True Switch GUIGetMsg() Case -3 Exit EndSwitch WEnd I searched google and the forum and did not find anything.
      Thanks!