Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

An InputBox Class for .NET


:P
On this page:

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.


The Voices of Reason


 

Pratik
December 30, 2004

# re: An InputBox Class for .NET

It is not working in asp.net. I want to to do this in asp.net

Vinodh
January 20, 2005

# re: An InputBox Class for .NET

Do you have any idea how we can implement a functionality similar to this in ASP.NET?

Thanks!
Vinodh M
email : shervin143@yahoo.com

Rick Strahl
January 21, 2005

# re: An InputBox Class for .NET

Geez, folks this is a CLIENT SIDE class, a WinForms solution. Input box for ASP.NET needs to be a script implementation using JavaScript.

Dan
January 26, 2005

# re: An InputBox Class for .NET

Thanks for that rick, very helpful.

I have something similar for ASP.NET, it returns a value from a calendar, but with a bit of tweaking could be made an input box...

http://www.danhedges.co.uk//2004_06_01_danhedgesdev_archive.html

Dave
August 09, 2005

# re: An InputBox Class for .NET

Enumerating the input types would be a much more effective way than string handling.

Rick Strahl
August 10, 2005

# re: An InputBox Class for .NET

Huh? Input types are provdided as enums. The class automatically determines the input type based on what type of value you pass in.

VenAdder
August 14, 2005

# re: An InputBox Class for .NET

how do i make the promp Multiline if I want to?

Rick Strahl
August 14, 2005

# re: An InputBox Class for .NET

Multi-line is not supported, but you can add that functionality by exposing the text box or adding a TextBox Height property which if set makes the textbox multi-line.

David Bicknell
October 04, 2005

# re: An InputBox Class for .NET

Sure glad there are people like you willing to share these jewels. I did not want to add a form to my project and needed an input box like in VB. Presto!

Shane Powser
January 31, 2006

# re: An InputBox Class for .NET

Very nice class. Still can't believe MS didn't include this one in C#.

Tarun Dudhatra
February 21, 2007

# re: An InputBox Class for .NET

It's a very nice class and it is very useable for .net development

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2025