czardas

Row Sum (Σ) Formula Validation

18 posts in this topic

#1 ·  Posted (edited)

Much work still needs to be done on my current project, but design concepts often benefit more from early criticism. Although there may be nothing particularly innovative about this project, user experience is everything. I would like you to look at the behaviour of the controls on this GUI. Unless you type something really awful, it allows you to finish typing the formula before testing the input. When typing into an edit control, pressing the enter key activates the okay button.

The program itself, mainly consists of a listview control, which you can't see - just imagine it looks like a stone age version of Excel with a nice paint job, but no bells or whistles. What Row Sum (Σ) is intended to do is loop through all the rows, calculate a sum and print the output in a newly created column. The user is prompted to type a formula into the following GUI. Please try to figure it out, and break it any way you can.

#include <GUIConstants.au3>
#include <GuiEdit.au3>
#include <Misc.au3>

Global $g_iAvailableCols = 24 ; arbitary dev variable [column count will eventually be read from a listview control]

; missing several thousand lines of code...

Formula()

; caution about including columns containing empty fields in the formula
; empty fields are treated as zero
; with addition and subtraction there is never any problem
; multiplication by an empty field (or by zero) returns zero
; division by an empty field (or by zero) returns an empty field [unless - see exception]
; use of the power operator is limited to rows in which all referenced fields contain numbers
; imaginary roots of negative numbers return an empty field [unless - see exception]
; exception: infinity (or an imaginary number) to the power of zero returns a meaningless number

Func FormulaSyntax()
    MsgBox(BitOR(64, 8192), "Row Sum (" & ChrW(0x03A3) & ") : Syntax To Use", _ ; $MB_ICONINFORMATION, $MB_TASKMODAL
    " mathematical operators :" & @TAB & "+ - * / ^" & @CRLF & _
    " column numbers :" & @TAB & @TAB & "c1, c2, c3  etc..." & @CRLF & _
    " decimal digits :" & @TAB & @TAB & "0 to 9 and ." & @CRLF & _
    " parenthesis :" & @TAB & @TAB & "( )" & @CRLF & _
    " minus sign :" & @TAB & @TAB & "-1, -c2, -(c3)  etc..." & @CRLF & _
    " example :" & @TAB & @TAB  & @TAB & "c1 +c2 -c3")
EndFunc ;==> FormulaSyntax

Func Formula() ; ($hParent, $idListView, $hListView) ; [missing parent window]
    Local $sTitle = "Row Sum (" & ChrW(0x03A3) & ")", _
    $iStyle = BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU), _
    $iExStyle = BitOR($WS_EX_MDICHILD, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST)

    Local $hChild = GUICreate($sTitle, 334 +25, 105 +1, Default + 100, Default + 100); , $iStyle, $iExStyle, $hParent)

    Local $hLabel = GUICtrlCreateLabel("Formula :", 8, 8, 55, 18)
    GUICtrlSetFont(-1, 10)

    Local $hSyntax = GUICtrlCreateButton("???", 72, 7, 40, 20)

    Local $hLabel = GUICtrlCreateLabel("Header :", 127, 8, 49, 18)
    GUICtrlSetFont(-1, 10)

    Local $hHeader = GUICtrlCreateInput("", 182, 7, 170, 20)
    GUICtrlSetFont(-1, 10)

    Local $hInput = GUICtrlCreateInput("", 7, 33, 320 +25, 40, BitOR($WS_TABSTOP, $ES_MULTILINE))
    GUICtrlSetFont(-1, 10)

    Local $hCheckBox = GUICtrlCreateCheckbox(" Round to", 7, 79, 75, 20)
    GUICtrlSetFont(-1, 10)

    Local $hPlaces = GUICtrlCreateInput("2", 86, 79 +1, 20, 20, BitOR($WS_TABSTOP, $ES_CENTER, $ES_NUMBER))
    GUICtrlCreateLabel("decimal places", 114 -2, 79 +2, 92, 18)
    GUICtrlSetFont(-1, 10)

    Local $hCancel = GUICtrlCreateButton("Cancel", 210, 79 +1, 66, 20)

    Local $hOkay = GUICtrlCreateButton("OK", 285, 79 +1, 66, 20)
    GUISetState(@SW_SHOW)

    Local $sInput = "", $aArray, $iError = 0, $iCols = $g_iAvailableCols ; $iCols = _GUICtrlListView_GetColumnCount($hListView), _ ; >>> CHANGE THIS LATER
    ;$iRows = _GUICtrlListView_GetItemCount($hListView), $aColOrder = _GUICtrlListView_GetColumnOrderArray($hListView)

    Local $vTemp, $msg2, $iPlaces = '2'
    While 1
        $msg2 = GUIGetMsg()
        If $msg2 = $hCancel Or $msg2 = $GUI_EVENT_CLOSE Then ExitLoop

        If $msg2 = $hSyntax Then
            FormulaSyntax()
            ContinueLoop
        EndIf

        $vTemp = GUICtrlRead($hPlaces)
        If $vTemp <> $iPlaces Then
            If $vTemp > 14 Then
                GUICtrlSetData($hPlaces, 14)
                $iPlaces = 14
                _GUICtrlEdit_SetSel($hPlaces, 0, -1)
            ElseIf Not $vTemp Then
                GUICtrlSetData($hPlaces ,'0')
                $iPlaces = '0'
                _GUICtrlEdit_SetSel($hPlaces, 0, -1)
            EndIf
        EndIf

        If BitAND(GUICtrlRead($hCheckBox), $GUI_CHECKED) == $GUI_CHECKED _
            And BitAND(GUICtrlGetState($hPlaces), $GUI_DISABLE) == $GUI_DISABLE Then
            GUICtrlSetState($hPlaces, $GUI_ENABLE)
        ElseIf BitAND(GUICtrlRead($hCheckBox), $GUI_CHECKED) <> $GUI_CHECKED _
            And BitAND(GUICtrlGetState($hPlaces), $GUI_ENABLE) == $GUI_ENABLE Then
            GUICtrlSetState($hPlaces, $GUI_DISABLE)
        EndIf

        $sFormula = GUICtrlRead($hInput)

        If $sFormula <> $sInput Then
            If StringRegExp($sFormula, '[^ ]') Then ; spaces between components are ignored

                If Not ValidSyntax($sFormula, $iCols) Then
                    If @extended < 5 Then
                        If $iError = 0 Then GUICtrlSetBkColor($hInput, 0xFFA090)
                        $iError = 1
                    ElseIf $iError Then
                        GUICtrlSetBkColor($hInput, 0xFFFFFF)
                        $iError = 0
                    EndIf

                ElseIf $iError Then
                    GUICtrlSetBkColor($hInput, 0xFFFFFF)
                    $iError = 0
                EndIf

            ElseIf $iError Then
                GUICtrlSetBkColor($hInput, 0xFFFFFF)
                $iError = 0
            EndIf
            $sInput = $sFormula
        EndIf

        ;  BitAND(WinGetState($hChild), $WIN_STATE_ACTIVE) = 8
        If $msg2 = $hOkay Or (_IsPressed("0D") And BitAND(WinGetState($hChild), 8) = 8 And (ControlGetFocus($hChild) = "Edit2" Or ControlGetFocus($hChild) = "Edit1")) Then
            $sFormula = GUICtrlRead($hInput) ;
            If StringRegExp($sFormula, '[^ ]') And ValidSyntax($sFormula, $iCols) Then
                ; missing formula execution code

                MsgBox(0, "VALID SYNTAX", "Well done, your formula can safely be executed, but" & @CRLF & "can't find parent window.")
                ExitLoop
            Else
                Switch @extended
                    Case 1
                        $vTemp = "The formula contains illegal characters."
                    Case 2
                        $vTemp = "No such column exists."
                    Case 3
                        $vTemp = "The formula contains a syntax error."
                    Case 4
                        $vTemp = "The formula is missing opening parenthesis."
                    Case 5
                        $vTemp = "The formula is unterminated"
                    case 6
                        $vTemp = "The formula is missing closing parenthesis."
                EndSwitch

                MsgBox(262160, "uh-uh!", StringRegExp($sFormula, '[^ ]') ? $vTemp : "Please enter a formula.")
                While _IsPressed("0D")
                    Sleep(50)
                WEnd
                ;$iError = 1
            EndIf
        EndIf
    WEnd
    GUIDelete($hChild)
