Ok, here’s one that’s not rocket science, but I spent sometime fixing up some code in my internal implementation for an InputBox like function/class that allows you to quickly present an input prompt to type in some text. Visual Basic includes an InputBox function to do this of course, but if you’re using C# there’s no real equivalent. But even the VB version is kinda limited as it doesn’t let you deal with cancel actions or handle input for common types like numeric data.
In my small WinForm framework class lib I have an InputForm class that performs this functionality. It’s a Windows Form that contains a textbox and a couple of buttons, and handles input of text, numerics and Date values (dates as strings at the moment though) using a custom TextBox control. Obviously there’s nothing tricky about this but it’s kind of a useful function that you see asked about quite frequently in newsgroups.
The class exposes a static InputBox method (actually a couple) as well as the class interface which allows a bit more control. It handles typed values by allowing to pass in an input value, identifying the type and on the way out converting the string value back into the same type. If the type conversion fails the form can’t be exited and error message is displayed. All of this adds up to making it as easy as possible to quickly getting input values of various types in your code without having to perform a variety of checks.
You can download the InputBox class from here:
http://www.west-wind.com/files/tools/winforminputbox.zip
Here are a couple of examples using the static methods of the class:
private void btnInputBox_Click(object sender, System.EventArgs e)
{
string Result = InputForm.InputBox("Text Input",
"Please enter some text:","") as String;
if (Result == null)
this.txtResult.Text = "Cancel pressed";
else
this.txtResult.Text = Result.ToString();
}
private void btnNumeric_Click(object sender, System.EventArgs e)
{
object Result = InputForm.InputBox("Numeric Input",
"Please enter a numeric value:",1.00M,"{0:N2}");
if (Result == null)
this.txtResult.Text = "Cancel pressed";
else
this.txtResult.Text = ((decimal) Result).ToString();
}
private void btnDate_Click(object sender, System.EventArgs e)
{
object Result = InputForm.InputBox("Date Input","Please enter a Date:",
DateTime.Now,"{0:d}");
if (Result == null)
this.txtResult.Text = "Cancel pressed";
else
this.txtResult.Text = ((DateTime) Result).ToString();
}
You can pass in a format string which is handy for dates and numbers although the format is applied only on the way in – it’s not exactly a masked edit control. Numeric and date fields though automatically limit input characters that are not allowed.
If you want more control you can use the class which allows much more control including the ability to control the form itself and set properties like alignment and size of the input box manually. This can make for a nicer looking control in some scenarios.
private void btnCustom_Click(object sender, System.EventArgs e)
{
InputForm Input = new InputForm();
Input.Width = 200;
Input.Text = "Custom Input Form";
Input.LabelText = "Please enter some text:";
Input.LabelWidth = 100;
Input.InitialValue = "Hello";
DialogResult dr = Input.ShowDialog();
object Result = Input.Result;
if (dr == DialogResult.Cancel ||
Result == null)
this.txtResult.Text = "Cancel pressed";
else
this.txtResult.Text = Result as String;
}
Values are passed back and forth using the InitialValue and Result properties and here a DialogResult value is also returned, so overall there’s much more control.
The class uses one support class wwTextBox, which provides some of the masking capabilities for numeric and date values – you can remove that if you’d rather have a single module that does it all in the source.
Other Posts you might also like