Click here to Skip to main content
Click here to Skip to main content

InputBox in C#

By , 21 Apr 2005
Rate this:
Please Sign up or sign in to vote.

Sample Image - InputBox.gif

Introduction

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.

Syntax
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

Usage

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"
                  ,"Title","Default",100,0); 

    if( test.ReturnCode == DialogResult.OK )
        MessageBox.Show(test.Text);
} 

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 = 
         Screen.PrimaryScreen.WorkingArea;

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

    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;
        n++;
    }

    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;
    txtInput.Focus();
}

Conclusion

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!

License

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

hestol
Software Developer (Senior) Cyber Com Consulting 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 SOA structure, MVC 3.0, SQL Server 2008 and VS.NET 2010. The company Cyber Com Consulting A/S is a IT consult house.

Comments and Discussions

 
QuestionLiceense Information PinmemberPanky_Kumar3-Mar-14 2:40 
QuestionCode changes with all the recomendations added. PinmemberMember 9194139-Jan-14 0:34 
Questionchecking for the blank input PinmemberJayakishor.r10-Jan-13 22:42 
QuestionPossible Enhancement. PinmemberMember 372800416-Jun-12 19:17 
AnswerRe: Possible Enhancement. PinmemberClifford Nelson26-Jun-12 13:54 
QuestionPasswordChar functionality PinmemberMember 791674021-Dec-11 2:38 
GeneralMy vote of 5 PinmemberMember 791674021-Dec-11 2:34 
GeneralImprovement suggestions PinmemberMario Majcica29-Mar-11 22:55 
GeneralRe: Improvement suggestions Pinmemberhestol7-Apr-11 1:57 
BugRe: Improvement suggestions PinmemberClifford Nelson26-Jun-12 13:57 
GeneralRe: Improvement suggestions PinmemberJack Mallord26-Sep-13 16:27 
NewsInputBox in C++ PinmemberMember 371636021-Jan-11 4:53 
GeneralMy vote of 5 PinmemberCon Fuse16-Nov-10 2:05 
GeneralThank you PinmemberAvi Farah15-Nov-10 10:25 
AnswerNice Inteligent Solution (Just flamed the other guy for that VB hack rubish). PinmemberDavePaterson11-Sep-10 10:32 
AnswerRe: Nice Inteligent Solution (Just flamed the other guy for that VB hack rubish). Pinmemberhestol13-Sep-10 3:19 
GeneralInputBox PinmemberJoeNovak6910-Nov-09 9:52 
QuestionRe: InputBox PinmemberJayakishor.r10-Jan-13 0:24 
GeneralJavier Daza PinmemberJavier Daza19-Sep-09 3:00 
Generalno need for this Pinmembereagles81212-Sep-07 23:52 
it is possible to use VB inputbox by using namespaces etc...
u dont have to do this unless u want to add something more.
try calling vb codes, there are some several helpfull things,
like inputbox.
GeneralRe: no need for this PinmemberJordanwb31-Mar-08 13:02 
GeneralRe: no need for this Pinmemberbhains17-Jul-08 9:43 
GeneralRe: no need for this Pinmemberhestol31-Jul-08 20:37 
GeneralRe: no need for this PinmemberRiz Thon14-Sep-08 22:20 
GeneralRe: Good One Pinmembermanas25-Sep-08 4:29 
AnswerRe: no need for this PinmemberClifford Nelson26-Jun-12 14:02 
GeneralIt was useful to me PinmemberPaul Reynolds15-Oct-09 9:05 
AnswerRe: It was useful to me Pinmemberhestol15-Oct-09 21:43 
GeneralRe: It was useful to me PinmemberClifford Nelson26-Jun-12 14:03 
GeneralRe: no need for this PinmemberMidax23-Apr-10 6:30 
GeneralProblems with this code PinmemberDavid Piepgrass3-May-05 14:30 
GeneralRe: Problems with this code PinmemberDavid Piepgrass3-May-05 14:36 
GeneralRe: Problems with this code Pinmemberhestol3-May-05 21:20 
GeneralRe: Problems with this code PinmemberDavid Piepgrass4-May-05 14:16 
GeneralRe: Problems with this code [modified] PinmemberBajotumn.com15-Sep-09 16:39 
AnswerRe: Problems with this code Pinmemberhestol15-Sep-09 21:38 
AnswerRe: Problems with this code PinmemberClifford Nelson26-Jun-12 14:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140415.2 | Last Updated 21 Apr 2005
Article Copyright 2005 by hestol
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid