Jump to content

How to write in two boxes and limit text in Edit


Go to solution Solved by Keketoco00,

Recommended Posts

Posted

Hello everyone!

I'm a beginner and I'm not very familiar with the commands yet.

I'm having trouble limiting the text in Edit.

I set the limit to 15 characters with the command GUICtrlSetLimit(-1, 15), but since I want the name to end with "-PC", the command GUICtrlSetLimit doesn't identify the "-PC" as typed and the limit is 18.

I can do this, but I don't think it's the right method.

I also believe that this method of typing in two boxes simultaneously is not the right one.

I would like it to only send the contents of $Edit2 to $Edit4 when I type, and not when I click with the cursor.

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

If $Edit2 = 24 Then
If  StringLen(GUICtrlRead($Edit2)) <= 12 Then ;Here I put $Edit2 because when I put $Edit4 <= 15 it types 16 characters
    GUICtrlSetData($Edit4, LimitCaracte($Edit2) & "-PC")
EndIf
EndIf

EndFunc

 

Posted (edited)

I always use this function RestrictControlRegExp

 

But the code is corrupt so here the files.

 

BR

funkey

RESTRICTCONTROLREGEXP_TEST.AU3

RestrictControlRegExp.au3

Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Posted (edited)

different approach

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPIDlg.au3>

_Main()

Func _Main()
    Local $hGUI = GUICreate("Example", 250, 100)
    GUICtrlCreateLabel("Type name here (max 12 chars)", 20, 20)
    Local $idEditName = GUICtrlCreateEdit("", 20, 40, 160, 20, $ES_AUTOHSCROLL)
    GUICtrlSetLimit($idEditName, 12)
    Local $idEditResult = GUICtrlCreateEdit("", 20, 65, 160, 20, $ES_READONLY, 0)
    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch

        ; Check if the active control is $idEditName
        If _WinAPI_GetDlgCtrlID(ControlGetHandle($hGUI, "", ControlGetFocus($hGUI))) = $idEditName Then
            Local $sValue = _InputLimiter($idEditName)
            If $sValue <> "" Then GUICtrlSetData($idEditResult, $sValue)
        EndIf
    WEnd
EndFunc

Func _InputLimiter($iCtrlInput, $iMaxBaseLength = 12, $sSuffix = "-PC")
    Local Static $sLastValue
    Local $sCurrentValue = GUICtrlRead($iCtrlInput)
    If $sCurrentValue <> $sLastValue Then
        $sLastValue = $sCurrentValue
        Return StringLeft($sCurrentValue, $iMaxBaseLength) & $sSuffix
    EndIf
    Return ""
EndFunc


Edit:
variant with more advanced limiter

Func _InputLimiter($iCtrlInput, $iMaxBaseLength = 12, $sSuffix = "-PC")
    Local Static $sLastValue
    Local $sCurrentValue = GUICtrlRead($iCtrlInput)
    Local $sFilteredValue = StringRegExpReplace($sCurrentValue, "[^a-zA-Z0-9]", "")

    If $sFilteredValue <> $sCurrentValue Then
        GUICtrlSetData($iCtrlInput, $sFilteredValue)
        Return ""
    EndIf

    If $sFilteredValue <> $sLastValue Then
        $sLastValue = $sFilteredValue ; Update static
        ConsoleWrite("$sCurrentValue=" & $sCurrentValue & @CRLF) ; For debugging
        Return StringLeft($sFilteredValue, $iMaxBaseLength) & $sSuffix
    EndIf

    Return ""
EndFunc

 

Edited by ioa747
variant with more advanced limiter

I know that I know nothing

Posted (edited)
9 hours ago, funkey said:

I always use this function RestrictControlRegExp

 

But the code is corrupt so here the files.

 

BR

funkey

RESTRICTCONTROLREGEXP_TEST.AU3 788 B · 4 downloads

RestrictControlRegExp.au3 5 kB · 5 downloads

funkey

I couldn't adapt this command to my script. However, ioa747 helped me with this. Anyway, I appreciate your help.

