65.9K
CodeProject is changing. Read more.
Home

Define Your Own Dialog Box in Winform

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (5 votes)

Mar 16, 2012

CPOL
viewsIcon

17194

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.

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:

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