Rhazz

How to configure a GUI Progress Bar by a double "for" loop

8 posts in this topic

#1 ·  Posted (edited)

Hello again, my second post in 24 hs! :P

It's my first GUI... and my first progress bar also! Sorry if I make a newbie mistake.

I want to configure a GUI Progress Bar by a double "for" loop.

Here is my code with a specific annotation with what I want to do:

Edit: new problem, I have no idea how can I continue with the next $a as if the current $a, I do not skip it because the line is an element less (I need help with both problems):

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <File.au3>
#include <String.au3>
#include <Array.au3>


Local $idGUI = GUICreate("ProgressBar", 220, 130, 100, 200)
Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
Local $idInput = GUICtrlCreateInput("",80,20,60,20,$ES_NUMBER)
Local $idProgressBar = GUICtrlCreateProgress(10, 60, 200, 20)
Local $idButton1 = GUICtrlCreateButton("Start",85,100,50)
GUICtrlSetOnEvent($idButton1, "StartProgressBar")
Local $aFileOpen = _WinAPI_GetOpenFileName("Open a text file", "Text Files (*.txt)")
Local $idCountLines = _FileCountLines($aFileOpen[2])
Local $idInputValue, $idSomething, $aFileResult, $idStringBetween

GUISetState(@SW_SHOW, $idGUI)
While 1
    Sleep(20000)
WEnd

Func StartProgressBar()
    If StringInStr($aFileOpen[2], ".txt") = True Then
        $idInputValue = Int(GUICtrlRead($idInput))
        If IsFloat($idCountLines/$idInputValue) = 1 Then
            $idStringBetween = _StringBetween(String($idCountLines/$idInputValue),"", ".")
            $idSomething = Number($idStringBetween[0]) + 1
        Else
            $idSomething = $idCountLines/$idInputValue
        EndIf
        For $i = 1 To $idSomething
            $aFileResult = @ScriptDir & "\result-" & $i & ".txt"
            _FileCreate($aFileResult)
            FileOpen($aFileResult,2)
            For $a = ( ( ( $i - 1 ) * $idInputValue ) + 1 ) To ( $i * $idInputValue )
                If FileReadLine($aFileOpen[2], $a) = "" Then
                    ; Here I have no idea how can I continue with the next $a as if the current $a, I do not skip it because the line is an element less
                EndIf
                FileWrite($aFileResult,FileReadLine($aFileOpen[2], $a) & " - ")
                ;Here I want to set the progress bar value = current percentage (line that it is currently reading) of the 100% (total of lines in the file opened)
            Next
            FileClose($aFileResult)
        Next
        MsgBox(0,"Done","Done")
        GUICtrlSetData($idProgressBar,0)
    Else
        MsgBox(0,"Hello :)","Please open a file :D")
    EndIf
EndFunc

Func CLOSEButton()
    Exit
EndFunc

 

Thanks in advance.

 

Edited by Rhazz
New problem added

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

First you need to get the percentage based on the multiplication of loop ($i*$a) real loops.  then divide  progressbar size by that multiplication.

100/($i*$a) that will result that number of necessary movement that you will use on loop. Finally you will get something like this.