EndFunc ; formula

Func ValidSyntax($sFormula, $iMax) ; [currently requires external check that string <> ""]
    ; quick test for accepted characters
    If StringRegExp($sFormula, '(?i)[^c \d\.\+\-\*/\^\(\)]') Then Return SetExtended(1, False) ; illegal characters
    If StringRegExp($sFormula, '(?i)\.\D|c[0\D]|\d\s+[\.\d]') Then Return SetExtended(3, False) ; syntax

    $sFormula = StringStripWS($sFormula, 8) ; strip all spaces

    ; split to separate formula components
    Local $aFormula = StringRegExp($sFormula, '(?i)\-?c\d+|\-?c|\-?\d*\.\d*|\-?\d+|\-\(|[\+\-\*/\^\(\)]|.+', 3), _ ; create array [ADDED] ==> |.+
    $iLast, $sTest, $sExpect = '(?i)\-?\(|\-?c\d+|\-?c|\-?\d*\.\d*|\-?\d+|\-', _ ; valid first component
    $iBracket = 0, $bSyntax = False, $bNoSuch = False, $bTermination = False ; error tracking variables

    For $i = 0 To UBound($aFormula) -1
        If Not StringRegExp($aFormula[$i], $sExpect) Then ; unexpected code sequence
            $bSyntax = True
            ExitLoop
        EndIf

        If StringRegExp($aFormula[$i], '(?i)\-?c\d+') Then ; column number
            $sTest = StringRegExpReplace($aFormula[$i], '(?i)[\-c]+', '')
            If $sTest > $iMax Or $sTest == 0 Then ; no such column exists
                $bNoSuch = True
                ExitLoop
            EndIf
            $sExpect = '[\+\-\*/\^\)]' ; operators / closing brackets

        ElseIf StringRegExp($aFormula[$i], '\-?\d*\.\d*') Then ; decimal
            If $aFormula[$i] == '.' Or $aFormula[$i] == '-.' Then
                If $i = UBound($aFormula) -1 Then
                    $bTermination = True
                    ExitLoop
                EndIf
                $bSyntax = True ; badly formated decimal
                ExitLoop
            EndIf
            $sExpect = '[\+\-\*/\^\)]' ; as above

        ElseIf StringRegExp($aFormula[$i], '\-?\d+') Then ; integer
            $sExpect = '[\+\-\*/\^\)]'

        ElseIf StringRegExp($aFormula[$i], '\A[\+\-\*/\^]\z') Then ; operator
            $iLast = UBound($aFormula) -1
            If $i = $iLast Or ($i = $iLast -1 And $aFormula[$iLast] == '-') Then
                $bTermination = True
                ExitLoop
            EndIf
            $sExpect = '(?i)\-?\(|\-?c\d+|\-?c|\-?\d*\.\d*|\-?\d+'

        ElseIf $aFormula[$i] == '(' Or $aFormula[$i] == '-(' Then ; opening bracket
            $iBracket += 1
            $sExpect = '(?i)\-?\(|\-?c\d+|\-?c|\-?\d*\.\d*|\-?\d+|\-'

        ElseIf $aFormula[$i] == ')' Then ; closing bracket
            $iBracket -= 1
            $sExpect = '[\+\-\*/\^\)]'

        ElseIf $aFormula[$i] = 'c' Or $aFormula[$i] = '-c' Then ; unterminated column number
            If $i = UBound($aFormula) -1 Then
                $bTermination = True
            Else
                $bSyntax = True
                ExitLoop
            EndIf

        Else ; unexpected exception [this should never happen]
            $bSyntax = True
            ExitLoop
        EndIf
        If $iBracket < 0 Then ExitLoop ; closing (unopened) parenthesis error

        ; [formula looks okay so far]
    Next

    ; better to be a bit wordy [for clarity]
    If $bNoSuch Then Return SetExtended(2, False) ; no such column exists
    If $bSyntax Then Return SetExtended(3, False) ; syntax error
    If $iBracket < 0 Then Return SetExtended(4, False) ; missing opening parenthesis
    If $bTermination Then Return SetExtended(5, False) ; formula may not terminate with operator or c
    If $iBracket > 0 Then Return SetExtended(6, False) ; missing closing parenthesis
    ; ConsoleWrite('valid' & @LF)
    Return True ; formula is correct
EndFunc ;==> ValidSyntax

 

Edited by czardas

Share this post


Link to post
Share on other sites



Just did a quick test.
You stated that "user experience is everything". I, as a user, would like to see the general error message "The formula you entered contains errors." replaced by helpful error messages like "no such column exists" etc.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks water :), how very true. I should have added that part: all the information is returned by ValidSyntax() with the @extended flag. I was just too lazy to add the final code (or distracted by other concerns). I was more wondering if I might have missed something while testing formula syntax.

Share this post


Link to post
Share on other sites

