Custom dialog in C #?

I have a button that, when clicked, opens a dialog box with various controls, such as radio buttons and text fields. If OK, then the values ​​in this dialog box are passed back to the button, and the rest of the code is processed with these values. If Cancel, do nothing.

How can i do this? I was thinking of creating another form with these controls, and this button invokes the new form, but I want the rest of the controls to stop until the form is completed, like a dialog box.

+8
c # winforms dialog
source share
5 answers

1.) Create the form you talked about with all the necessary user interface elements. Also add the OK and Cancel buttons to it.

2.) On the properties panel for the OK and Cancel buttons, set the DialogResult values ​​to OK and Cancel, respectively. In addition, you can also set the Form CancelButton property as the name of the created Cancel button.

3.) Add additional properties to the dialog that corresponds to the values ​​you want to return.

4.) To display a dialog box, do something in the lines

using( MyDialog dialog = new MyDialog() ) { DialogResult result = dialog.ShowDialog(); switch (result) { // put in how you want the various results to be handled // if ok, then something like var x = dialog.MyX; } } 
+31
source share

Can you do this. Create a new form. From your main form, you can call the user form using:

 CustomForm customForm = new CustomForm(); customForm.ShowDialog(); 

Make sure you add the appropriate button to the user form and set your DialogResult property to OK, Cancel, or something else.

+4
source share

Somewhere in the code that has the dialog, you can also explicitly set the result. For example, you can put the following code in a button click event handler.

 OnOKButton_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Dispose(); } 
+1
source share

One way is to create an event in the form of a dialogue. Depending on how many values ​​you want to send back, you can simply have parameters in the event deletion. The best way is to create a small class or structure for arguments containing a list of properties that you want to return.

If you click OK, you will fire up the event with values ​​from the dialog box. To cancel, the event does not fire.

In the form, using the button, you attach a handler for the event. This gets your values, and you can do whatever you need with them.

http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

0
source share
  • Add buttons to a Windows form. I usually name the buttons as cmdOK or cmdCancel

enter image description here

  • Define the Cancel and OK buttons in the dialog form

enter image description here

0
source share

All Articles