Modal Forms in C#
Submitted by joken on Tuesday, May 13, 2014 - 11:46.
This tutorial will show you how to use the Modal Forms in C#. in C# instead of using a method “Show()” we will change it into “ShowDialog()”method and this creates a Modal form. This Modal form is something you need deal with before you can proceed with your program process.
To display a dialog box modally, here’s the following code:
In C#, a ShowDialog method has an optional argument that can be used to specify a parent-child relationship for a form.
At this time, let’s create a new project called “modal” then on the form1 design it look like as shown below:
Then add another from and design it looks like as shown below:
Next double click the “Ok” button and the code view will appear. Then add the following code:
The code above will record the result of the button click, and set it to OK. Then double click the “Abort” button and add the following code:
At this time, let’s go back to Form1 then double click the “Click Me!” button and add the following code:
The code above will simply checks to see if the OK or the Abort button is clicked. If so, it display a message. you can now try to run the application and test it. In my example, I just simply click the “OK” button and the output looks like as shown below.
- formDialog.ShowDialog();
- private void ok_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.OK;
- }
- private void abort_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Abort;
- }
- formDialog.ShowDialog();
- if (formDialog.ShowDialog() == DialogResult.OK)
- {
- MessageBox.Show("OK button is click!");
- }else if (formDialog .ShowDialog () == DialogResult .Abort )
- {
- MessageBox.Show("Abort button is click!");
- }
Add new comment
- 805 views