Rashim's Blog

Define your own dialog box in win form

Posted on: October 7, 2011

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 has been given below,

  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 set 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;
            }
        }

        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 like the way,

List<Test> currentList=null;

        if (FrmDialog.GetData (ref currentList)== DialogResult.OK)
        {
            ////Do Whatever you want
        }
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 37 other followers

%d bloggers like this: