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

C#窗体如何从提示对话框中获取值,c#,winforms,class,dialog,prompt,C#,Winforms,Class,Dialog,Prompt,我有一个名为“AddChips”的类,它基本上是一个带有一些按钮的提示对话框和一个文本框用户在其中输入一些数据,我想得到他刚刚输入的数据,并将其发送回其他类,比如说“Form1”,然后将数据发送给名为Chips的整数 public static string ShowDialog(string text, string caption) { Form prompt = new Form(); prompt.Width = 500; prompt.Height = 150;

我有一个名为“AddChips”的类,它基本上是一个带有一些按钮的提示对话框和一个
文本框
用户在其中输入一些数据,我想得到他刚刚输入的数据,并将其发送回其他类,比如说“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 ;
            }

它适合我。

如果创建新的Form1,则无法将其发送回Form1。您最好使用designer来创建对话框。我不知道该怎么做。可以用文本框和两个按钮发布一些示例代码作为答案吗?我不知道怎么做这很简单@denis,但您对评论的唯一反应是
我不知道怎么做
您知道怎么做
谷歌搜索
然后搜索
DialogResult.Ok
您需要的是一个if语句
if(prompt.ShowDialog()==DialogResult.Ok){}
然后捕获以另一种形式填充的值。您需要创建另一个表单的实例,并在调用表单中保留所需的局部变量。。然后,当表单的创建实例返回时,只要没有销毁对象,您仍然可以获取该对象。。像@MethodMan刚才解释的那样,在Google上搜索一下,你还需要在这个新表单上创建一个公共属性,当你点击OK时它会被更新。然后,使用表单的实例来阅读它。这真的很容易做到。