65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 中的 InputBox

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.51/5 (47投票s)

2005年4月21日

2分钟阅读

viewsIcon

562463

downloadIcon

10028

这是 C# 中自己编写的 InputBox,只有一个函数。因此,此 InputBox 不继承自 WinForm。

Sample Image - InputBox.gif

引言

Visual Basic 6.0 有一个 InputBox() 函数,Visual Basic .NET 也有一个,但在 C# 中却没有。你可以很容易地通过添加对 'Microsoft.VisualBasic.dll' 的引用,并使用静态方法 'Microsoft.VisualBasic.Interaction.InputBox()' 来解决这个问题。

请参阅 VB 帮助中的 MSDN 2001 参考。

InputBox 函数

在对话框中显示提示,等待用户输入文本或单击按钮,并返回包含文本框内容的字符串。

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

InputBox 函数语法具有这些命名参数:请参阅帮助文件 MSDN\2001OCT\1033。

在 VB 6.0 中,调用 InputBox 函数时,有标题、默认值、xpos 和 ypos 可选值。我所做的也是同样的事情。

但在本例中,我们自己创建。

InputBox 类

InputBox 类是一个公共类,没有从 System.Windows.Forms.Form 继承。你通过调用静态函数名 'Show' 来使用它,就像我们在 MessageBox 中所做的那样。此方法返回一个公共 InputBoxResult。该对象有两个公共属性,一个名为 Text,类型为字符串,另一个名为 ReturnCode

有一个 enum。它与 MessageBox 返回的值相同,但 InputBox 仅返回这两个参数,DialogResult.OKDialogResult.Cancel

请参阅类头。

InputBox Class Image

用法

你通过调用静态 Show() 方法来激活 InputBox。它有一个必需的参数和四个可选参数(使用重载)。

提示参数中存在这么多新行,是因为旧的 VB 6.0 会根据提示参数的大小调整 InputBox 表单的大小。

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);
} 

最棒的是,从 InputBox 来的下拉列表非常小。如果从 Windows.Form 继承,它会更大。

InputBox drop down list Image

这是 InputBox 类中的一个方法,我们在此方法中将值赋给控件中的所有属性。它根据提示输入字符串的大小来调整 InputBox 大小和提示文本框大小。

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

结论

InputBox 是一个简单的静态类,你可以在 Windows Forms 应用程序中使用它来提示文本。它也可以从 Visual Basic .NET 使用,只需将其编译为库并从你的 VB 项目引用该库即可。

如果我们自己制作 MessageBox,那么我们必须以与制作 InputBox 相同的方式进行。然后我们将使用我们自己的 MessageBox 支持其他语言。

玩得开心!

© . All rights reserved.