1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'HTML, Graphics & Programming' started by Inquisitor, 3 Feb 2006.

  1. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    [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: 3 Feb 2006
  2. Mickey

    Mobster

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

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    Brilliant, thanks :)

    One more question, is there a way of scrolling to the bottom of the ListBox after each update?
     
  4. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    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 :)
     
  5. Mickey

    Mobster

    Joined: 18 Oct 2002

    Posts: 3,157

    Location: Leeds

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

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    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 :)
     
  7. Mickey

    Mobster

    Joined: 18 Oct 2002

    Posts: 3,157

    Location: Leeds

    Threading will usually make a massive difference to performance too you know, even on smallish tasks like this. Even just one worker thread ;)

    Mick.
     
  8. Mickey

    Mobster

    Joined: 18 Oct 2002

    Posts: 3,157

    Location: Leeds

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


    Mick.
     
  9. roboffer

    Sgarrista

    Joined: 18 Oct 2002

    Posts: 9,599

    Location: Sunderland

    Just another note to agree with Mickey, you really should look into threading, for something like this it wont be too complicated :)
     
  10. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    Aye I'm very interested in it and will look into it :)
     
  11. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    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: 4 Feb 2006