83

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?

2

2 Answers 2

110

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...

5
  • 2
    I am using .net 2 and C# 2.0. It cannot find .dll. Any ideas?
    – Sunscreen
    May 29, 2012 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... Sep 13, 2012 at 18:14
  • 1
    Doesn't work on .net 4.5
    – ave
    Dec 6, 2014 at 19:30
  • 3
    @ardaozkal it does, are you sure you have referenced Microsoft.VisualBasic? Dec 8, 2014 at 9: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 8, 2014 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();
}
7
  • 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. Jan 10, 2013 at 14:22
  • 3
    Note that you have to set the DialogResult yourself! See this question
    – styfle
    Oct 22, 2013 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 G.
    Mar 31, 2017 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, 2018 at 17:57
  • @LeoGurdian why would you need to distribute that DLL? Its part of the framework. Apr 1, 2019 at 19:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.