#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 374, 115, 192, 124)
$Progress1 = GUICtrlCreateProgress(16, 24, 345, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $i=300-100
Local $a=200
Local $imove=100/($i*$a)
Local $itemp=0


For $i= 100 to 300
    For $a=1 to 200
        $itemp+=$imove
        GUICtrlSetData($Progress1,$itemp)
        ;for continue with the next $a just use ContinueLoop
    Next
    $itemp+=$imove
    GUICtrlSetData($Progress1,$itemp)

Next

ConsoleWrite($itemp & @CRLF)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
 

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites

First you need to get the percentage based on the multiplication of loop ($i*$a) real loops.  then divide  progressbar size by that multiplication.

100/($i*$a) that will result that number of necessary movement that you will use on loop. Finally you will get something like this.

#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 374, 115, 192, 124)
$Progress1 = GUICtrlCreateProgress(16, 24, 345, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $i=300-100
Local $a=200
Local $imove=100/($i*$a)
Local $itemp=0


For $i= 100 to 300
    For $a=1 to 200
        $itemp+=$imove
        GUICtrlSetData($Progress1,$itemp)
        ;for continue with the next $a just use ContinueLoop
    Next
    $itemp+=$imove
    GUICtrlSetData($Progress1,$itemp)

Next

ConsoleWrite($itemp & @CRLF)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
 

Saludos

Spanish: Veo que hablás español, así que hola! "ContinueLoop" salta la linea del primer archivo y no la agrega a la linea del segundo archivo, ejemplo:

  • Línea 1 - Línea 2 - Línea 3 -
  • Línea 5 - Línea 6 - Línea 7- Línea 8 -

Si te fijas, saltó la línea 4 (suponiendo que está vacía). Igualmente tengo que mirarlo con detenimiento para ver qué puedo hacer, pero no he tenido tiempo.

 

English: Hi, with ContinueLoop the script skips the empty line (of the first file) and therefore there is an element less in the line of the second file (the empty line of the first file becomes into empty space in the second file). e.g. the user wants four lines (of the 1st file) per line (of the 2nd file) and the fourth line (of the 1st file) is empty:

  • Line 1 - Line 2 - Line 3 -
  • Line 5 - Line 6 - Line 7 - Line 8

In the first line there is an element less.Whatever, I have to try it again, these days I didn't have much time.

Share this post


Link to post
Share on other sites

Si fijate en la ayuda y notaras un ejemplo que continueloop esquiva un determinado loop.

Yes look into help file and you will notice an example that continuwloop avoid a determined loop.

 

Saludos

Share this post


Link to post
Share on other sites

Si fijate en la ayuda y notaras un ejemplo que continueloop esquiva un determinado loop.

Yes look into help file and you will notice an example that continuwloop avoid a determined loop.

 

Saludos

I said that ContinueLoop doesn't work, I want to skip a line, not skip a loop. Sorry if I didn't explain me well. Here are another example with the code:

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <File.au3>
#include <String.au3>
#include <Array.au3>


Local $idGUI = GUICreate("ProgressBar", 220, 130, 100, 200)
Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
Local $idInput = GUICtrlCreateInput("",80,20,60,20,$ES_NUMBER)
Local $idProgressBar = GUICtrlCreateProgress(10, 60, 200, 20)
Local $idButton1 = GUICtrlCreateButton("Start",85,100,50)
GUICtrlSetOnEvent($idButton1, "StartProgressBar")
Local $aFileOpen = _WinAPI_GetOpenFileName("Open a text file", "Text Files (*.txt)")
Local $idCountLines = _FileCountLines($aFileOpen[2])
Local $idInputValue, $idSomething, $aFileResult, $idStringBetween

GUISetState(@SW_SHOW, $idGUI)
While 1
    Sleep(20000)
WEnd

Func StartProgressBar()
    If StringInStr($aFileOpen[2], ".txt") = True Then
        $idInputValue = Int(GUICtrlRead($idInput))
        If IsFloat($idCountLines/$idInputValue) = 1 Then
            $idStringBetween = _StringBetween(String($idCountLines/$idInputValue),"", ".")
            $idSomething = Number($idStringBetween[0]) + 1
        Else
            $idSomething = $idCountLines/$idInputValue
        EndIf
        For $i = 1 To $idSomething
            $aFileResult = @ScriptDir & "\result-" & $i & ".txt"
            _FileCreate($aFileResult)
            FileOpen($aFileResult,2)
            For $a = ( ( ( $i - 1 ) * $idInputValue ) + 1 ) To ( $i * $idInputValue )
                If FileReadLine($aFileOpen[2], $a) = "" Then
                    ; ContinueLoop
                    ; Here I have no idea how can I continue with the next $a as if the current $a, I do not skip it because the line is an element less
                EndIf
                GUICtrlSetData($idProgressBar, (100/($i*$a)))
                MsgBox(0,"",(100/($i*$a)))
                FileWrite($aFileResult,FileReadLine($aFileOpen[2], $a) & " - ")
                ; Here I want to set the progress bar value = current percentage (line that it is currently reading) of the 100% (total of lines in the file opened)
            Next
            FileClose($aFileResult)
        Next
        MsgBox(0,"Done","Done")
        Sleep(5000)
        GUICtrlSetData($idProgressBar,0)
    Else
        MsgBox(0,"Hello :)","Please open a file :D")
    EndIf
EndFunc

Func CLOSEButton()
    Exit
EndFunc

With a first text file that contains:

Line1
Line2
Line3
Line4

Line6
Line7
Line8
Line9
Line10
Line11
Line12

And if the user introduces "6" the result is:

  • result-1.txt --> Line1 - Line2 - Line3 - Line4 - Line6 - (five lines, the user wants six)
  • result-2.txt --> Line7 - Line8 - Line9 - Line10 - Line11 - Line12 - (six lines, great)

I have been reading the Help File and I think that there are two options that might work:

  1. Read the text file and create a temporary file without blank lines, and use it to create the final text file.
  2. Try to use _FileReadToArray

What do you think about it? What is most effectively? Are there a better option?

And with that code, the progress bar works wrong :s I didn't find the correct operation, I think that might it works with _FileReadToArray (the total of it would be 100% and the current row/position would be the current percentage).

PS: I also think maybe using _FileReadToArray might be too much work for big text files... I'm wrong?

Share this post


Link to post
Share on other sites

Simply check for a not empty line. also create a variable counter to plus one every time that is not emply line instead using $i.

 

Saludos

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Simply check for a not empty line. also create a variable counter to plus one every time that is not emply line instead using $i.

 

Saludos

That doesn't work because I need to exclude the empty lines, but what you say will return the numbers of non-empty lines and the script will read that numbers of lines but without skip the blank lines.

A good news: Finally I could set the progress bar! It works now! I used "Total Of Lines" / ("Current Line" * 100)

Finally I got the solution!!!!

I created another variable to set what line have to read the script. Here is my full code, I hope be helpful for somebody :)

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <File.au3>
#include <String.au3>
#include <Array.au3>


Local $idGUI = GUICreate("ProgressBar", 220, 130, 100, 200)
Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
Local $idInput = GUICtrlCreateInput("",80,20,60,20,$ES_NUMBER)
Local $idProgressBar = GUICtrlCreateProgress(10, 60, 200, 20)
Local $idButton1 = GUICtrlCreateButton("Start",85,100,50)
GUICtrlSetOnEvent($idButton1, "StartProgressBar")
Local $aFileOpen = _WinAPI_GetOpenFileName("Open a text file", "Text Files (*.txt)")
Local $idCountLines = _FileCountLines($aFileOpen[2])
Local $idInputValue, $idSomething, $aFileResult, $idStringBetween, $LineToRead, $totalNonEmptyLines
For $y = 1 To $idCountLines
    If FileReadLine($aFileOpen[2],$y) <> "" Then $totalNonEmptyLines += 1
Next


GUISetState(@SW_SHOW, $idGUI)
While 1
    Sleep(20000)
WEnd

Func StartProgressBar()
    If StringInStr($aFileOpen[2], ".txt") = True Then
        $idInputValue = Int(GUICtrlRead($idInput))
        If IsFloat($totalNonEmptyLines/$idInputValue) = 1 Then
            $idStringBetween = _StringBetween(String($totalNonEmptyLines/$idInputValue),"", ".")
            $idSomething = Number($idStringBetween[0]) + 1
        Else
            $idSomething = $totalNonEmptyLines/$idInputValue
        EndIf
        For $i = 1 To $idSomething
            $aFileResult = @ScriptDir & "\result-" & $i & ".txt"
            _FileCreate($aFileResult)
            FileOpen($aFileResult,2)
            For $a = ( ( ( $i - 1 ) * $idInputValue ) + 1 ) To ( $i * $idInputValue )
                $LineToRead += 1
                While FileReadLine($aFileOpen[2], $LineToRead) = ""
                    $LineToRead += 1
                    If $a = $idCountLines Then ExitLoop
                WEnd
                GUICtrlSetData($idProgressBar, (($LineToRead*100)/$totalNonEmptyLines))
                FileWrite($aFileResult,FileReadLine($aFileOpen[2], $LineToRead) & " - ")
            Next
            FileClose($aFileResult)
        Next
        MsgBox(0,"Done","Done")
        GUICtrlSetData($idProgressBar,0)
    Else
        MsgBox(0,"Hello :)","Please open a file :D")
    EndIf
EndFunc

Func CLOSEButton()
    Exit
EndFunc

 

Edited by Rhazz
Finally solved!

Share this post


Link to post
Share on other sites

This is an Old post but anyways here an Advise:

Why you dont use Round(), you will avoid unnecessary call to _StringBetween?
Your Code:

If IsFloat($totalNonEmptyLines/$idInputValue) = 1 Then
            $idStringBetween = _StringBetween(String($totalNonEmptyLines/$idInputValue),"", ".")
            $idSomething = Number($idStringBetween[0]) + 1

What I would probably do:
 

If IsFloat($totalNonEmptyLines/$idInputValue) = 1 Then
            $id = Round($totalNonEmptyLines/$idInputValue),0)
            $idnum = Number($id + 1)

