80

Is it possible to show (pop-up) a message box with an input field in it, possibly a text box? Is somewhere in the language or the framework?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Sunscreen
  • 3,452
  • 8
  • 34
  • 40
  • See this: http://social.msdn.microsoft.com/Forums/en-SG/winforms/thread/191ddf61-3ae5-4845-b852-56bb9b77238a – Adam May 29 '12 at 11:00
  • 1
    Here are two examples, one basic and another with input validation: 1. basic - http://www.csharp-examples.net/inputbox/ 2. validation - http://www.csharp-examples.net/inputbox-class/ – CodeMonkey Apr 11 '13 at 13:30

2 Answers2

108

You can reference Microsoft.VisualBasic.dll.

Then using the code below.

Microsoft.VisualBasic.Interaction.InputBox("Question?","Title","Default Text");

Alternatively, by adding a using directive allowing for a shorter syntax in your code (which I'd personally prefer).

using Microsoft.VisualBasic;
...
Interaction.InputBox("Question?","Title","Default Text");

Or you can do what Pranay Rana suggests, that's what I would've done too...

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
animaonline
  • 3,715
  • 5
  • 30
  • 57
  • 2
    I am using .net 2 and C# 2.0. It cannot find .dll. Any ideas? – Sunscreen May 29 '12 at 11:08
  • 13
    Upgrade? And if you can't - change job. I feel for you, bro. I had an assignment once. We were sitting on 1.something. It felt like we would start to rediscover fire and wheel any time soon... – Konrad Viltersten Sep 13 '12 at 18:14
  • 1
    Doesn't work on .net 4.5 – ave Dec 06 '14 at 19:30
  • 3
    @ardaozkal it does, are you sure you have referenced Microsoft.VisualBasic? – animaonline Dec 08 '14 at 09:54
  • 1
    I did, but I realised that I should also reference it from the framework tab of the add referance button (found this out after I commented), It works now. Thx. – ave Dec 08 '14 at 22:46
67

You can do it by making form and displaying it using ShowDialogBox....

Form.ShowDialog Method - Shows the form as a modal dialog box.

Example:

public void ShowMyDialogBox()
{
   Form2 testDialog = new Form2();

   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if (testDialog.ShowDialog(this) == DialogResult.OK)
   {
      // Read the contents of testDialog's TextBox.
      this.txtResult.Text = testDialog.TextBox1.Text;
   }
   else
   {
      this.txtResult.Text = "Cancelled";
   }
   testDialog.Dispose();
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 3
    I like this because it doesn't require using the VisualBasic library and gives more granular control over the form and appearance of the dialog box. One note: Be sure to make the access on the text box (TextBox1 in the example) public in the code behind file for the dialog. – Steve Ferguson Jan 10 '13 at 14:22
  • 3
    Note that you have to set the DialogResult yourself! See [this question](http://stackoverflow.com/q/487920/266535) – styfle Oct 22 '13 at 18:54
  • I really like this solution vs adding the entire Microsoft.VisualBasic.dll library just to use one simple function. Adds weight to the distribution size and is not practical. This should be the answer. – Leo Gurdian Mar 31 '17 at 21:10
  • 4
    I don't know what it's the problem adding VisualBasic DLL library. After all, it is part of the net framework, so it is already installed, or am I miss something? – magallanes Dec 16 '18 at 17:57
  • @LeoGurdian why would you need to distribute that DLL? Its part of the framework. – StayOnTarget Apr 01 '19 at 19:31
  • @LeoGurdian We're talking a 140kB DLL here, and a third of that size once packaged for distribution... Sure, these things add up, but it's a lot easier than rolling your own (unit tested, hopefully) dialog from scratch. – Extragorey Jan 22 '20 at 01:35
  • by default TextBox1 will be private, the property "Modifiers" can be changed to public in the properties window of the control – t0b4cc0 Jan 01 '21 at 22:04