What? No InputBox in C#?
VB.NET has support for the InputBox function mostly for backward compatibility. Knowing Microsoft, it will probably disappear in the next Framework release. How silly is this? No InputBox in C#.
So I did this.
- Add a Windows Form to your project. Name it “InputBox”
- Add a label for the question, a textbox for the answer and a button for OK (with dialogresult=OK)
- Expose two properties — Question and Answer
- OnClose of the InputBox form, set the Answer property to the textbox value. You can also add a check to see if it’s null and if so, set it to the answer sent in with the request
Now, wherever it is you need an InputBox, do:
string author = "(none)";InputBox f = new InputBox(); f.Question = "Name of the New Author?"; f.Answer = author; // also serves as a default one if left blank! f.ShowDialog();author = f.Answer; f.Dispose();
There. Problem solved. A simple, silly C# InputBox implementation. You could get rid of the properties and fiddle with the constructor if you wish.


thanks for the information.
Too easy, but ….
I’m writting code for PocketPC. Is it the reason ? I cannot / I don’t know how to set dialogResult=OK for the OK button
That’s easy.. If you are using .Net Studio, click on the button, so it shows the properties, then Under Behavior change DialogResult to return OK
If you really want to use the VB InputBox (say if you’re ultimately lazy and want a quick one); just add a reference to Microsoft.VisualBasic, and call: Microsoft.VisualBasic.Interaction.InputBox();
The overloads don’t seem to work, so you’ll have to specify all params.