views:

1062

answers:

2

I'm building a winform in C# with various elements in a panel that start out either invisible, disabled, or set to null (labels, combo boxes, grids, etc.). As the user goes through and makes choices, these elements are populated, selected, etc.

The idea is to upload files, read them, and process entries to a database. Once the processing for this directory has completed, I'd like to be able to have the user select another directory without exiting and restarting the winform app, by pressing a button that becomes visible when the process has completed.

Is there an easy call to reset the application (or the panel that contains the elements), similar to when a webform is refreshed, or do I have to write a function that "resets" all of those elements one at a time?

EDIT: As the result of a development meeting, my project has changed direction. I thank the two of you who helped with answers, and am going to close the question.

+1  A: 

You could try calling this.InitializeComponent(), which may do the trick. Alternately, if your application has a 'Directory Select' form and a 'Process Files' form, you could have the Directory Select form do a "new" on the Process Files form, which should return it to its original state (not while its open, though).

GWLlosa
It threw an error because (I think) the button calling it was not the object to re-initialize.
John Dunagan
If you're on a form, and you call 'this', it should refer to the form. What error?
GWLlosa
Maybe that's my problem. It turns out, however, that as the result of a development meeting, they want the datagrid to persist so that records can be added, so I can't reinit the tabpage panel. Thanks, anyway.
John Dunagan
+2  A: 

Simple remove panel from the form and create the new one.

EDIT: Sample:

Panel CreatePanelWithDynamicControls() {
    Panel ret = new Panel();
    ret.Dock = DockStyle.Fill;
    // some logic, which initialize content of panel

    return ret;
}

void InitializeDynamicControls() {
    this.Controls.Clear();
    Panel pnl = this.CreatePanelWithDynamiControls();
    this.Controls.Add( pnl );
}

void Form1_Load( object sender, EventArgs e ) {
    if ( !this.DesignMode ) {
        this.InitializeDynamicControls();
    }
}

// I don't know exactly, on which situation
// do you want reset controls
void SomeEvent( object sender, EventArgs e ) {
    this.InitializeDynamicControls();
}
TcKs
I'm trying this - I think it might work. If it does, I'm marking you Best Answer.
John Dunagan
It completed, but it didn't reset the elements in the TabPage, and it put it at the end. I may have to enclose all of the elements in another panel, and then remove and add just the panel(s).
John Dunagan
This was almost there, so in the spirit of not seeing that annoying red message, I'm marking it best.
John Dunagan