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.
- Design your FrmMain.cs
- Design your FrmDialog.cs as a Dialog Window
- From the property window, set
controlBox=False
- 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)
{
}