如何强迫用户输入东西?

时间:2018-06-26 07:33:04

标签: validation user-interface autoit

有没有一种方法可以迫使用户在使用GUICtrlCreateInput()按下OK按钮之前输入一些东西?就像在InputBox()中一样:

re.split(r' *[;\n] *', source_code)

我们使用字符InputBox("Sample", "Input Something:", "", " M", -1, -1) 。如果有,我该怎么办?

1 个答案:

答案 0 :(得分:0)

  

…强制用户输入某些内容,然后再按GUICtrlCreateInput中的“确定”按钮?

使用InputBox()的示例:

Global Const $g_sPromt   = 'Input something:'
Global Const $g_sChar    = '*'
Global Const $g_sDefault = 'something'
Global       $g_sInput   = ''

While Not $g_sInput Or $g_sInput = $g_sDefault

    $g_sInput = InputBox(@ScriptName, $g_sPromt, $g_sDefault, $g_sChar)

WEnd

ConsoleWrite($g_sInput & @CRLF)

使用GUICtrlCreateInput()的示例:

#include <GUIConstantsEx.au3>

Global Const $g_iGuiMargin  = 10
Global Const $g_iGuiSizeH   = $g_iGuiMargin * 2
Global Const $g_iGuiWidth   = 200
Global Const $g_iGuiHeight  = ($g_iGuiSizeH * 2) + ($g_iGuiMargin * 3)
Global Const $g_sInpText    = 'Input something:'
Global Const $g_sInpDefault = 'something'
Global Const $g_iInpLeft    = $g_iGuiMargin
Global Const $g_iInpTop     = $g_iInpLeft
Global Const $g_iInpWidth   = $g_iGuiWidth - ($g_iInpLeft * 2)
Global Const $g_iInpHeight  = $g_iGuiSizeH
Global Const $g_sBtnText    = 'OK'
Global Const $g_iBtnLeft    = $g_iInpLeft
Global Const $g_iBtnTop     = $g_iInpHeight + $g_iInpTop + $g_iGuiMargin
Global Const $g_iBtnWidth   = $g_iInpWidth
Global Const $g_iBtnHeight  = $g_iGuiSizeH

Global       $g_sInput      = ''
Global       $g_hGui
Global       $g_hInput
Global       $g_hButton

$g_hGui    = GUICreate(@ScriptName, $g_iGuiWidth, $g_iGuiHeight)
$g_hInput  = GUICtrlCreateInput($g_sInpText, $g_iInpLeft, $g_iInpTop, $g_iInpWidth, $g_iInpHeight)
$g_hButton = GUICtrlCreateButton($g_sBtnText, $g_iBtnLeft, $g_iBtnTop, $g_iBtnWidth, $g_iBtnHeight)
GUICtrlSetData($g_hInput, $g_sInpDefault)
GUISetState(@SW_SHOW, $g_hGui)

While True

    Switch GUIGetMsg()

        Case $GUI_EVENT_CLOSE
            Exit

        Case $g_hButton
            $g_sInput = GUICtrlRead($g_hInput)

            If Not $g_sInput Or $g_sInput = $g_sInpDefault Then

                SoundPlay(@WindowsDir & "\media\Windows Background.wav")
                GUICtrlSetData($g_hInput, $g_sInpDefault)
                ContinueLoop

            Else

                ConsoleWrite($g_sInput & @CRLF)
                GUIDelete($g_hGui)
                Exit

            EndIf

    EndSwitch

WEnd

如果有特定的验证要求,请使用StringRegExp()。仅接受数字输入(数字字符)的示例:

If Not $g_sInput Or $g_sInput = $g_sInpDefault Or Not StringRegExp($g_sInput, '^\d$') Then