Jump to content

Real-time Line Counter


 Share

Go to solution Solved by Melba23,

Recommended Posts

I have an edit box that I wanted to keep tabs of the line number in real-time.  For an example, if a user enter 3 lines, the counter should update as soon as the user hit "Enter" after the 3rd entry.  I managed to do it but it's not real-time.  Program would hung or slow to response after I hit the "Configuration" button.

Many thanks!

; Testing Host Count

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <StaticConstants.au3>

Example()

Func Example()
    Local $hostLabel, $hostEdit, $button, $msg, $count, $hostname
    GUICreate("Host Count") ; will create a dialog box that when displayed is centered

    $hostLabel = GUICtrlCreateLabel ("Hostname", 10, 10, 120, 20)
    $hostEdit = GUICtrlCreateEdit ("", 10, 40, 200, 80)
    $button = GUICtrlCreateButton ("Configure", 20, 140, 120, 30)

    GUISetState() ; will display an  dialog box with 1 checkbox

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        $hostname = StringRegExp (GUICtrlRead ($hostEdit), '\V\S+', 3)
        $count = UBound ($hostname)
        sleep (500)
        GUICtrlCreateLabel ($count, 200, 10, 10, 20, $SS_RIGHT)
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $button
                MsgBox (0, "Count", "The count is " & $count)
                #cs
                $hostname = StringRegExp (GUICtrlRead ($hostEdit), '\V\S+', 3)
                $count = UBound ($hostname)
                GUICtrlCreateLabel ($count, 200, 10, 10, 20, $SS_RIGHT)
                #ce
        EndSelect
    WEnd
EndFunc   ;==>Example
Link to comment
Share on other sites

This is the best I can do.  I'm sure there is a better way, so stick around.  I'm *sure* that someone will see my post and be inspired to do a better job.  8)

; Testing Host Count

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <StaticConstants.au3>

Example()

Func Example()
    GUICreate("Host Count")

    Local Const $hostLabel = GUICtrlCreateLabel("Hostname", 10, 10, 120, 20)
        
    Local Const $label = GUICtrlCreateLabel('0', 200, 10, 30, 20, $SS_RIGHT)
    
    Local Const $hostEdit = GUICtrlCreateEdit("", 10, 40, 200, 80)
    
    Local Const $button = GUICtrlCreateButton("Configure", 20, 140, 120, 30)

    GUISetState(@SW_SHOWNORMAL)

    Local $count, $hostname
    
    Do              
        Switch GUIGetMsg()
            Case $button
                $hostname = StringRegExp(GUICtrlRead($hostEdit), "(*ANY)", $STR_REGEXPARRAYGLOBALMATCH)
                
                $count = (UBound($hostname) - 1) / 2
                
                GUICtrlSetData($label, $count)
                
            Case $GUI_EVENT_CLOSE
                ExitLoop                
        EndSwitch
    Until False
EndFunc
Link to comment
Share on other sites

Thank you for the quick response.  I tried running your code but came up with undeclared variable error:

C:\Users\nguyenk\Documents\AutoIT\RxG\test5.au3(20,91) : WARNING: $STR_REGEXPARRAYGLOBALMATCH: possibly used before declaration.
                $hostname = StringRegExp(GUICtrlRead($hostEdit), "(*ANY)", $STR_REGEXPARRAYGLOBALMATCH)
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\nguyenk\Documents\AutoIT\RxG\test5.au3(20,91) : ERROR: $STR_REGEXPARRAYGLOBALMATCH: undeclared global variable.
                $hostname = StringRegExp(GUICtrlRead($hostEdit), "(*ANY)", $STR_REGEXPARRAYGLOBALMATCH)
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\nguyenk\Documents\AutoIT\RxG\test5.au3 - 1 error(s), 1 warning(s)
!>18:06:06 AU3Check ended. Press F4 to jump to next error.rc:2
>Exit code: 2    Time: 0.517
Link to comment
Share on other sites

  • Moderators

jaberwacky & dreamzboy,

I would do it this way: ;)

Case $button
    $count = ControlCommand("Host Count", "", $hostEdit, "GetLineCount", "")
    GUICtrlSetData($label, $count)
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Maybe I'm missing something but that's not real-time.  The count only update once you hit the $button.  What I'm looking for is a line counter similar to the Scite Editor where it shows the number of lines at the bottom of the status bar as you type.  It should show the number of lines even before you hit the button.

My current code work great counting only the number of entries which is what I wanted instead of empty lines.  The problem is that it's not real-time.  It doesn't show the entry number until you hit the $button.

Case $msg = $button
                $hostname = StringRegExp (GUICtrlRead ($hostEdit), '\V\S+', 3)
                $count = UBound ($hostname)
                GUICtrlSetData ($countLabel, $count)

 counter.jpg

Edited by dreamzboy
Link to comment
Share on other sites

  • Moderators
  • Solution

dreamzboy,

Sorry about that - I based my response on jaberwacky's code. How about this? ;)

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <StaticConstants.au3>

Example()

Func Example()
    GUICreate("Host Count")

    Local $hostLabel = GUICtrlCreateLabel("Hostname", 10, 10, 120, 20)
    Local $label = GUICtrlCreateLabel('0', 200, 10, 30, 20, $SS_RIGHT)
    Local $hostEdit = GUICtrlCreateEdit("", 10, 40, 200, 80)
    Local $button = GUICtrlCreateButton("Configure", 20, 140, 120, 30)

    GUISetState()

    Local $count, $iCurrentCount = 0

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch

        ; Get current line count
        $count = ControlCommand("Host Count", "", $hostEdit, "GetLineCount", "")
        ; If not what is stored
        If $count <> $iCurrentCount Then
            ; Reset label
            GUICtrlSetData($label, $count)
            ; Store new value
            $iCurrentCount = $count
        EndIf

    WEnd

EndFunc   ;==>Example
Better? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

×
×
  • Create New...