Just noticed that there is a limit of 24 columns. Would be fine to have this described in the help function :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The limit is a temporary measure. You can change it to any number you like > 0. Modify this line:

Global $g_iAvailableCols = 24 ; arbitary dev variable [column count will eventually be read from a listview control]

 

Edited by czardas

Share this post


Link to post
Share on other sites

I mean: Modify the help page to

" column numbers :" & @TAB & @TAB & "c1, c2, c3  etc. maximum is c" & $g_iAvailableCols & @CRLF & _

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

If the # of decimal places exceeds 14 then it is set to the # of allowed columns (now 24).
Is this intended by you?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Maybe, I get what you are saying. There will be a help menu topic. I wanted to know if it is possible to figure out what's going on without the need to refer to help topics. There has to be a quick syntax reference though. I'm just about to update the first post to include a more detailed error report.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Oops, I missed your last question about decimal places (post #7): an error crept in on my first edit which I have now fixed. I also notice that the final error message doesn't entirely like the enter key when an edit control has focus. It's an easy fix.

Edit: The enter key issue has also been fixed now.

Edited by czardas

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Another validation bugfix, although you probably won't notice any difference. In theory it should be easy to quickly learn how to type a formula with just the information available. How easy do you think this is? Does the colour change assist you or annoy you?

Edited by czardas

Share this post


Link to post
Share on other sites

I like the colour change because it immediately tells me that the last letter/number I entered was wrong.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@water I couldn't ask for a better critic after all the work you have done creating the Excel UDF. I really appreciate you taking the time to try it. :thumbsup:

Share this post


Link to post
Share on other sites

Thanks :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

I've made some small improvements to the code in first post. I may change it to allow rounding up to 15 decimal places. Once I implement this code, it may become unrecognizable. While this may not be the most exciting topic of discussion, I'm also posting the code I've just written for rounding and formatting the executed formula, since it is part of the same module: it might also be of inspiration to someone. It is currently intended that the formula will return a double. The following code runs with all the safeties turned off (potentially returning up to 17 significant digits along with a load of zeros). The user will be advised (not forced) to remain within 15 significant figures when rounding. Trailing zero padding is added by default.

MsgBox(0, "", DecimalPlaces(Execute('0.30000000000000099'), 15))
MsgBox(0, "", DecimalPlaces(Execute('2/-3'), 15))
MsgBox(0, "", DecimalPlaces(Execute('-0.000000000000011 *3'), 15))
MsgBox(0, "", DecimalPlaces(Execute('10000000000000.01000000000000000'), 15))
MsgBox(0, "", DecimalPlaces(Execute('-123456789012345.6199123458989'), 3))
MsgBox(0, "", DecimalPlaces(1.8734176520787252e+004, 12)) ; lucky number!
MsgBox(0, "", DecimalPlaces(Execute('-000222222222222222922222222222222222222200.888888888888888*2'), 14))

; accepts doubles
Func DecimalPlaces($fNumber, $iPlaces = 2) ;, $bTrailingZeros = True) ; [add this option later along with program settings]
    ; error checks [magnitude maybe]
    $iPlaces = Int($iPlaces) ; max = 15
    $fNumber = Number($fNumber, 3) ; convert to double (just in case) [more complex routines could also be written: BigNum etc...]

    Local $bNeg = False
    If StringLeft($fNumber, 1) == '-' Then
        $bNeg = True
        $fNumber = Abs($fNumber)
    EndIf

    $fNumber = Round($fNumber, $iPlaces)

    Local $nDigits, $iExponent, $nOutput, $iLen, $iDot = StringInStr($fNumber, '.')
    If StringInStr($fNumber, 'e') Then
        $nDigits = StringReplace(StringRegExpReplace($fNumber, 'e[\+\-]\d+\z', ''), '.', '')
        $iLen = StringLen($nDigits)
        $iExponent = StringRight($fNumber, 4)
        If $iLen = 15 Then ; [uh-uh - rounding had no discernible effect on length]
            $iLen = $iExponent + $iPlaces +1
            $nDigits = __FloatToDigits($fNumber, (($iLen < 16) ? $iLen : 16)) ; return all 17 digits of the rounded double [it makes sense]
            $iExponent = @extended
            $iLen = StringLen($nDigits)
        Else
            $iExponent += (1 -$iLen)
        EndIf

    ElseIf $iDot And StringLen($fNumber) < 16 Then
        $nOutput = StringLeft($fNumber & '000000000000000', $iDot + $iPlaces)
        If $bNeg Then $nOutput = '-' & $nOutput
        Return $nOutput

    Else
        $iLen =  ($iDot ? $iDot : StringLen($fNumber)) + $iPlaces
        $nDigits = __FloatToDigits($fNumber, (($iLen < 16) ? $iLen : 16))
        $iExponent = @extended
        $iLen = StringLen($nDigits)
    EndIf

    Local $sZeros = '000000000000000'
    If $iExponent < 0 Then
        Local $iGap = $iLen + $iExponent
        If $iGap < 1 Then
            $nOutput = '0.' & StringLeft($sZeros, Abs($iGap)) & $nDigits
        Else
            $nOutput = StringLeft($nDigits, $iGap) & '.' & StringRight($nDigits, $iLen -$iGap)
        EndIf

        $iGap = $iLen -$iGap
        If $iGap < $iPlaces Then
            $nOutput &= StringLeft($sZeros, $iPlaces -$iGap)

        ElseIf $iGap > $iPlaces Then ; this one slipped through the net
            Local $bAddOne = (StringMid($nOutput, StringInStr($nOutput, '.') +$iPlaces +1, 1) >= 5) ? True : False ; round up
            $nOutput = StringTrimRight($nOutput, $iGap -$iPlaces)

            If $bAddOne Then ; when all else fails, use the old fashioned method
                Local $vNext, $nRound
                For $i = StringLen($nOutput) To 1 Step -1
                    $vNext = StringMid($nOutput, $i, 1)
                    If $vNext = '.' Then
                        $nRound &= '.'
                    Else
                        $vNext += 1
                        If $vNext < 10 Then
                            $nRound &= $vNext
                            $nOutput = StringLeft($nOutput, $i -1) & StringReverse($nRound)
                            $bAddOne = False
                            ExitLoop ; this should happen on the first run [does this loop ever actually repeat?]
                        EndIf
                        $nRound &= '0'
                    EndIf
                Next
                If $bAddOne Then $nOutput = '1' & StringReverse($nRound)
            EndIf
        EndIf

    Else
        While StringLen($sZeros) < $iExponent
            $sZeros &= '000000000000000'
        WEnd

        $nOutput = $nDigits & StringLeft($sZeros, $iExponent) & '.' & StringLeft('000000000000000', $iPlaces)
    EndIf

    If $iPlaces = 0 Then $nOutput = StringReplace($nOutput, '.', '')
    If $bNeg And StringRegExp($nOutput, '[1-9]') Then $nOutput = '-' & $nOutput

    Return $nOutput