6 hours ago, ioa747 said:
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPIDlg.au3>

_Main()

Func _Main()
    Local $hGUI = GUICreate("Example", 250, 100)
    GUICtrlCreateLabel("Type name here (max 12 chars)", 20, 20)
    Local $idEditName = GUICtrlCreateEdit("", 20, 40, 160, 20, $ES_AUTOHSCROLL)
    GUICtrlSetLimit($idEditName, 12)
    Local $idEditResult = GUICtrlCreateEdit("", 20, 65, 160, 20, $ES_READONLY, 0)
    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch

        ; Check if the active control is $idEditName
        If _WinAPI_GetDlgCtrlID(ControlGetHandle($hGUI, "", ControlGetFocus($hGUI))) = $idEditName Then
            Local $sValue = _InputLimiter($idEditName)
            If $sValue <> "" Then GUICtrlSetData($idEditResult, $sValue)
        EndIf
    WEnd
EndFunc

Func _InputLimiter($iCtrlInput, $iMaxBaseLength = 12, $sSuffix = "-PC")
    Local Static $sLastValue
    Local $sCurrentValue = GUICtrlRead($iCtrlInput)
    If $sCurrentValue <> $sLastValue Then
        $sLastValue = $sCurrentValue
        Return StringLeft($sCurrentValue, $iMaxBaseLength) & $sSuffix
    EndIf
    Return ""
EndFunc

iao747

Thank you very much for your answer. It worked very well for me.
I adapted it to use WM_COMMAND. I don't know if it's a problem, but I prefer it this way.

6 hours ago, ioa747 said:
Func _InputLimiter($iCtrlInput, $iMaxBaseLength = 12, $sSuffix = "-PC")
    Local Static $sLastValue
    Local $sCurrentValue = GUICtrlRead($iCtrlInput)
    Local $sFilteredValue = StringRegExpReplace($sCurrentValue, "[^a-zA-Z0-9]", "")

    If $sFilteredValue <> $sCurrentValue Then
        GUICtrlSetData($iCtrlInput, $sFilteredValue)
        Return ""
    EndIf

    If $sFilteredValue <> $sLastValue Then
        $sLastValue = $sFilteredValue ; Update static
        ConsoleWrite("$sCurrentValue=" & $sCurrentValue & @CRLF) ; For debugging
        Return StringLeft($sFilteredValue, $iMaxBaseLength) & $sSuffix
    EndIf

    Return ""
EndFunc

Since I want to block special characters only when I type in the second edit, I chose to call StringRegExpReplace only for the second edit. This way, I can type "ä" in $Edit2 and "a" will appear in $Edit4.

Example:

Func _LimitCaracte($Caracte)

$string = GUICtrlRead($Caracte)

$string1 = StringRegExpReplace($string, "[ä]", "a")

Return $string1

EndFunc

 

Edited by Keketoco00
Posted (edited)
1 hour ago, argumentum said:

Explique-se.

I would like to know if there is a command that detects when I type an edit. Example: If "I typed in the Edit control", then

Edited by Keketoco00
  • Solution
Posted (edited)
1 hour ago, Nine said:

Claro, mas não está claro o que você quer fazer. Você precisa deixar sua solicitação clara o suficiente, caso contrário, simplesmente a rejeitaremos.

I don't know which words to use because the translator changes them.

I'm using the wm_command command to type two edits at the same time, but when I click on $Edit1, it automatically sends the text to $Edit2. And I would like it to send the text only after I edit something.

I think the EN_CHANGE command does this, but I didn't know how to use it in AutoIT.

The way iao747 sent it worked partially. If there is already some text in $Edit1, it automatically sends it to $Edit2. I hope I understood.

 

Edit: with a little research I ended up getting it.

If _WinAPI_HiWord($wParam) = $EN_CHANGE Then
GUICtrlSetData($Edit2, GUICtrlRead($Edit1))
EndIf

 

Edited by Keketoco00

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
  • Recently Browsing   0 members

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