Many programming languages, such as Visual Basic 6, include pre-made functions to gather user input via means of an input box. However, the .Net Frameworks, and more specifically CSharp .Net, do not include such an option. This tutorial will walk you through the steps of creating and using your very own input box class.
Step One - Create a Input Box Class
The first step will be to start up Visual Studios and load your CSharp project. Once your project has been loaded, click the 'Project' menu and select the 'Add Class...' button. You will be prompted to enter a name for the new class. Call it, 'InputBox.cs'. By default the class will be private(not accessible by other classes or forms), to make the class public and the prefix 'public' before the class declaration. Like so,
public class InputBox
Now that the input box class is available to the public, create a private DialogResult function inside the class called, 'GetResponse()' using the source code supplied below.
private DialogResult GetResponse(string Question, string Title, ref string Response)
{
// The Input Box Form
Form inputBox = new Form();
inputBox.StartPosition = FormStartPosition.CenterScreen;
inputBox.Size = new System.Drawing.Size(394, 114);
inputBox.Text = Title;
inputBox.TopMost = true;
inputBox.FormBorderStyle = FormBorderStyle.FixedToolWindow;
// Question Label
Label lblQuestion = new Label();
lblQuestion.Text = Question;
lblQuestion.Location = new System.Drawing.Point(12, 9);
// Input Box
TextBox inputText = new TextBox();
inputText.Size = new System.Drawing.Size(361, 20);
inputText.Location = new System.Drawing.Point(15, 48);
// OK Button
Button cmdOK = new Button();
cmdOK.Text = "OK";
cmdOK.Location = new System.Drawing.Point(335, 71);
cmdOK.Size = new System.Drawing.Size(42, 19);
cmdOK.DialogResult = DialogResult.OK;
// Cancel Button
Button cmdCancel = new Button();
cmdCancel.Text = "Cancel";
cmdCancel.Location = new System.Drawing.Point(271, 71);
cmdCancel.Size = new System.Drawing.Size(58, 19);
cmdCancel.DialogResult = DialogResult.Cancel;
// Add Controls to the Form
inputBox.Controls.Add(lblQuestion);
inputBox.Controls.Add(inputText);
inputBox.Controls.Add(cmdOK);
inputBox.Controls.Add(cmdCancel);
// Set the Forms Accept Button
inputBox.AcceptButton = cmdOK;
inputBox.CancelButton = cmdCancel;
// Show the Form
DialogResult Result = inputBox.ShowDialog();
// Return the Results
Response = inputText.Text;
return Result;
}
And, create a public string function called, 'ShowInputBox()' using this code,
public string ShowInputBox(string Question, string Title)
{
string Result = "";
if (new InputBox().GetResponse(Question, Title, ref Result) == DialogResult.OK)
{
return Result;
}
return Result;
}
Conclusion
To use the input box class simple call the ShowInputBox() method and supply the proper parameters. Here is an example usage,
this.Text = new InputBox().ShowInputBox("What would you like the form's title to be?", "Set Form Title");
This code will set a form's title text to whatever the user types.
Published by James Cloud
I like to program and do basically anything that has to do with technology and computers. View profile
- CSharp .Net Tutorial: Move a Borderless FormLearn how to give the user the ability to move a borderless form in your CSharp .Net Applications.
- Csharp.Net Tutorial: Playing Audio with DirectXLearn how to play sounds and music using DirectX in your Csharp.net projects.
- CSharp .Net Tutorial: PingingLearn how to ping an IP Address using CSharp .Net in this easy to understand tutorial.
- CSharp .Net Tutorial: Directory ManipulationIn this tutorial, learn how to rename, delete, and create new folders using CSharp .Net source code.
- CSharp .Net Tutorial: Loading Textures in Truevision 3DA concise tutorial on how to load textures into your Truevision 3D CSharp .Net projects.
- CSharp .Net Tutorial: Draw Strings on a From
- CSharp .Net Tutorial: Sending Email
- CSharp .Net Tutorial: Reading and Writing INI
- CSharp .Net Tutorial: Picture Box Load Icon
- CSharp .Net Tutorial: Read / Write Registry
- CSharp .Net Tutorial: How to Skin Your Forms
- Csharp .Net Tutorial: Catching Errors



