One of the things that always irked me about C# is its lack of an InputBox function. In VB, You can simply do
response = InputBox("Enter your name")
Unfortunately, there is no C# equivalent. Sure, you can call the VB one, but doesn't that kind of defeat the purpose of C#? So, like any good programmer, I set out to make my own pure C# InputBox. Its fairly bare bones, but it gives all the functionality of the VB InputBox.
You can use it by including the InputBox class in your program's namespace and calling it like this:
string lang = InputBox.Show("What's your favorite language?", "Language", "C#", -1, -1);
- Because C# 3.5 and below doesn't support optional parameters, you must supply an argument for every parameter.
- For the xPos and yPos, passing in -1 will center the form in the corresponding direction. (x is horizontal, y is vertical)
- There is almost nothing in the way of error trapping, because I feel that you, the end programmer, can best trap the errors as suited to your project. Simply surrounding InputBox.Show() with a try statement should prove adequate.
- If the user presses cancel, InputBox.Show() returns an empty string ("")
- If you modify/distribute the source code, please retain my information comment, however you may add yourself as an author.
- PM me with any features you think this should have or any bugs you find.