Login


Dynamically Creating a WinForms Dialog

By Jonathan Wood on 12/31/2010
Language: C#
Technology: WinForms
Platform: Windows
License: CPOL
Views: 17,547
Frameworks & Libraries » WinForms » General » Dynamically Creating a WinForms Dialog

Demo Project Screenshot

Download Source Code Download Source Code

Introduction

Although Visual Studio provides nice form designers that makes it easy to create your application's forms, everything created by the designers can also be done directly from code.

In most cases, I would recommend sticking with the designers when adding new forms to your application. But there are a cases where a code solution might be best. For example, what if you simply need to get some text from the user and don't want to add yet another form to your project? Or perhaps you need many forms that are very similar. It may make sense to use a single form and write code to alter that form's appearance and behavior as needed by your application.

The InputBox Class

Listing 1 shows my InputBox class. This class dynamically creates a small dialog box with a prompt and a text box. The user can type in text and click OK or Cancel.

The prompt is specified in the argument list so you could use this same class for soliciting different text values from the user. And the form is created on the fly so there isn't yet another form to add to your project.

InputBox contains the single, static method Show(), which first creates the dialog box and then displays it.

You specify the dialog's title, prompt and the initial text value. If Show() returns Dialog.OK, then the user selected OK and the string typed by the user is returned. Show() returns Dialog.Cancel if the user selected Cancel.

Listing 1: The InputBox Class

class InputBox
{
    /// <summary>
    /// Displays a dialog with a prompt and textbox where the user can enter information
    /// </summary>
    /// <param name="title">Dialog title</param>
    /// <param name="promptText">Dialog prompt</param>
    /// <param name="value">Sets the initial value and returns the result</param>
    /// <returns>Dialog result</returns>
    public static DialogResult Show(string title, string promptText, ref string value)
    {
        Form form = new Form();
        Label label = new Label();
        TextBox textBox = new TextBox();
        Button buttonOk = new Button();
        Button buttonCancel = new Button();

        form.Text = title;
        label.Text = promptText;
        textBox.Text = value;

        buttonOk.Text = "OK";
        buttonCancel.Text = "Cancel";
        buttonOk.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;

        label.SetBounds(9, 20, 372, 13);
        textBox.SetBounds(12, 36, 372, 20);
        buttonOk.SetBounds(228, 72, 75, 23);
        buttonCancel.SetBounds(309, 72, 75, 23);

        label.AutoSize = true;
        textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
        buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new Size(396, 107);
        form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
        form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;

        DialogResult dialogResult = form.ShowDialog();
        value = textBox.Text;
        return dialogResult;
    }
}

Using the Class

Listing 2 shows sample code that calls the InputBox class.

The code sets value to the text that should initially appear in the dialog's text box. If DialogResult.OK is returned, value contains the text entered by the user.

Listing 2: Using the InputBox Class

string value = txtInputString.Text;

if (InputBox.Show("Test Input Box",
    "&Enter a new string value:", ref value) == DialogResult.OK)
{
    txtInputString.Text = value;
}

Conclusion

Simple enough. I've found this class to be useful on a number of occasions. Perhaps you will too.

NOTE: This article was derived from the code posted at http://www.csharp-examples.net/inputbox/ with the consent of the original author.

End-User License

Use of this article and any related source code or other files is governed by the terms and conditions of The Code Project Open License.

Author Information

Jonathan Wood

I'm a software and website developer working out of the greater Salt Lake City area of Utah. I've developed many websites including Black Belt Coder, Trail Calendar, and others.

I hike each week with my dogs Suki and Sasha. You can see my hiking blog at Hiking Salt Lake.