Custom C# InputBox Class

Disclaimer: The following source code is for educational purposes only. Please do not use for evil.

I have a small project in C# which I use for testing out various things I find when going through the .NET namespaces. It's a good way to learn new things, and on more than one occasion I've tinkered with something that came up later in a work-related project, thereby saving me the time it would take to learn it on-the-job. Anyway, I was working with this app the other day and was reminded of something when I needed to prompt the user for a string: C# does not have an InputBox function/class. So I wrote my own. This is a very simple example, but just because it's simple doesn't mean it's not useful.

First, create a new form, identical to the standard InputBox that VB has. Give it a label for the message to the user, a text box for user input, and two buttons, one labeled "OK", the other "Cancel". Set the form's AcceptButton property to the OK button, and it's CancelButton property to the Cancel button. Set the FormBorderStyle property to FixedDialog, and the MaximizeBox and MinimizeBox properties to false. Finally, set the StartPosition property to CenterScreen. Also, set the TabIndexes of the controls appropriately (the textbox should be 0, the OK button 1, and the Cancel button 2).

In the code, add a property to represent the text entered by the user. I called mine EnteredText:

public string EnteredText
      return txtEnteredText.Text; //This is the text of the text box (no kidding!)

Now comes the implementation. You could do this a number of ways -- you could use a custom method to display the form, but I chose to go with a constructor-based initialization and then used the ShowDialog() method as you would other forms. Take whichever approach suits you best.

My constructor looked something like this:

public InputBox(string Message, string Title, string DefaultEnteredText)
   // Required for Windows Form Designer support

   lblMessage.Text = Message; //Sets the text of the message label
   this.Text = Title;
   txtEnteredText.Text = DefaultEnteredText; //Default text for the text box -- leave blank if not desired

In the click event for the OK button, add the following:
this.DialogResult = DialogResult.OK;

In the click event for the Cancel button, add the following:
this.DialogResult = DialogResult.Cancel;

That's it. To use your new class, you would need to create an instance of it, and use ShowDialog() to display it. If the user clicked the OK button, you would retrieve the value of the EnteredText property to get what they entered. Like so:

InputBox ibox = new InputBox("Enter something", "InputBox Example", "Default Text");

if( ibox.ShowDialog() == DialogResult.OK )
   MessageBox.Show("User entered " + ibox.EnteredText, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);

ibox = null;

Again, there are many other ways you could create your own InputBox. This is just one of them. You could expand it to give it other capabilities as well. For the purposes of prompting the user for a string for my testing app, this worked out fine.


Anonymous said…
I had a similar difficulty and found this neat little solution.

You can add a reference to Microsoft.VisualBasic, which then allows you to call Microsoft.VisualBasic.Interaction.InputBox.

Neat eh?

Popular Posts

How To Mock Out Child Components In Unit Tests of Angular 2 Code

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

Unit Testing with a Mock Entity Framework DbContext and Fake DbSets

A Red Herring When Using Moq to Mock Methods With Optional Parameters