Jump to content

limit max characters per line in a multiline input box


 Share

Recommended Posts

Hello everybody!

This is my first question in this forum, I have already got many good ideas from here:)

So here is my little code:

$data = GUICreate("data", 500, 500)
GUICtrlCreateLabel("Sample info:", 50, 230,100)
$infoinput = GUICtrlCreateInput("", 50, 380,330,100,BitOR($ES_MULTILINE,$ES_WANTRETURN))

GUICtrlSetLimit($infoinput, 55) ;limits all characters, I want to limit it for every line
    
GUISetState(@SW_SHOW) 
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
                    ExitLoop
        EndSwitch
    WEnd

So I want many lines, but max 55 characters in any line.

THx for any ideas

Link to comment
Share on other sites

This is like my little input validation technique I came up with on accident that actually works well.

Never tried to limit by character number before but it seems that this works.

I am limiting to 54 characters as 55 causes a new line with the size of the GUI in your example.

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

$data = GUICreate("data", 500, 500)
GUICtrlCreateLabel("Sample info:", 50, 230,100)
$infoinput = GUICtrlCreateInput("", 50, 380,330,100,BitOR($ES_MULTILINE,$ES_WANTRETURN))


GUISetState(@SW_SHOW)

While 1
    ;Small Delay in Loop for CPU Cycles
    Sleep(10)
    ;Read Current GUI Data
    $sI1 = GUICtrlRead($infoinput)
    ;Replace Any Character Combination (Except New Line) that is Exactly 54 Long Followed By Any Other Character with the first 54 Characters.
    $Validate1 = StringRegExpReplace($sI1, "(.{54})(.)", "$1")
    ;Write New Data
    GUICtrlSetData($infoinput, $Validate1)

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
WEnd

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

ViciousXUSMC,
Your example is horrifying  :D
Such things should never be done inside a While loop
And it doesn't really answer to the OP's question which was about 'many lines'
Please try this one - far from perfect but even so a little better

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

$data = GUICreate("data", 500, 500)
GUICtrlCreateLabel("Sample info:", 50, 230,100)
$input = GUICtrlCreateEdit("", 50, 380,330,100,BitOR($ES_MULTILINE,$ES_WANTRETURN))
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')

Global $count = 20

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


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 Local Static $n0
 Local $id = BitAND($wParam, 0x0000FFFF)
 Local $code = BitShift($wParam, 16)
   If $id = $input AND $code = $EN_UPDATE Then
       Local $content = GUICtrlRead($input)
       Local $n1 = StringLen($content)
       If $n1 > $n0 Then GUICtrlSetData($input, StringRegExpReplace($content, '(?<=.{' & $count & '}(?=$))', @crlf))
       $n0 = $n1
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

It was just an example :) I actually have a function I use to prevent the flicker, but in your case you can simulate what I do with the function by adding an if statement

StringRegExpReplace()
If @Extended = 0 Then
;Do Not Update
Else
;Run the GUIData Update
EndIf

The good thing about mine is it is not limited to anything in particular you get regex for any kind of validation ( such as limit what characters you can use)  let me see if I can find the function version of it that i use maybe I can put it in the examples section. 

 

Edit Here You Go I like this function since it makes my code much neater if I need to have input validation on more than one input.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 618, 276, 192, 124)
$Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17)
$Input1 = GUICtrlCreateInput("", 40, 72, 553, 21)
$Input2 = GUICtrlCreateInput("", 40, 97, 553, 21)
$Edit1 = GUICtrlCreateEdit("", 40, 120, 553, 105)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
;Example 1 Limit to 10 Characters
 _GUIRegExValidate($Input1, "(.{10})(.)", "$1")
;Example 2 Can only type numbers
 _GUIRegExValidate($Input2, "[^\d]", "")
;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line
 _GUIRegExValidate($Edit1, "(.{10})(.)", "$1" & @CRLF)
 _GUIRegExValidate($Edit1, "[^\d\r\n]", "")
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _GUIRegExValidate($sInputLabel, $sRegExCapture, $sRegExReplace)
    $Step1Read = GUICtrlRead($sInputLabel)
    $Step2Validate = StringRegExpReplace($Step1Read, $sRegExCapture, $sRegExReplace)
    If @Extended = 0 Then Return
    GUICtrlSetData($sInputLabel, $Step2Validate)
EndFunc

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

@mikell good idea.  Not too familiar with using that have only done it once or twice for other reasons.

I do not understand how the 4 variables used in the WM Function are resolving when they have not been declared but this is working fine.

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

$Form1 = GUICreate("Test GUI", 618, 276, 192, 124)
$Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17)
$Button1 = GUICtrlCreateButton("Toggle Validation", 290, 24, 110, 40)
$Input1 = GUICtrlCreateInput("Max 10 Characters", 40, 72, 553, 21)
$Input2 = GUICtrlCreateInput("Numb3rs 0nly", 40, 97, 553, 21)
$Edit1 = GUICtrlCreateEdit("Max 10 Chars Numbers Only + Auto Line Break", 40, 120, 553, 105)
GUISetState(@SW_SHOW)

$iToggle = 1

GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')

While 1
    Sleep(10)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $iToggle = $iToggle + 1
    EndSwitch
WEnd

Func _GUIRegExValidate($sInputLabel, $sRegExCapture, $sRegExReplace)
    $Step1Read = GUICtrlRead($sInputLabel)
    $Step2Validate = StringRegExpReplace($Step1Read, $sRegExCapture, $sRegExReplace)
    If @Extended = 0 Then Return
    GUICtrlSetData($sInputLabel, $Step2Validate)
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
If Mod($iToggle, 2) = 0 Then
;Example 1 Limit to 10 Characters
 _GUIRegExValidate($Input1, "(.{10})(.)", "$1")
;Example 2 Can only type numbers
 _GUIRegExValidate($Input2, "[^\d]", "")
;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line
 _GUIRegExValidate($Edit1, "(.{10})(.)", "$1" & @CRLF)
 _GUIRegExValidate($Edit1, "[^\d\r\n]", "")
 EndIf
EndFunc

 

Link to comment
Share on other sites

The windows message function doesn't require any parameters, the ones that are used in the function header would only be needed if you're actually using some kind of information from the windows message handler, in this case you're not so you don't even need them. You can't use more than 4, but you can use any number of parameters from 0-4 depending on what information you need to process.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

May I add, in this case it should be used with at least 3 parameters to allow the use of $wParam which contains information about the control in use and the message sent
For more details about this usage have a look at the examples for _GUICtrlEdit_Create in the helpfile (and see how I used it in my previous code)

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...