[C#] Updating the form half way through a function?

Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
[C#] Updating the form in real time?

I've written a little backup utility in C# which copies the contents of one directory to another with several options, and I want to be able to add an item to a listbox for each file that is copied, in real time.

So far, I have a class that does the file copying, and raises an event every time a file is copied, which is handled back in the form file, and an item is added to the listbox. However, the changes can't be seen until the backup object has finished doing its thing and control has passed back into and through the buttonBackup_Click() event handler.

Is there a way of getting the changes to be visible as the files are being copied? :confused:

Any help appreciated :)
 
Last edited:
Soldato
Joined
18 Oct 2002
Posts
3,157
Location
Leeds
Just stick "Application.DoEvents();" directly after you've done your form updates and that should sort it for you.

To do what you're doing properly though you'd normally make the application threaded, doing the actual work in another thread and make the changes to the main form using a delegate.

Mick.
 
Soldato
OP
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Mickey said:
To do what you're doing properly though you'd normally make the application threaded, doing the actual work in another thread and make the changes to the main form using a delegate.
I did think of that, but I thought it would probably be overkill for such a simple application, and I'm probably not experiened enough with C# to do that yet. I'll look into it though :)
 
Soldato
Joined
18 Oct 2002
Posts
3,157
Location
Leeds
Inquisitor said:
Brilliant, thanks :)

One more question, is there a way of scrolling to the bottom of the ListBox after each update?
You can get this behaviour using the SelectedIndex or SelectedItem properties of the listbox.

Something like:

listbox1.SelectedIndex = listbox1.Items.Count - 1;

If you don't want to be changing the selected item in the list box you'll have to call some windows API functions to send a message to the listbox AFAIK.


Mick.
 
Soldato
OP
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
I found a way of doing it without Selecting the bottom item actually:
Code:
int maxVisibleItems = listBoxProgress.Height / listBoxProgress.ItemHeight;
listBoxProgress.TopIndex = listBoxProgress.Items.Count - maxVisibleItems;
Works perfectly!

Thanks for the help :)
 
Soldato
Joined
18 Oct 2002
Posts
3,157
Location
Leeds
Inquisitor said:
I did think of that, but I thought it would probably be overkill for such a simple application, and I'm probably not experiened enough with C# to do that yet. I'll look into it though :)
Threading will usually make a massive difference to performance too you know, even on smallish tasks like this. Even just one worker thread ;)

Mick.
 
Soldato
Joined
18 Oct 2002
Posts
3,157
Location
Leeds
Inquisitor said:
I found a way of doing it without Selecting the bottom item actually:
Code:
int maxVisibleItems = listBoxProgress.Height / listBoxProgress.ItemHeight;
listBoxProgress.TopIndex = listBoxProgress.Items.Count - maxVisibleItems;
Works perfectly!

Thanks for the help :)
Ah, didn't spot that property. Its a while since i've worked on any windows forms projects. Thats my excuse anyway :p


Mick.
 
Soldato
OP
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Edit: Nevermind, got it all working now! Much faster than it was before, and the UI is fully responsive during backup :cool:
Hurrah for multithreading, thanks for the help all :D
 
Last edited:
Back
Top Bottom