Refresh/Update Form1 during loop?

Gladimir

Regular
Joined
Mar 29, 2003
Messages
60
Location
San Diego, CA
I have a fairly long loop as I process records in a database table. In that loop, I update the Text properties of a few labels controls, but I only get to see the updates made for the last record when all the processing is done.

How do I go about refreshing/updating the controls on Form1?
 
put a DoEvents inside your loop, but keep in mind that the user can click/use other controls on the form during the loop, so you may want to disable some controls untill you finish.
 
If you use an Application.DoEvents, inside the loop, it will
allow other messages (e.g. painting to get through). You better
disable or hide the necessary controls though, because your user
will still be able to click on buttons, which can really mess things
up.

[edit]D'oh![/edit]
 
Worked...

The Application.DoEvents worked perfectly. Good advice on disabling unnecessary controls, but this is a status form for my own benefit; I can screw my programs up quite well without buttons. ;)
 
You can also use "this.Refresh()" (in C#) or "Me.Refresh()" (in VB.NET). This will cause a repainting of the form and all of it's controls but won't let any other windows messages get through (such as button clicks).

You can also use the Refresh method on any single control, such as the TextBox or Label that you're updating. But you may see some strange artifacts by doing this - such as a form that turns ALL battleship grey except the individual controls you're Refreshing. I only mention this becaus calling "this.Refresh" may provide unwanted flicker if you have a large form with a lot of controls.

Of course, this is only for you for now. But if it ever goes to another user and they're not as forgiving as you, be ready to change a line or two of code :)

-Nerseus
 
Would it not be better to spawn a thread and do the heavy db work in that? Then when a ui component is changed marshal a call to the ui event q thread with BeginInvoke?
 
Back
Top