EndFunc ;==> DecimalPlaces

; function taken from : Fraction.au3
; #INTERNAL_USE_ONLY# ==========================================================================================================
; Name...........: __FloatToDigits
; Description ...: Extracts a specified number of digits from a float.
; Syntax.........: __FloatToDigits($fFloat, $iDigits)
; Parameters.....; $fFloat - The float to extract the digits from.
;                  $iDigits - The number of digits to extract after the floating point (exponential representation).
; Return values .: Success - Returns a 32-bit or 64-bit signed integer.
;                  Sets @extended to the decimal exponent. ==> $fFloat = return value * 10 ^ exponent
;                  Failure sets @error to 1 if the input is not a float or undefined.
; Author ........: czardas
; ==============================================================================================================================
Func __FloatToDigits($fFloat, $iDigits = 14)
    If VarGetType($fFloat) <> 'Double' Or StringInStr($fFloat, '#') Then Return SetError(1)
    Local $iSign = ($fFloat < 0) ? -1 : 1
    ; machine epsilon = 5 × 10^-15, so the final two digits (16 and 17) could be highly innacurate
    $fFloat = StringFormat('%.' & $iDigits & 'e', $fFloat) ; rounds to the specified number of decimal places

    Local $aFloat = StringSplit($fFloat, "e", 2) ; zero-based array
    If $iSign < 0 Then $aFloat[0] = StringTrimLeft($aFloat[0], 1) ; remove the minus sign

    ; remove the decimal point and trailing zeros
    $aFloat[0] = StringLeft($aFloat[0], 1) & StringRegExpReplace(StringRight($aFloat[0], $iDigits), '(0+\z)', '')
    $aFloat[1] += 1 - StringLen($aFloat[0]) ; adjust the exponent to accommodate changes

    Return SetExtended($aFloat[1], Int($aFloat[0]) * $iSign) ; add back the minus sign
EndFunc ;==> __FloatToDigits

There may yet be some superfluous (and therefore dubious) lines of code in the above example. I'll try removing them later, and see if it breaks anything. Which is worse, broken code or fuzzy human logic? :ermm:

Edited by czardas
code tidying

Share this post


Link to post
Share on other sites

Oh my! The worst is broken code with fuzzy human logic. I'm sure there must be a trick I can use like adding '.0' and just throwing the value into some engine designed by someone who can read the mind of the user.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

The above code has now been implemented and seems to work like a charm. I was also thinking of adding a column ratio feature, but I'm not sure how useful it might be. Here someone asks how to do this: http://stackoverflow.com/questions/472989/how-do-i-display-a-ratio-in-excel-in-the-format-ab , and I want to know why. Can you think of any practical applications?

Edited by czardas

Share this post


Link to post
Share on other sites

No, not really ;)

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I suppose application may be limited, but I think it looks nice. It can be useful in music theory or calculating betting odds, or if you just want to annoy people on stackoverflow. :D

Share this post


