Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Define Your Own Dialog Box in Winform

0.00/5 (No votes)
19 Mar 2012 1  
How to define your own dialog box

Say, you have two Forms, namely FrmMain and FrmDialog and you want to show up FrmDialog from FrmMain. When you click OK/Yes button, then it might return DialogResult and Fill Up some variables data that you need in your FrmMain for further processing. So what should we do in this case? Let’s move forward in a step by step manner.

  1. Design your FrmMain.cs
  2. Design your FrmDialog.cs as a Dialog Window
    1. From the property window, set controlBox=False
    2. Add two Buttons, Ok and Cancel

In the below code, GetData() method opens up the Dialog window. When we click Okay/Cancel button, it sets up DialogResult for current window, also fill-up the variable data as well.

C#
private static List<Test> currentList=null;
public static bool GetData(ref List<Test> referenceList)
{
FrmDialog frm = new FrmDialog();
if (frm.ShowDialog() == DialogResult.OK)
{
referenceList = currentList;
}
this.DialogResult
}
private void BtnOk_Click(object sender, EventArgs e)
{
currentList=this.GetData();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void BtnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}

Call the GetData() method from FrmMain.cs in this way:

C#
List<Test> currentList=null;
if (FrmDialog.GetData (ref currentList)== DialogResult.OK)
{
////Do Whatever you want
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here