![]() |
Desktop Development »
Edit Controls »
General
Intermediate
InputBox in C#By hestolThis is owner's own InputBox in C#, with only one function. So this InputBox do not inherit from a WinForm. |
C#, .NET, Win2K, WinXP, Visual Studio, WinForms, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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 FunctionDisplays 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.
SyntaxInputBox(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.
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:
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.
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();
}
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!
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Apr 2005 Editor: Sumalatha K.R. |
Copyright 2005 by hestol Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |