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

C# Forms How to get value from Prompt Dialog

我有一个名为 "AddChips" 的 class 它基本上是带有一些按钮的提示对话框和一个 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 : "";
}

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

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

对我有用。