8 May 2004
What? No InputBox in C#?
Posted by Mikhail Esteves under: C#; Tips .
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.
4 Comments so far...
Gerald Says:
16 May 2004 at 5:51 pm.
thanks for the information.
jc Says:
30 June 2004 at 2:06 am.
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
Dave Says:
11 November 2005 at 2:34 am.
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
Ali Says:
29 June 2007 at 3:07 pm.
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.