【问题标题】:C# Forms How to get value from Prompt Dialog [closed]C# Forms如何从提示对话框中获取价值[关闭]
【发布时间】:2015-12-03 23:26:49
【问题描述】:

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

【问题讨论】:

  • 如果你创建一个新的Form1,你不能把它发回Form1。使用设计器进行对话可能会更好。
  • 我不知道该怎么做可以发布一些示例代码作为带有文本框和 2 个按钮的答案?
  • 我不知道该怎么做
  • 这很简单@denis 但你对 cme​​ts 的唯一回应是I don't know how to do that 你知道如何做一个Google Search 并搜索DialogResult.Ok 你想要的是一个if 语句if (prompt.ShowDialog() == DialogResult.OK){ }然后您捕获以其他形式填充的值。您需要创建另一个表单的实例,并在调用表单中保留您想要的局部变量。然后当创建的表单实例返回时,只要您不破坏它,您仍然可以获取对象。 . 做一些Googling
  • 正如@MethodMan 刚刚解释的那样,您还需要在这个新表单上创建一个属性,该属性是公共的,并且在您单击“确定”时会更新。之后,只需使用您的表单实例阅读它。这真的很容易实现。

标签: c# winforms class dialog prompt


【解决方案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 ;
            }

它对我有用。

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 2013-09-27
    • 1970-01-01
    • 2022-01-18
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多