Link to post
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

  • Similar Content

    • zetaimmersion
      By zetaimmersion
      I have 2 items (a field box and a bypass checkbox). every time the box is checked i need the field to become writable. unchecked is read only displaying some text.
      this is as far as i got as I am stuck at making it read/write toggle
      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <EditConstants.au3> #include <WindowsConstants.au3> $hGUI = GUICreate("Test", 500, 500) Global $hCombo = GUICtrlCreateInput("", 10, 10, 200, 20, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) GUICtrlSetBkColor($hCombo,0xe7e5e5) Global $cbox = GUICtrlCreateCheckbox ("", 40,50,10,20) GUICtrlSetState($cbox, $GUI_Unchecked) GUISetState() Global $sCurrCombo = "" While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cbox If GUICtrlRead($cbox) <> $sCurrCombo Then $sCurrCombo = GUICtrlRead($cbox) GUICtrlSetStyle ($hCombo, $SS_LEFTNOWORDWRAP) GUICtrlSetBkColor($hCombo,0xFFFFFF) MsgBox(0, "Choice", "PLease enter the text") EndIf EndSwitch WEnd  
    • dascondor
      By dascondor
      So I having issues with this GUI/Listview. My main GUI works like it needs to but the ListView GUIs don't. The exit buttons do not work on them and om have a hard time getting them to work. And I'm trying to make the  listview GUI resizeable. But I'm not sure how to fix either of these issues any thoughts.
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <ScreenCapture.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <GuiListView.au3> #include <GuiImageList.au3> #include <ListviewConstants.au3> #include 'MultiPing.au3' Opt("GUIOnEventMode", 1) #Region ;Declares ; Reads text file for the base list of IP's Global $aBase_IPlist = _FileReadToArray_mod(".\IP_List.txt") Global $replace2 = "Second" Global $replace3 = "Thrid" ;Global $userdata1 = GUICtrlRead($data1) ;Global $userdata2 = GUICtrlRead($data2) Global $savefolder = "C:\Users\" Global $paths = "\Desktop\PingChecks" Global $1stpaths = "\~~~Store" Global $2ndpaths = "~~~" Global $results = FileExists($savefolder & @UserName & $paths) ;Global $foldercheck = FileExists($savefolder & @UserName & $paths & $1stpaths & $userdata1 & $userdata2 & $2ndpaths) ;Global $savefile = "C:\Users\" & @UserName & "\Desktop\PingChecks\~~~Store" & $userdata1 & $userdata2 & "~~~\" #EndRegion ;Declares #Region ### START Koda GUI section ### Form= $IPMonkey = GUICreate("IP Monkey", 573, 254, -1, -1) GUISetIcon("C:\Users\Dlex\Pictures\monkeyicon.ico", -1) GUISetFont(16, 800, 0, "Arial") GUISetBkColor(0xFFFFFF) GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents") GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "SpecialEvents") GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents") $data1 = GUICtrlCreateInput("1", 184, 120, 89, 32, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) GUICtrlSetColor(-1, 0x000000) $data2 = GUICtrlCreateInput("23", 280, 120, 89, 32, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) GUICtrlSetColor(-1, 0x000000) $GOButton = GUICtrlCreateButton("GO", 80, 192, 75, 25) GUICtrlSetFont(-1, 10, 800, 0, "Arial") GUICtrlSetColor(-1, 0x000000) GUICtrlSetBkColor(-1, 0x00FF00) GUICtrlSetTip(-1, "Runs A Live Continuous Ping" & @CRLF & _ "Until Told Other Wise") GUICtrlSetOnEvent(-1, "GOButtonClick") $quickscan = GUICtrlCreateButton("Quick Scan", 224, 192, 107, 25) GUICtrlSetFont(-1, 12, 800, 0, "Arial") GUICtrlSetColor(-1, 0x000000) GUICtrlSetBkColor(-1, 0xFFFF00) GUICtrlSetOnEvent(-1, "quickscanClick") GUICtrlSetTip(-1, "Quickly Scans, Then Saves" & @CRLF & _ "Results In Your 'PingChecks' " & @CRLF & _ "Folder Under Your Store Number Folder") Global $exitbutton = GUICtrlCreateButton("Exit", 416, 192, 75, 25) GUICtrlSetFont(-1, 12, 800, 0, "Arial") GUICtrlSetColor(-1, 0x000000) GUICtrlSetBkColor(-1, 0xFF0000) GUICtrlSetTip(-1, "What Do You Think This Button Does?") GUICtrlSetOnEvent(-1, "exitbuttonClick") $titlebar = GUICtrlCreateLabel("IP Monkey", 16, 120, 107, 28) GUICtrlSetColor(-1, 0x000000) $Pic1 = GUICtrlCreatePic("C:\Users\Dlex\Pictures\monkeyicon-0.jpg", 16, 16, 100, 100) GUICtrlSetOnEvent(-1, "Pic1Click") $infobutton = GUICtrlCreateButton("?", 536, 8, 27, 25) GUICtrlSetColor(-1, 0x000000) GUICtrlSetBkColor(-1, 0xFF8000) GUICtrlSetTip(-1, "Unsure what to do?" & @CRLF & _ "This will get give you " & @CRLF & _ "the help for IP Monkey") GUICtrlSetOnEvent(-1, "infobuttonClick") $Whatstorelabel = GUICtrlCreateLabel("What Store?", 208, 56, 130, 28) GUICtrlSetColor(-1, 0x000000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### #Region ;ping gui ; ------ pinger GUI ------------------- Global $StopPing = 0 Local $Win_X = 1200, $Win_Y = 880 ; dimension of window Global $PingGui = GUICreate("IP Monkey", $Win_X, $Win_Y, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents") Global $listview = GUICtrlCreateListView("", 10, 10, $Win_X - 20, $Win_Y - 40) GUICtrlSetFont(-1, 8) $button1 = GUICtrlCreateLabel("Hit Exit To Quit", 10, $Win_Y - 25, $Win_X - 20, 20, -1,$ES_CENTER) GUICtrlSetFont(-1, 10, 800) GUICtrlSetTip(-1, "exit") GUICtrlSetOnEvent(-1, 'SpecialEvents') GUICtrlSetStyle($listview, $LVS_ICON) ; + $LVS_NOLABELWRAP) GUISetState(@SW_HIDE) ; hidden at startup ; --------- end of pinger gui ------------- #EndRegion ;ping gui ; Generate colored square images $hImage = _GUIImageList_Create(30, 30) _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFFFF00, 30, 30)) ; yellow _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFF0000, 30, 30)) ; red _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0x00FF00, 30, 30)) ; green _GUICtrlListView_SetImageList($listview, $hImage, 0) While 1 Sleep(100) WEnd Func exitbuttonClick() MsgBox(0,'0','0') Exit EndFunc ;==>exitbuttonClick Func GOButtonClick() $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) ; pass the wanted new octet replace_And_Go($userdata1, $userdata2) ; generate a list of new IP and Go EndFunc ;==>GOButtonClick Func infobuttonClick() EndFunc ;==>infobuttonClick Func Pic1Click() EndFunc ;==>Pic1Click Func quickscanClick() $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) replace_And_Go2($userdata1, $userdata2) EndFunc ;==>quickscanClick Func SpecialEvents() Select Case @GUI_CtrlId = $GUI_EVENT_CLOSE _button1() Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE Case @GUI_CtrlId = $GUI_EVENT_RESTORE Case @GUI_CtrlId = $GUI_EVENT_MAXIMIZE EndSelect EndFunc ;==>SpecialEvents #Region ; Main_GO/Replace_and_GO #cs ############################## second script ############################################# ; this is to ping continuously a list of IP addresses, get and display ping result "live" ; it simulates the dos "ping -t" command but performed simultaneously on many IP ; presenting the results in a ListView highlighting not responding devices with a red box #ce Func replace_And_Go($s_2, $s_3) ; Create a new array wit a new list of IP according to passed second and third octet Local $aNew_IPlist = $aBase_IPlist For $i = 0 To UBound($aNew_IPlist) - 1 $aNew_IPlist[$i][1] = StringReplace(StringReplace($aBase_IPlist[$i][1], $replace2, $s_2), $replace3, $s_3) Next GUISetState(@SW_HIDE, $IPMonkey) GUISetState(@SW_SHOW, $PingGui) ; pass the new list to the pinger Main_GO($aNew_IPlist) GUISetState(@SW_HIDE, $PingGui) GUISetState(@SW_SHOW, $IPMonkey) EndFunc ;==>replace_And_Go Func Main_GO($IPlist) HotKeySet("{esc}", "exitbuttonClick") $StopPing = 0 ; $IPlist = _FileReadToArray_mod(".\IP_List.txt") ; Reads text file for list of IP's ; ; the above command, it loads in the $IPlist array the values contained in the file IP_List.txt ; values in the file should be separated by a semicolon, something like in the following example: ; ; hostname1;192.168.0.1 ; hostname2;192.168.0.5 ; hostnameX;10.59.7.200 ; etc.... ; ; if values in the file are not separated by a semicolon, but another char is used, for example a comma, ; then just pass it as second parameter of the function: $IPlist = _FileReadToArray_mod(".\IP_List.txt", ",") ; _GUICtrlListView_BeginUpdate($listview) _GUICtrlListView_DeleteAllItems($listview) _GUICtrlListView_AddArray($listview, $IPlist) ; fill ListView _GUICtrlListView_EndUpdate($listview) While Not $StopPing ; 1 ; continuously ping addresses of the previously loaded file (IP_List.txt) Sleep(10) ; ; $IPlist is the array loaded with all the IP to be pinged (a 2d array in this case) ; | ; | 1 means the IP are in the second column of the $IPlist array (first colun is nr. 0) ; | | ; | | +--> 0 means return back an array loaded with results from all pinged addresses (responding and not responding) ; | | | if you use 1 then only responding addresses are loaded in the returned array [default] ; | | | if you use 2 then only NOT responding addresses are loaded in the returned array ; | | | In this case we do not need an array to be returned, we only need to perform all pings and pass results ; | | | directly (on the fly) to the "_refresh" callback function that will refresh the listview ; | | | ; | | | 0 means NO lookup name resolution must be performed ; | | | | ; | | | | +--> this is the callback function to be called for each pinged address each time the ping has finished ; | | | | | (see the MultiPing.au3 file for info on all passed params) ; | | | | | 6 parameters are passed to this function, but only 2 are used by the called function in this case: ; | | | | | [4] roundtrip of the responding ping or -1 if IP is down ; | | | | | [5] Index (position) of this IP within the caller's passed array ; | | | | | ; v v v v v _nPing($IPlist, 1, 0, 0, "_refresh") WEnd EndFunc ;==>Main_GO #EndRegion ; Main_GO/Replace_and_GO #Region ; QuickScan/Replace_and_GO2 Func replace_And_Go2($s_2, $s_3) ; Create a new array wit a new list of IP according to passed second and third octet Local $aNew_IPlist = $aBase_IPlist For $i = 0 To UBound($aNew_IPlist) - 1 $aNew_IPlist[$i][1] = StringReplace(StringReplace($aBase_IPlist[$i][1], $replace2, $s_2), $replace3, $s_3) Next GUISetState(@SW_HIDE, $IPMonkey) GUISetState(@SW_SHOW, $PingGui) ; pass the new list to the pinger QuickRun($aNew_IPlist) GUISetState(@SW_HIDE, $PingGui) GUISetState(@SW_SHOW, $IPMonkey) EndFunc ;==>replace_And_Go2 Func QuickRun($IPlist) $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) HotKeySet("{esc}", "_button1") Local $Win_X = 1200, $Win_Y = 880 ; dimension of window $PingGui = GUICreate("IP Monkey" & "~~~" & @UserName & "~~~" & "Store" & $userdata1 & $userdata2 & "~~~" & @MON & "." & @MDAY & "." & @YEAR & "~~~" & @HOUR & "." & @MIN, $Win_X, $Win_Y, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, "_button1", $PingGui) $listview = GUICtrlCreateListView("", 10, 10, $Win_X - 20, $Win_Y - 40) GUICtrlSetFont(-1, 8) GUICtrlSetStyle($listview, $LVS_ICON) ; + $LVS_NOLABELWRAP) ; Generate colored square images $hImage = _GUIImageList_Create(30, 30) _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFFFF00, 30, 30)) ; yellow _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFF0000, 30, 30)) ; red _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0x00FF00, 30, 30)) ; green _GUICtrlListView_SetImageList($listview, $hImage, 0) $button1 = GUICtrlCreateButton("Exit", 10, $Win_Y - 25, $Win_X - 20, 20) GUICtrlSetTip(-1, "End of program") GUICtrlSetOnEvent(-1, "exitbuttonClick") GUISetState(@SW_SHOW) ;~ $IPlist = _FileReadToArray_mod(".\IP_List.txt") ; Reads text file for list of IP's _GUICtrlListView_BeginUpdate($listview) _GUICtrlListView_AddArray($listview, $IPlist) ; fill ListView _GUICtrlListView_EndUpdate($listview) ;While 1 ; perform Pings and update ListView by the "_refresh" callback function $aFinalResult = _nPing($IPlist, 1, 0, 0, "_refresh") ;~ _ArrayDisplay($aFinalResult) ;DirCheck() EndFunc ;==>QuickRun #EndRegion ; QuickScan/Replace_and_GO2 #Region ; Misc Funcs (network) Func _refresh($Params) ; this receive ping results and displays them in the ListView _GUICtrlListView_SetItemImage($listview, $Params[5], 0) ; set colour to Yellow Sleep(50) ; a little wait If $Params[4] = -1 Then ; Device not responding to ping _GUICtrlListView_SetItemImage($listview, $Params[5], 1) ; set colour to RED _GUICtrlListView_EnsureVisible($listview, $Params[5]) ; Position view to this item Else ; Device responds to ping _GUICtrlListView_SetItemImage($listview, $Params[5], 2) ; set colour to GREEN EndIf EndFunc ;==>_refresh Func _button1() ; Button 1 clicked ;~ $StopPing = 1 Exit EndFunc ;==>_button1 #EndRegion ; Misc Funcs (network)  
      MultiPing.au3
      IP_List.txt
    • jannikrendl
      By jannikrendl
      Hey,
      I'd like to create a gui wich records the total runtime of the script. 
      It should tell the hours,minutes and seconds (like this: Running since 00:00:00)
      This is what I have right now:
      Local $iTimer, $hGUI, $cLabel $iTimer = TimerInit() $hGUI = GUICreate("") $cLabel = GUICtrlCreateLabel("",300,380,100,20) GUISetState() AdlibRegister("_UpdateLabel",1000) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case -3 Exit EndSwitch Wend Func _UpdateLabel() $iTimerDiff = TimerDiff($iTimer) GUICtrlSetData($cLabel,"Running Since " & Round($iTimerDiff/1000,0) & " sec.") EndFunc Thanks in advance
    • dascondor
      By dascondor
      I  need to has Opt(GUISetOnEvent,-1) available in my script, but since I'm using koda designer it uses GUIgetmessage is there any work around for this. I'm pretty new to this stuff so any help is great appreciated.
      Any Thoughts?
       
    • dascondor
      By dascondor
      Hello I've been working on an IP Scanner Program with help from other people on the forum. Anyway I've hit a wall with why the GUI won't close on Exit button once pass the main screen, please if anyone could help that would be great.
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\..\..\Downloads\monkeyicon.ico #AutoIt3Wrapper_Outfile=IP Monkey.exe #AutoIt3Wrapper_Run_Tidy=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <ScreenCapture.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <GuiListView.au3> #include <GuiImageList.au3> #include <ListviewConstants.au3> #include 'MultiPing.au3' ; <-- take this from the following link: ; http://www.autoitscript.com/forum/topic/156395-versatile-multi-ping #Region ;ping gui Opt("GUIOnEventMode", 1) ; ------ pinger GUI ------------------- Global $StopPing = 0 Local $Win_X = 1200, $Win_Y = 880 ; dimension of window Global $PingGui = GUICreate("IP Monkey", $Win_X, $Win_Y, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, "_button1", $PingGui) Global $listview = GUICtrlCreateListView("", 10, 10, $Win_X - 20, $Win_Y - 40) GUICtrlSetFont(-1, 8) ;~ $button1 = GUICtrlCreateLabel("Hit Exit To Quit", 10, $Win_Y - 25, $Win_X - 20, 20, -1, $ES_CENTER) ;~ GUICtrlSetFont(-1, 1, 800) ;~ GUICtrlSetTip(-1, "save") ;~ GUICtrlSetOnEvent(-1, 'QuickRun') GUICtrlSetStyle($listview, $LVS_ICON) ; + $LVS_NOLABELWRAP) GUISetState(@SW_HIDE) ; hidden at startup ; --------- end of pinger gui ------------- #EndRegion ;ping gui ; Generate colored square images $hImage = _GUIImageList_Create(30, 30) _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFFFF00, 30, 30)) ; yellow _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFF0000, 30, 30)) ; red _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0x00FF00, 30, 30)) ; green _GUICtrlListView_SetImageList($listview, $hImage, 0) #Region ### START Koda GUI section ### Form= $IPpong = GUICreate("IP Monkey", 572, 253, -1, -1) GUISetBkColor(0xFFFFFF) Global $data1 = GUICtrlCreateInput("1", 184, 104, 81, 37, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0x000000) Global $data2 = GUICtrlCreateInput("23", 280, 104, 73, 37, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0x000000) Global $gobutton = GUICtrlCreateButton("GO", 80, 192, 75, 25) GUICtrlSetTip(-1, "Runs A Live Continuous Ping" & @CRLF & _ "Until Told Other Wise") GUICtrlSetBkColor(-1, 0x00FF00) Global $quickbutton = GUICtrlCreateButton("Quick Run", 240, 192, 75, 25) GUICtrlSetTip(-1, "Quickly Scans, Then Saves" & @CRLF & _ "Results In Your 'PingChecks' " & @CRLF & _ "Folder Under Your Store Number Folder") GUICtrlSetBkColor(-1, 0xFFFF00) $title = GUICtrlCreateLabel("IP Monkey", 16, 128, 116, 33) GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0x000000) $infobutton = GUICtrlCreateButton("?", 536, 16, 27, 25) GUICtrlSetTip(-1, "Unsure what to do?" & @CRLF & _ "This will get give you " & @CRLF & _ "the help for IP Monkey") GUICtrlSetBkColor(-1, 0xFF8000) ;$superbutton = GUICtrlCreateButton("", 0, 0, 19, 17) ;GUICtrlSetBkColor(-1, 0xFFFFFF) ;GUICtrlSetState(-1, $GUI_HIDE) $Label1 = GUICtrlCreateLabel("What Store?", 200, 40, 145, 33) GUICtrlSetFont(-1, 18, 800, 0, "MS Sans Serif") GUICtrlSetColor(-1, 0x000000) Global $exitbutton = GUICtrlCreateButton("Exit", 400, 192, 75, 25) GUICtrlSetTip(-1, "What Do You Think This Button Does?") GUICtrlSetBkColor(-1, 0xFF0000) $Icon = GUICtrlCreatePic("C:\Users\" & @UserName & "\Desktop\monkeyicon-0.jpg", 16, 16, 100, 100) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### #Region ;Declares ; Reads text file for the base list of IP's Global $aBase_IPlist = _FileReadToArray_mod(".\IP_List.txt") Global $replace2 = "Second" Global $replace3 = "Thrid" Global $userdata1 = GUICtrlRead($data1) Global $userdata2 = GUICtrlRead($data2) Global $savefolder = "C:\Users\" Global $paths = "\Desktop\PingChecks" Global $1stpaths = "\~~~Store" Global $2ndpaths = "~~~" Global $results = FileExists($savefolder & @UserName & $paths) Global $foldercheck = FileExists($savefolder & @UserName & $paths & $1stpaths & $userdata1 & $userdata2 & $2ndpaths) Global $savefile = "C:\Users\" & @UserName & "\Desktop\PingChecks\~~~Store" & $userdata1 & $userdata2 & "~~~\" #EndRegion ;Declares #Region ; Foldercreation/ScreenCap/HelpFile Func DirCheck() Global $savefolder = "C:\Users\" Global $paths = "\Desktop\PingChecks" $results = FileExists($savefolder & @UserName & $paths) Global $helptext = "C:\Users\" & @UserName & "\Desktop\PingChecks\README.txt" If $results = 0 Then DirCreate($savefolder & @UserName & $paths) FileWrite($helptext, " .. IMMMI .. " & @CRLF & _ " .~MMMMMMMMMMMMM~. ") Createdforstore() ElseIf $results = 1 Then Createdforstore() EndIf EndFunc ;==>DirCheck Func Createdforstore() Local $pathslocal = "\Desktop\PingChecks\" If $foldercheck = 0 Then DirCreate($savefolder & @UserName & $pathslocal & $1stpaths & $userdata1 & $userdata2 & $2ndpaths) Screencap() ElseIf $foldercheck = 1 Then Screencap() EndIf EndFunc ;==>Createdforstore Func Screencap() $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) Local $savesfile = "C:\Users\" & @UserName & "\Desktop\PingChecks\~~~Store" & $userdata1 & $userdata2 & "~~~\" Global $point = "," Global $time = "." Global $id = "Store" Global $tid = "~" ;~ Global $pathslocal = "\Desktop\PingChecks\" _ScreenCapture_CaptureWnd($savesfile & @UserName & $tid & $id & $userdata1 & $userdata2 & $tid & @MON & $time & @MDAY & $time & @YEAR & $tid & @HOUR & $time & @MIN & $tid & ".jpg", $PingGui) MsgBox(0, "Completed", " Your Ping Scan Is In Your PingChecks Folder, " & @CRLF & _ "On Your Desktop.") Exit EndFunc ;==>Screencap ;helpfile Func Helpfile() $readmecheck = FileExists("README.txt") If $readmecheck = 1 Then ShellExecute("README.txt") ElseIf $readmecheck = 0 Then Run("Notepad.exe") WinWait("Untitled - Notepad") ControlSetText("Untitled - Notepad", '', 'Edit1', " .. IMMMI .. " & @CRLF & _ " .~MMMMMMMMMMMMM~. ") EndIf EndFunc ;==>Helpfile #EndRegion ; Foldercreation/ScreenCap/HelpFile #Region ; Main_GO/Replace_and_GO #cs ############################## second script ############################################# ; this is to ping continuously a list of IP addresses, get and display ping result "live" ; it simulates the dos "ping -t" command but performed simultaneously on many IP ; presenting the results in a ListView highlighting not responding devices with a red box #ce Func replace_And_Go($s_2, $s_3) ; Create a new array wit a new list of IP according to passed second and third octet Local $aNew_IPlist = $aBase_IPlist For $i = 0 To UBound($aNew_IPlist) - 1 $aNew_IPlist[$i][1] = StringReplace(StringReplace($aBase_IPlist[$i][1], $replace2, $s_2), $replace3, $s_3) Next GUISetState(@SW_HIDE, $IPpong) GUISetState(@SW_SHOW, $PingGui) ; pass the new list to the pinger Main_GO($aNew_IPlist) GUISetState(@SW_HIDE, $PingGui) GUISetState(@SW_SHOW, $IPpong) EndFunc ;==>replace_And_Go Func Main_GO($IPlist) HotKeySet("{esc}", "_button1") ; $IPlist = _FileReadToArray_mod(".\IP_List.txt") ; Reads text file for list of IP's ; ; the above command, it loads in the $IPlist array the values contained in the file IP_List.txt ; values in the file should be separated by a semicolon, something like in the following example: ; ; hostname1;192.168.0.1 ; hostname2;192.168.0.5 ; hostnameX;10.59.7.200 ; etc.... ; ; if values in the file are not separated by a semicolon, but another char is used, for example a comma, ; then just pass it as second parameter of the function: $IPlist = _FileReadToArray_mod(".\IP_List.txt", ",") ; _GUICtrlListView_BeginUpdate($listview) _GUICtrlListView_DeleteAllItems($listview) _GUICtrlListView_AddArray($listview, $IPlist) ; fill ListView _GUICtrlListView_EndUpdate($listview) While Not $StopPing ; 1 ; continuously ping addresses of the previously loaded file (IP_List.txt) Sleep(10) ; ; $IPlist is the array loaded with all the IP to be pinged (a 2d array in this case) ; | ; | 1 means the IP are in the second column of the $IPlist array (first colun is nr. 0) ; | | ; | | +--> 0 means return back an array loaded with results from all pinged addresses (responding and not responding) ; | | | if you use 1 then only responding addresses are loaded in the returned array [default] ; | | | if you use 2 then only NOT responding addresses are loaded in the returned array ; | | | In this case we do not need an array to be returned, we only need to perform all pings and pass results ; | | | directly (on the fly) to the "_refresh" callback function that will refresh the listview ; | | | ; | | | 0 means NO lookup name resolution must be performed ; | | | | ; | | | | +--> this is the callback function to be called for each pinged address each time the ping has finished ; | | | | | (see the MultiPing.au3 file for info on all passed params) ; | | | | | 6 parameters are passed to this function, but only 2 are used by the called function in this case: ; | | | | | [4] roundtrip of the responding ping or -1 if IP is down ; | | | | | [5] Index (position) of this IP within the caller's passed array ; | | | | | ; v v v v v _nPing($IPlist, 1, 0, 0, "_refresh") WEnd HotKeySet("{esc}", _button1) $StopPing = 0 EndFunc ;==>Main_GO #EndRegion ; Main_GO/Replace_and_GO #Region ; QuickScan/Replace_and_GO2 Func replace_And_Go2($s_2, $s_3) ; Create a new array wit a new list of IP according to passed second and third octet Local $aNew_IPlist = $aBase_IPlist For $i = 0 To UBound($aNew_IPlist) - 1 $aNew_IPlist[$i][1] = StringReplace(StringReplace($aBase_IPlist[$i][1], $replace2, $s_2), $replace3, $s_3) Next GUISetState(@SW_HIDE, $IPpong) GUISetState(@SW_SHOW, $PingGui) ; pass the new list to the pinger QuickRun($aNew_IPlist) GUISetState(@SW_HIDE, $PingGui) GUISetState(@SW_SHOW, $IPpong) EndFunc ;==>replace_And_Go2 Func QuickRun($IPlist) $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) HotKeySet("{esc}", "_button1") Local $Win_X = 1200, $Win_Y = 880 ; dimension of window $PingGui = GUICreate("IP Monkey" & "~~~" & @UserName & "~~~" & "Store" & $userdata1 & $userdata2 & "~~~" & @MON & "." & @MDAY & "." & @YEAR & "~~~" & @HOUR & "." & @MIN, $Win_X, $Win_Y, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, "_button1", $PingGui) $listview = GUICtrlCreateListView("", 10, 10, $Win_X - 20, $Win_Y - 40) GUICtrlSetFont(-1, 8) GUICtrlSetStyle($listview, $LVS_ICON) ; + $LVS_NOLABELWRAP) ; Generate colored square images $hImage = _GUIImageList_Create(30, 30) _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFFFF00, 30, 30)) ; yellow _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFF0000, 30, 30)) ; red _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0x00FF00, 30, 30)) ; green _GUICtrlListView_SetImageList($listview, $hImage, 0) $button1 = GUICtrlCreateButton("Exit", 10, $Win_Y - 25, $Win_X - 20, 20) GUICtrlSetTip(-1, "End of program") GUICtrlSetOnEvent(-1, "_button1") GUISetState(@SW_SHOW) ;~ $IPlist = _FileReadToArray_mod(".\IP_List.txt") ; Reads text file for list of IP's _GUICtrlListView_BeginUpdate($listview) _GUICtrlListView_AddArray($listview, $IPlist) ; fill ListView _GUICtrlListView_EndUpdate($listview) ;While 1 ; perform Pings and update ListView by the "_refresh" callback function $aFinalResult = _nPing($IPlist, 1, 0, 0, "_refresh") ;~ _ArrayDisplay($aFinalResult) DirCheck() EndFunc ;==>QuickRun #EndRegion ; QuickScan/Replace_and_GO2 #Region ; Misc Funcs (network) Func _refresh($Params) ; this receive ping results and displays them in the ListView _GUICtrlListView_SetItemImage($listview, $Params[5], 0) ; set colour to Yellow Sleep(50) ; a little wait If $Params[4] = -1 Then ; Device not responding to ping _GUICtrlListView_SetItemImage($listview, $Params[5], 1) ; set colour to RED _GUICtrlListView_EnsureVisible($listview, $Params[5]) ; Position view to this item Else ; Device responds to ping _GUICtrlListView_SetItemImage($listview, $Params[5], 2) ; set colour to GREEN EndIf EndFunc ;==>_refresh Func _button1() ; Button 1 clicked $StopPing = 1 Exit EndFunc ;==>_button1 #EndRegion ; Misc Funcs (network) While (1) $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $gobutton $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) ; pass the wanted new octet replace_And_Go($userdata1, $userdata2) ; generate a list of new IP and Go Case $infobutton Helpfile() ;Case $superbutton ;Run("notepad.exe") Case $quickbutton $userdata1 = GUICtrlRead($data1) $userdata2 = GUICtrlRead($data2) replace_And_Go2($userdata1, $userdata2) Case $exitbutton Exit EndSwitch WEnd