# C# & Windows Forms

## Recommended Posts

OK...so, I'm trying to create an app that has a nice little menu bar, and one of the options brings up a preferences dialog where you can set....settings. Problem is, how do I make clicking this option bring up a new windows form? I'm assuming I want a new windows form with all the proper controls, set up so that when I hit "Apply" it saves the changes (in my case to a txt file for simplicity) and closes the form. How might I do this? Am I using the right method, or should I be using a usercontrol? I'm a little lost here. Thanks! RA

##### Share on other sites
juanpaco    150
A new form should be just fine. In the handler for the menu option, you can create an instance of the new form and then call Show() or ShowDialog() on it. Just doing Show() would require a more detailed design, but it would free your users from the evil we all call "modal dialog boxes." The new form you create would need to have some sort of reference back to the where you're storing the settings the user will be changing on it though.

That's a pretty high-level explanation. Look into those two functions... try calling them on a form to get a feel for how they work. If you have more questions, post 'em!

Happy coding!

##### Share on other sites
ApochPiQ    23059
In the Project menu, select Add Windows Form. Follow the dialogs through and generate the new form and class.

Once it is generated, you can access it easily in code:

FormClassName form = new FormClassName();form.ShowDialog();

##### Share on other sites
Xeile    256
Hello Rampantandroid,

Do you have the 'pop-up' dialog already defined (did you made a separated form with all the controls in it), or do only have the main form?

If it is the first situation, you can create and show this very simple:
// MyApplication is your own namespace// SettingsDialog is the form class you defined, the DialogSystem.Windows.Forms.Form NewDialog = new MyApplication.SettingsDialog(...);NewDialog.Show(); // Thats all

if it is the latter situation you need to add the controls at runtime

System.Windows.Forms.Form NewDialog = new System.Windows.Forms.Form();Form.Controls.Add(new Button("Apply"));Form.Controls.Add(new CheckBox("God mode: "));// ...NewDialog.Show();

Hope this helps you in on the way, if not feel free to ask :D

Regards,

Xeile