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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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();
}
Method 2
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).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0