Click here to Skip to main content
11,930,222 members (40,178 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked

InputBox in C#

, 21 Apr 2005
Rate this:
Please Sign up or sign in to vote.
This is owner's own InputBox in C#, with only one function. So this InputBox do not inherit from a WinForm.

Sample Image - InputBox.gif


Visual Basic 6.0 has an InputBox() function, Visual Basic .NET has one but in C# you don't. You can easily solve this by adding a reference to 'Microsoft.VisualBasic.dll' and using the static method 'Microsoft.VisualBasic.Interaction.InputBox()'.

See reference to MSDN 2001 in VB help.

InputBox Function

Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a string containing the contents of the text box.

InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

The InputBox function syntax has these named arguments: see the help file MSDN\2001OCT\1033.

In the VB 6.0, there were the title, default, xpos and ypos optional values to the call of InputBox function. It is the same thing I have made.

But in this example we make ours own.

InputBox Class

The InputBox class is a public class there are not inherited from System.Windows.Forms.Form class. You use it by calling the static function name 'Show' like we also do in MessageBox. This method is returning a public InputBoxResult. This object has two public properties one called Text with a string and a ReturnCode.

There is an enum. It the same as the MessageBox returns, but the InputBox only returns these two parameters, DialogResult.OK or DialogResult.Cancel.

See the class header:

InputBox Class Image


You activate the InputBox by calling the static Show() method. It has one required and four optional arguments (using overloading).

Why there are so many new lines in the prompt argument is that, the old VB 6.0 made the InputBox form bigger after the size of the prompt argument.

private void button1_Click(object sender, System.EventArgs e)
    // This test that the InputBox can handle more newline than one.
    InputBoxResult test = InputBox.Show("Prompt" + "\n" + "DDDD" + 
                  "Prompt" + "\n" + "DDDD" +
                  "Prompt" + "\n" + "DDDD" + "Prompt" + "\n" + "DDDD" +
                  "Prompt" + "\n" + "DDDD" +
                  "Prompt" + "\n" + "DDDD"

    if( test.ReturnCode == DialogResult.OK )

The best of all is that the drop down list there comes from the InputBox is very small. If we have inherited from Windows.Form then it will have been bigger.

InputBox drop down list Image

This is one of the methods in the InputBox class where we assign values to all the properties in the control. It regulates the InputBox size and the prompt textbox size based on the size of the prompt input string.

static private void LoadForm()
    OutputResponse.ReturnCode = DialogResult.Ignore;
    OutputResponse.Text = string.Empty;

    txtInput.Text = _defaultValue;
    lblPrompt.Text = _formPrompt;
    frmInputDialog.Text = _formCaption;

    // Retrieve the working rectangle from the Screen class
    // using the PrimaryScreen and the WorkingArea properties.
    System.Drawing.Rectangle workingRectangle = 

    if((_xPos >= 0 && _xPos < workingRectangle.Width) && 
         (_yPos >= 0 && _yPos < workingRectangle.Height))
        frmInputDialog.StartPosition = FormStartPosition.Manual;
        frmInputDialog.Location = new System.Drawing.Point(_xPos, _yPos);
        // InputBox in the center if not specifier or out of screen size
        frmInputDialog.StartPosition = 

    string PrompText = lblPrompt.Text;

    int n = 0;
    int Index = 0;
    // Counting the new line in the Prompt string
    while(PrompText.IndexOf("\n",Index) > -1)            
        Index = PrompText.IndexOf("\n",Index)+1;

    if( n == 0 )
        n = 1;

    // Down here making the form bigger.
    System.Drawing.Point Txt = txtInput.Location; 
    Txt.Y = Txt.Y + (n*4);
    txtInput.Location = Txt; 
    System.Drawing.Size form = frmInputDialog.Size; 
    form.Height = form.Height + (n*4);
    frmInputDialog.Size = form; 

    txtInput.SelectionStart = 0;
    txtInput.SelectionLength = txtInput.Text.Length;


The InputBox a simple static class which you can use in Windows Forms application to prompt for a text. It can also be used from Visual Basic .NET when you compile it in a library and refer this library from your VB project.

If we will make our own MessageBox, then we have to make in the same way as we made the InputBox. Then we will support other languages with our own MessageBox.

Have fun!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior) Bluegarden A/S
Denmark Denmark
I have working with C since 1988, and updates to VC++ in 1998. The first software I was making in VC++ was Fan calculation program with graph. This Fan Was integrated with the Oracle system.
I've been working almost exclusively with C# and .NET since
beginning of the technology.
Now I am working with Web API and CRM, MVC 5.0, SQL Server 2012 and .NET 2013. The company Bluegarden A/S is a IT/Finans house.

You may also be interested in...

Comments and Discussions

Generalsuper Pin
Member 1070787020-Oct-15 2:32
memberMember 1070787020-Oct-15 2:32 
QuestionLiceense Information Pin
Panky_Kumar3-Mar-14 3:40
memberPanky_Kumar3-Mar-14 3:40 
QuestionCode changes with all the recomendations added. Pin
Member 9194139-Jan-14 1:34
memberMember 9194139-Jan-14 1:34 
Questionchecking for the blank input Pin
Jayakishor.r10-Jan-13 23:42
memberJayakishor.r10-Jan-13 23:42 
QuestionPossible Enhancement. Pin
Member 372800416-Jun-12 20:17
memberMember 372800416-Jun-12 20:17 
AnswerRe: Possible Enhancement. Pin
Clifford Nelson26-Jun-12 14:54
memberClifford Nelson26-Jun-12 14:54 
QuestionPasswordChar functionality Pin
Member 791674021-Dec-11 3:38
memberMember 791674021-Dec-11 3:38 
GeneralMy vote of 5 Pin
Member 791674021-Dec-11 3:34
memberMember 791674021-Dec-11 3:34 
GeneralImprovement suggestions Pin
Mario Majcica29-Mar-11 23:55
memberMario Majcica29-Mar-11 23:55 
GeneralRe: Improvement suggestions Pin
hestol7-Apr-11 2:57
memberhestol7-Apr-11 2:57 
BugRe: Improvement suggestions Pin
Clifford Nelson26-Jun-12 14:57
memberClifford Nelson26-Jun-12 14:57 
GeneralRe: Improvement suggestions Pin
Jack Mallord26-Sep-13 17:27
memberJack Mallord26-Sep-13 17:27 
NewsInputBox in C++ Pin
Member 371636021-Jan-11 5:53
memberMember 371636021-Jan-11 5:53 
Hello, Hestol!

I've modified your code to C++ and I could be realized if you take a look on it:

Thank you!
GeneralMy vote of 5 Pin
Con Fuse16-Nov-10 3:05
memberCon Fuse16-Nov-10 3:05 
GeneralThank you Pin
Avi Farah15-Nov-10 11:25
memberAvi Farah15-Nov-10 11:25 
AnswerNice Inteligent Solution (Just flamed the other guy for that VB hack rubish). Pin
DavePaterson11-Sep-10 11:32
memberDavePaterson11-Sep-10 11:32 
AnswerRe: Nice Inteligent Solution (Just flamed the other guy for that VB hack rubish). Pin
hestol13-Sep-10 4:19
memberhestol13-Sep-10 4:19 
GeneralInputBox Pin
JoeNovak6910-Nov-09 10:52
memberJoeNovak6910-Nov-09 10:52 
QuestionRe: InputBox Pin
Jayakishor.r10-Jan-13 1:24
memberJayakishor.r10-Jan-13 1:24 
GeneralJavier Daza Pin
Javier Daza19-Sep-09 4:00
memberJavier Daza19-Sep-09 4:00 
Generalno need for this Pin
eagles81213-Sep-07 0:52
membereagles81213-Sep-07 0:52 
GeneralRe: no need for this Pin
Jordanwb31-Mar-08 14:02
memberJordanwb31-Mar-08 14:02 
GeneralRe: no need for this Pin
bhains17-Jul-08 10:43
memberbhains17-Jul-08 10:43 
GeneralRe: no need for this Pin
hestol31-Jul-08 21:37
memberhestol31-Jul-08 21:37 
GeneralRe: no need for this Pin
Riz Thon14-Sep-08 23:20
memberRiz Thon14-Sep-08 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 21 Apr 2005
Article Copyright 2005 by hestol
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid