从提示对话框返回值



我有一个应用程序,它读取一堆文本并查找某些值,当这些值被拾取时,系统会提示用户输入新值。

我遇到的问题是我似乎无法在其方法之外调用此值。

下面是调用提示的代码:

return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";

当用户添加值时,将按如下方式返回:

System.Windows.Forms.Control.Text.get returned  "1234"  string

我现在想做的是在另一个方法中调用这个值,以便我可以做字符串。取代。

任何帮助,不胜感激。

**编辑**

完整方法如下;

public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
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 = "Add", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";

您必须在提示对话框中创建一个带有公共 getter 的属性。 可以在调用 ShowDialog 后从该属性读取值。 事实上,只要窗体未释放,您就可以访问该属性。

using( var frm = new PromptForm())
{
if( frm.ShowDialog() == DialogResult.OK )
{
var s = frm.SomeProperty;
}
}

最新更新