This is just an Idea but I think is more efficient.

Kind Regards
Alien
 

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

    • 31290
      By 31290
      Hi guys, 
      Hope you are fine today
      I'm trying to restart a function by calling it back in the script:
      Here's my code so far:
      Func f_VPN() $iInputBox = InputBox("Password", "Please Enter the User's password.") If @Error = 1 Then GUICtrlSetState($fVPN, $GUI_UNCHECKED) Else ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe") $hVPN = WinWait("Cisco AnyConnect Secure Mobility Client", "Ready to connect") ControlClick($hVPN, "", "Button1") $hVPN1 = WinWait("Cisco AnyConnect | SEE VPN", "Cancel") ControlSetText($hVPN1, "", "Edit2", @Username) Sleep(250) ControlSetText($hVPN1, "", "Edit3", $iInputBox) Sleep(250) ControlClick($hVPN1, "", "Button1") Sleep(5000) If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Login Failed" Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button4") WinClose($hVPN) f_VPN() Else ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe") If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Connected To SEE VPN." Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button1") Sleep(1000) WinClose("Cisco AnyConnect Secure Mobility Client", "") ProcessClose("vpnui.exe") ; ProcessClose("explorer.exe") <<< TO UNCOMMENT IniWrite($oIniFile, "LaptopChkBox", "VPN", "1") EndIf EndIf EndFunc I'd like to the function to be restarted in the case the password provided is not correct. 
      I've googled many things but nothing very relevant.
      Any ideas over here?
      Thanks
      -31290
       
    • ronaldo97
      By ronaldo97
      Hello How can I make the GUI is compatible with all screen sizes ??  
      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 623, 445, 192, 124, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZE)) GUISetBkColor(0x1E1E1E) $Label1 = GUICtrlCreateLabel("Welcome Back !!", 424, 64, 161, 29) GUICtrlSetFont(-1, 16, 400, 0, "Tahoma") GUICtrlSetColor(-1, 0x800000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd How do I make Gui interface compatible with all screen sizes ?? 800*600
      1024*768
      1280*1024
    • Genotypek
      By Genotypek
      Hello.
      I've got a problem. I want to center my input vertically, but I don't have any single idea how to do it.
      There's my example code:
      #include <GUIConstantsEx.au3> $GUI = GUICreate("Gui", 237, 93, 192, 124) $Input1 = GUICtrlCreateInput("Center vertically isn't correct", 16, 16, 201, 62) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd What could I do?
      Greetings, "genotypek".
    • argv1
      By argv1
      Hi,
      working on my first autoit GUI an got some questions.
      Is it possible to get an action on the first menu level
      For example:
      $StartMenu = GUICtrlCreateMenu("Datei") ... while 1 Switch GUIGetMsg() Case $StartMenu MsgBox(0, "Test", "yes this works") (currently this does not work for me)
      Currently I have to create GUICtrlCreateMenuItem 's this will leed to a result
       
      And a 2nd question. I saw many time this:
      $StartMenu = GUICtrlCreateMenu("&Datei") For what does I need the ampersand? If I leave out the & nothing seems to change
       
      Thank you very much in advance
       
    • Blueman
      By Blueman
      Hey Guys,
      I am Struggling with a issue that i cannot seem to fix.
      I hope that you guys can help me with this.
      I have a GUI with some text attributes, for all i use the following code;
      $Label_ts_Header = GUICtrlCreateLabel("Manual Adress", 60, 10, 500, 50) GUICtrlSetBkColor($Label_ts_Header, $COLOR_WHITE) GUICtrlSetFont ($Label_ts_Header, 26, 800, 0, "Calibri") This is all working like it should, but when i activate the GUI on another pc the text size is bigger and the GUI size is somewhat smaller.
      The problem is that it isn't on all computers, i can't seem to find the problem,..
      If i make it fit on one pc it is to small on another or to big,..
      What am i doing wrong?
       
      Is there some kind of attribute that i need to add to the top of my script to normalize text sizes in the script?
       
      Thanks!
       
      EDIT: Found the problem guys, some computers has changed the text size to 125% // Can i force 100% for my GUI somehow ?
       
      EDIT2: Fixed it guys! // I used the Function Below to get the DPI Setting and used that to determine the text size (example below)
       
      Func _GDIPlus_GraphicsGetDPIRatio($iDPIDef = 96) _GDIPlus_Startup() Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(0) If @error Then Return SetError(1, @extended, 0) Local $aResult #forcedef $__g_hGDIPDll, $ghGDIPDll $aResult = DllCall($__g_hGDIPDll, "int", "GdipGetDpiX", "handle", $hGfx, "float*", 0) If @error Then Return SetError(2, @extended, 0) Local $iDPI = $aResult[2] Local $aresults[2] = [$iDPIDef / $iDPI, $iDPI / $iDPIDef] _GDIPlus_GraphicsDispose($hGfx) _GDIPlus_Shutdown() Return $aresults EndFunc ;==>_GDIPlus_GraphicsGetDPIRatio  
      $Label_S2 = GUICtrlCreateLabel("Data from The Base", 10, 300, 400, 20) GUICtrlSetBkColor($Label_S2, $COLOR_WHITE) GUICtrlSetFont ($Label_S2, 13 * _GDIPlus_GraphicsGetDPIRatio()[0])