Click here to Skip to main content
15,881,423 members
Articles / Desktop Programming / WPF

Define Your Own Dialog Box in Winform

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
19 Mar 2012CPOL 16.7K   6   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
}
This article was originally posted at http://rashimuddin.wordpress.com/2011/10/07/hello-world

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer RightKnack Limited
Bangladesh Bangladesh
A big fan of getting down the latest cutting-edge technologies to the ground to innovate exceptionally amazing ideas.

My Blog: http://rashimuddin.wordpress.com/

My Email: rashimiiuc at yahoo dot com

Comments and Discussions

 
QuestionHello Pin
Yaseer Arafat19-Dec-12 11:02
professionalYaseer Arafat19-Dec-12 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.