Jump to content

CharLength


sandman
 Share

Recommended Posts

Behold.. CharLength! (short for Character Length.) I'll say what it does as simply as it is.. it measures how many characters are in the Edit control. You can set a custom limit and whether or not to disable the Edit control once the limit is reached. I guess this could be useful, if you are using an SMS program on your computer and want to know how much it would cost the recipient.

Anyway, the very short code..

#include <GUIConstants.au3>

$win = GUICreate("CharLength", 629, 533, 202, 176);
$edit = GUICtrlCreateEdit("", 8, 16, 609, 481);
$curchar = GUICtrlCreateInput("", 24, 504, 73, 21);
$lbl = GUICtrlCreateLabel("/", 104, 507, 9, 17);
$limchar = GUICtrlCreateInput("Unlimited", 120, 504, 73, 21);
$disable = GUICtrlCreateCheckbox("Disable typing when limit is reached", 208, 508, 193, 17);
GUICtrlSetState(-1, $GUI_CHECKED);
GUISetState(@SW_SHOW);

While 1
    $nMsg = GUIGetMsg();
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit;
    EndSwitch
    $redit = GUICtrlRead($edit);
    $len = StringLen($redit);
    GUICtrlSetData($curchar, $len);
    If $len >= GUICtrlRead($limchar) And GUICtrlRead($disable) = $GUI_CHECKED And GUICtrlRead($limchar) <> "Unlimited" Then
        If GUICtrlGetState($edit) <> $GUI_DISABLE Then
            GUICtrlSetState($edit, $GUI_DISABLE);
            GUICtrlSetBkColor($curchar, 0xFF0000);
        EndIf
    ElseIf $len >= GUICtrlRead($limchar) And GUICtrlRead($limchar) <> "Unlimited" Then
        GUICtrlSetBkColor($curchar, 0xFF0000);
    ElseIf GUICtrlGetState($edit) = $GUI_DISABLE And $len < GUICtrlRead($limchar) Then
        GUICtrlSetState($edit, $GUI_ENABLE);
    EndIf
WEnd

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Link to comment
Share on other sites

Why not just do:

$ReadEdit = GuiCtrlRead($Edit)
$GetChar = StringLen($ReadEdit)
MsgBox(0, "", $Getchar)
Because that would be extremely annoying. You wouldn't be able to type with a messagebox taking the focus every 5 or so milliseconds.

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Link to comment
Share on other sites

In your version, a user could only type so many characters but could not edit the message once he had reached the amount of allowed characters. In my version, you cannot type more then the maximum allowed characters, so you can edit and remove parts of your message.

#include <GUIConstants.au3>

Dim $sMaxChar = 55, $sPrevLenght

$GUI = GUICreate("CharLength", 629, 533, 202, 176)
$gEdit = GUICtrlCreateEdit("", 8, 16, 609, 481)
GUICtrlSetBkColor($gEdit, 0xFFFFFF)
$gCharLabel = GUICtrlCreateInput("", 24, 504, 73, 21)
GUICtrlCreateLabel("/", 104, 507, 9, 17)
GUICtrlCreateInput($sMaxChar, 120, 504, 73, 21)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    $sRead = GUICtrlRead($gEdit)
    $sLenght = StringLen($sRead)
    If $sLenght <> $sPrevLenght Then
        GUICtrlSetData($gCharLabel, $sLenght)
        $sPrevLenght = $sLenght
    EndIf
    If $sLenght > $sMaxChar Then
        GUICtrlSetData($gEdit,StringTrimRight($sRead,$sLenght-$sMaxChar))
    EndIf
WEnd
Edited by Manadar
Link to comment
Share on other sites

If you uncheck the "Disable.." box, though, it'll let you edit it again.

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

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