Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

List box or Label info show immediately

I have a Label and a List Box.  I have a command.click sub that has a loop sending mail.  In the loop, with each iteration, I change the text in the label and list box to keep track of the progress.  The problem is that it only displays after the loop has finished.  I also have a label that says "Starting" but it doesn't display either until the whole sub is finished.

How can I make them change their displays immediately in the loop?
0
donb1
Asked:
donb1
1 Solution
 
ggilmanCommented:
Put a DOEvents in the middle of the loop. This will allow the contents to be updated.
0
 
PatrickVDCommented:
Hi donb,

Try to add a DoEvents statement just after the update of the Label Caption property, this will probably fix your problem.

Label1.Caption = "Starting..."
DoEvents
Do While Condition= True
   Label2.Caption = "Item Name"
   List1.AddItem "Item Data"
   DoEvents
   'Do your process intensive work now
Loop

The problem is that due to the processing loop, you are not giving away enough time to the window handler to update your user interface.

Try it, I'm sure it will solve your problem !

Patrick.
0
 
dmridgCommented:
If you insert the "Repaint" method after each label change it will force the repaint of the screen.

For example

Label1.Caption = "New Value"
Me.Repaint
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mcriderCommented:
donb1,

You will find that if you place a "DoEvents" in your loop, your program will update the listbox and the label, HOWEVER, you will also find that you will be able to click the button that started the loop more than once and your loop will be kicked of each time you click it... while the first iteration of the loop is running. This will DEFINATELY cause problems with what you are doing in the loop.

You can solve this problem two ways...

1) If you are going to perform a "DoEvents", then do it like this:

Command1.Enabled = False
Label1.Caption = "Starting..."
DoEvents
Do While Condition= True
   Label2.Caption = "Item Name"
   List1.AddItem "Item Data"
   DoEvents
   'Do your process intensive work now
Loop
Command1.Enabled = True

Also remember that you will be able to click other buttons and menus while the loop is running... If this will interfere with the loop running, then you may actually want to do this instead:


Form1.Enabled = False
Label1.Caption = "Starting..."
DoEvents
Do While Condition= True
   Label2.Caption = "Item Name"
   List1.AddItem "Item Data"
   DoEvents
   'Do your process intensive work now
Loop
Form1.Enabled = True


HOWEVER... There is another way to get your label and listbox to update without using "DoEvents"...

Inside your loop, explicitly call a refresh of the label and list.  For example:

Do While Condition= True
   Label2.Caption = "Item Name"
   Label2.Refresh
   List1.AddItem "Item Data"
   List1.Refresh
   'Do your process intensive work now
Loop


Cheers!®©
0
 
mcriderCommented:
dmridg, REPAINT is not a valid statement...
0
 
donb1Author Commented:
Repaint doesn't work

mcrider had the best answer and should get the points.  DOEvents is the easy answer and works except for the problems mcrider pointed out.

Thanks to the others for their comments too.
0
 
donb1Author Commented:
Thanks
0
 
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now