user9108838
user9108838

Reputation:

custom confirmation dialog in C#

I am new to C# Development. I'm having an issue related to windows forms where I need to show a custom confirm dialog. Cannot use the traditional message box in C#. Also there have to have two string values passing in to the custom confirm dialog.

So far I managed to create a basic instance confirm dialog via forms(). Below is the code :

Form prompt = new Form()
{
    Width = 500,
    Height = 150,
    FormBorderStyle = FormBorderStyle.FixedDialog,
    Text = caption,
    StartPosition = FormStartPosition.CenterScreen,

};

I have no clue whats so ever of what to do after this. Please help.

Upvotes: 0

Views: 4064

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82534

Well, don't just create an instance of Form, that's not going to help you very much unless you will add all the controls programmatically, on run time.

What you want to do is add a form to your application, that will act as your dialog box. To this form you will add whatever controls you want to show on it at design time, and also set all the other design elements such as height and width, start position and form border (The caption you might want to change in code, but that shouldn't stop you from setting a default caption). Then when you need to show the dialog just create an instance of that form, pass whatever you need to it (Usually, it would be a string to show to the user and perhaps another parameter to control what buttons you want to show etc').

Be sure to show this form using ShowDialog when showing your dialog form, and have whatever buttons you want on your dialog form set the form's DialogResult property to whatever value you need (Ok, Cancel, Yes, No etc').

Here is a very basic example:

For a dialog that has a yes and no buttons, with a customizable caption and text:

Custom dialog

There is a label called message, and the buttons are called yes and no. Note: the StartPosition property of the form is set to CenterParent.

Here is the form's code:

public partial class CustomDialog : Form
{

    // This static method is the equivalent of MessageBox.Show
    public static DialogResult ShowDialog(IWin32Window owner, string caption, string text)
    {
        // Setting the DialogResult does not close the form, it just hides it. 
        // This is why I'm disposing it. see the link at the end of my answer for details.
        using(var customDialog = new CustomDialog(caption, text))
        {
            return customDialog.ShowDialog(owner);
        }
    }

    // private constructor so you don't accidentally create an instance of this form
    private CustomDialog(string caption, string text)
    {
        InitializeComponent();
        this.Text = caption;
        this.message.Text = text;
    }

    // Handle the click event of the `yes` button
    private void yes_Click(object sender, EventArgs e)
    {
        // This will automatically close the form
        this.DialogResult = DialogResult.Yes;
    }

    // Handle the click event of the `no` button
    private void no_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.No;
    }
}

To use it in your other forms, instead of MessageBox.Show, you do this:

var result = CustomDialog.ShowDialog(this, "title", "Hello. Did you get it?");

Now result contains one of the following:

  • DialogResult.Yes - The user clicked the yes button
  • DialogResult.No - the user clicked the no button
  • DialogResult.Cancel - the user closed the window (clicked the x on the window border) - Note I didn't have to write any code for this - this is the value assigned by the framework when the form is shown as a dialog.

Upvotes: 4

Related Questions