Jump to content

Difficulty in making a progress bar move


triodz
 Share

Recommended Posts

I have been trying to get a progress bar to, well, progress. I have looked at several scripts on here and most of them relate to copying files across, which I do not want to do. I tried chopping it up to suit my needs, but just break them.

I simply want the user to press the "Start" button and the bar to run for a few seconds (15 or 20 seconds. Less is fine), then show them a completed message.

Here is what I have so far:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ###
Opt("GUIOnEventMode", 1)
$Form1_1 = GUICreate("Form1", 506, 223, 236, 128)
$Exit = GUICtrlCreateButton("Exit", 336, 144, 129, 41)
GUICtrlSetOnEvent($GUI_EVENT_CLOSE, "close");
$Button1 = GUICtrlCreateButton("Start", 184, 144, 129, 41)
GUISetState (@SW_SHOW)
$Progress1 = GUICtrlCreateProgress(16, 104, 465, 17)
GUISetState()
#EndRegion ### END Koda GUI section ###

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

Case $Exit
EndSwitch
WEnd
$wait = 20
$s = 0
do
$nmsg = GUIGetMsg()
If $nmsg = $Button1 Then
    For $i = $s To 100
    If GUICtrlRead($progress1) = 100 Then Msgbox(0,"Info","Complete!", 1)
    $m = GUIGetMsg ()

    If $m = -3 Then ExitLoop

        $s=0
      GUICtrlSetData ($progress1,$i)
      Sleep($wait)
    Next
    if $i >100 then
    endif
EndIf
until $nmsg = $GUI_EVENT_CLOSE
func close ()
Exit
EndFunc

Pressing the exit button works fine. The bar is there, but no progress shows. It's more for the user so they know something is happening. All I want it to do is turn green from start to finish when "Start" is pressed.

I am very new and I know the aesthetics need work, but I just need to get over this hurdle first.

Thanks for any help on this one. I have had nothing but wonderful help from this community in the past!

Thanks in advance

Link to comment
Share on other sites

triodz, couple of things to point out.

First you shouldnt use GUIOnEventMode and GUIGetMsg() in the same script. Pick one or the other. Also the reason nothing happens with the progress bar is the script never reaches that far. After your gui is create, your script goes into an endless "while" loop. Put your progress bar code into a function and then call that function when the start button is pressed.

Link to comment
Share on other sites

Thanks Beege,

I tried the below with your suggestions, but still having trouble. I just don't know what I'm doing wrong.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### 
Opt("GUIOnEventMode", 1);
$Form1_1 = GUICreate("Form1", 506, 223, 236, 128)
$Exit = GUICtrlCreateButton("Exit", 336, 144, 129, 41)
GUIctrlSetOnEvent($GUI_EVENT_CLOSE, "close")
$Button1 = GUICtrlCreateButton("Start", 184, 144, 129, 41)
GUIctrlSetOnEvent($GUI_EVENT_CLOSE, "progress")
GUISetState (@SW_SHOW)
$Progress1 = GUICtrlCreateProgress(16, 104, 465, 17)
GUISetState()
#EndRegion ### END Koda GUI section ###

func progress ()
$wait = 20
$nmsg = GUIGetMsg()
If $nmsg = $Button1 Then
    For $i = 0 To 100
    If GUICtrlRead($Progress1) = 100 Then Msgbox(0,"Info","Complete!", 1)
Next
EndIf
EndFunc
func close ()
Exit
EndFunc
While 1
sleep(10)
WEnd

It's driving me nuts!

Link to comment
Share on other sites

Ok.. that code is better though. You removed alot of the code that was not being used at all. But you still using GUIOnEventMode, and GUIGetmsg() in the same script. GUIOnEventMode lets you assign a function to be called whenever a certian event happens. GUIGetMsg() is designed to retrive the event msgs and then let you handle what happens with that event. Using both at the same time kinda makes them fight each other. Understanding these concepts will become more clear in time.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ###
;Use OnEventMode
Opt("GUIOnEventMode", 1);

;Create GUI
$Form1_1 = GUICreate("Form1", 506, 223, 236, 128)

;create Exit button
$Exit = GUICtrlCreateButton("Exit", 336, 144, 129, 41)
;If exit button is pressed, call "close" function
GUICtrlSetOnEvent($Exit, "close")

;If a close event happens call "close" function
GUICtrlSetOnEvent($GUI_EVENT_CLOSE, "close")

;create start button
$Button1 = GUICtrlCreateButton("Start", 184, 144, 129, 41)
;If $button1 is pressed call function "progress"
GUICtrlSetOnEvent($Button1, "progress")

;create progress bar
$Progress1 = GUICtrlCreateProgress(16, 104, 465, 17)

;Show the gui
GUISetState()
#endregion ### END Koda GUI section ###

;loop around and just wait for any events to happen
While 1
Sleep(10)
WEnd

Func progress()
$wait = 20
For $i = 0 To 100
  GUICtrlSetData($Progress1, $i)
  Sleep($wait)
Next
EndFunc   ;==>progress

Func close()
  Exit
EndFunc   ;==>close
Edited by Beege
Link to comment
Share on other sites

Your welcome. :) Look into starting to use GUIGetMsg(). Oneventmode is easy, but limits you. For example, change $wait to 100. Then while the progress bar is increasing try and press the exit or close button. You will notice a small problem...

Link to comment
Share on other sites

Your welcome. :) Look into starting to use GUIGetMsg(). Oneventmode is easy, but limits you. For example, change $wait to 100. Then while the progress bar is increasing try and press the exit or close button. You will notice a small problem...

Wouldn't using the message loop mode display the same problem? While the function is running, no button presses will be activated until that function ends. OnEventMode causes you to create more functions than MessageLoop mode, but works easier when dealing with multiple GUIs, as an example, for the new user.

I'd learn to use the GUIRegisterMsg functions so that you can eliminate 90% of the delay problems exhibited with long running functions you want to interrupt. Then it won't matter if you're using messageloop or onevent modes.

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

Wouldn't using the message loop mode display the same problem?

Yes it would, but using msg loop mode you can fix the problem much easier by adding msg checks with in the function. Its harder to pull that off using only onevent mode. (as you pretty much state in the next comment) :)

I'd learn to use the GUIRegisterMsg functions so that you can eliminate 90% of the delay problems exhibited with long running functions you want to interrupt. Then it won't matter if you're using messageloop or onevent modes.

Not sure I follow. Your saying learn to use GUIRegistermsg (message loop), then it wont matter if your using message loop or onevent?
Link to comment
Share on other sites

GUIRegisterMsg (monitoring Windows messages) isn't the same as using GUIGetMsg (message loop mode).

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

Ohhh. My bad. I read and typed GUIRegistermsg thinking GuiGetMsg(). :) You absolutely right.

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