C#Forms如何从提示对话框中获取值

时间:2015-12-03 23:26:49

标签: c# winforms class dialog prompt

我有一个名为“AddChips”的课程,它基本上是带有一些按钮的提示对话框,而TextBox用户输入了一些数据,我想获取他刚刚输入的数据并将其发回给其他人class让我们说“Form1”并将数据提供给称为Chips的整数

public static string ShowDialog(string text, string caption)
{
    Form prompt = new Form();
    prompt.Width = 500;
    prompt.Height = 150;
    prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
    prompt.Text = caption;
    prompt.StartPosition = FormStartPosition.CenterScreen;
    Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
    TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
    Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
    Button leaveApp = new Button() { Text = "No", Left = 250, Width = 100, Top = 70, DialogResult = DialogResult.OK };
    confirmation.Click += (sender, e) => { new Form1() { Chips = int.Parse(textBox.Text) }; };
    leaveApp.Click += (sender, e) => { Application.Exit(); };
    prompt.Controls.Add(textBox);
    prompt.Controls.Add(confirmation);
    prompt.Controls.Add(leaveApp);
    prompt.Controls.Add(textLabel);
    textLabel.Width = 300;
    prompt.AcceptButton = confirmation;
    return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}

1 个答案:

答案 0 :(得分:1)

我在提示表单上使用过像这样:

public string ValueIWant { get; set; }

private void btnSetValueIWant_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtbxValue.Text))
    {
        ValueIWant= txtbxValue.Text;
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
        Close();
    }
}

在另一种形式上,我得到了价值:

frmValue f= new frmValue ();
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               string ValueIWantFromProptForm=f.ValueIWant ;
            }

它对我有用。