Windows Forms
Prompt Dialog
C# Programming
User Interface
Software Development

Prompt Dialog in Windows Forms

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Windows Forms has MessageBox for simple notifications, but it does not have a built-in prompt dialog that directly asks the user for text input in the way many developers expect. In practice, you either build a small modal form yourself or use a helper from another library, with the custom form being the cleaner WinForms-native approach.

Why a custom prompt is common

A prompt dialog usually needs three things:

  • a message label
  • an input box
  • OK and Cancel buttons

That sounds simple, but it is still more than MessageBox provides. Building a tiny reusable form gives you control over validation, default values, focus behavior, and button wiring.

A minimal reusable prompt dialog

Here is a compact helper method that shows a modal prompt and returns the entered text when the user confirms.

csharp
1using System;
2using System.Drawing;
3using System.Windows.Forms;
4
5public static class Prompt
6{
7    public static string? ShowDialog(string text, string caption)
8    {
9        Form prompt = new Form()
10        {
11            Width = 360,
12            Height = 160,
13            Text = caption,
14            FormBorderStyle = FormBorderStyle.FixedDialog,
15            StartPosition = FormStartPosition.CenterParent,
16            MinimizeBox = false,
17            MaximizeBox = false
18        };
19
20        Label message = new Label() { Left = 12, Top = 15, Width = 320, Text = text };
21        TextBox input = new TextBox() { Left = 12, Top = 45, Width = 320 };
22        Button ok = new Button() { Text = "OK", Left = 176, Width = 75, Top = 80, DialogResult = DialogResult.OK };
23        Button cancel = new Button() { Text = "Cancel", Left = 257, Width = 75, Top = 80, DialogResult = DialogResult.Cancel };
24
25        prompt.Controls.Add(message);
26        prompt.Controls.Add(input);
27        prompt.Controls.Add(ok);
28        prompt.Controls.Add(cancel);
29        prompt.AcceptButton = ok;
30        prompt.CancelButton = cancel;
31
32        return prompt.ShowDialog() == DialogResult.OK ? input.Text : null;
33    }
34}

Usage is straightforward:

csharp
1string? name = Prompt.ShowDialog("Enter your name:", "User Input");
2if (name != null)
3{
4    MessageBox.Show($"Hello, {name}");
5}

Why modal dialogs work well here

A prompt dialog is usually modal because the program should wait for the answer before continuing. ShowDialog() blocks interaction with the owner window until the prompt is closed, which is usually the right UX for short required input.

If the input is optional or should not interrupt the main workflow, a panel or side form may be better than a modal prompt.

Improving the basic prompt

Once you have a small custom form, it is easy to extend:

  • prefill the textbox with a default value
  • validate the text before closing
  • add masked input for passwords
  • add a multiline text box for longer content

For example, a simple validation rule can keep the dialog open when the value is empty instead of returning bad input immediately.

The Visual Basic InputBox option

Some WinForms projects call Microsoft.VisualBasic.Interaction.InputBox(...). That works, but it is usually more of a convenience shortcut than a great design choice for a C# WinForms application.

A custom form is clearer, easier to style, and easier to maintain when requirements grow beyond a trivial one-line prompt.

Common Pitfalls

The biggest mistake is using MessageBox for a problem that actually needs input. MessageBox is for acknowledgement, not data entry.

Another issue is forgetting to set AcceptButton and CancelButton. Without those, the prompt feels less natural because Enter and Escape do not behave as users expect.

It is also easy to return an empty string and treat it as valid input accidentally. If blank input is not allowed, add validation rather than trusting the textbox content blindly.

Finally, prompt dialogs are best for short pieces of input. If the user needs several fields or rich validation, create a real form instead of stretching a tiny prompt beyond its purpose.

Summary

  • WinForms does not provide a built-in text prompt dialog equivalent to MessageBox.
  • The usual solution is a small custom modal form with a label, textbox, and buttons.
  • 'ShowDialog() is appropriate for short blocking input flows.'
  • A custom prompt is easier to validate and extend than a quick convenience hack.
  • Use a full form instead when the interaction grows beyond a single simple value.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.