如何制作等待按钮点击的方法
本文关键字:方法 按钮 何制作 等待 | 更新日期: 2023-09-27 18:28:54
如何让方法等待在创建的表单上单击按钮(而不停止应用程序本身)并从中获取值?类似于MessageBox.Show()
的工作方式。
Form oForm = new Form();
List<TextBox> ListOfTB = new List<TextBox>();
oForm.Height = 20;
foreach (FontPair vFont in ListOfFonts)
{
Label oLabel = new Label();
oLabel.Text = vFont.Western;
oLabel.Top = oForm.Height - 10;
oLabel.Left = 10;
TextBox oText = new TextBox();
oText.Top = oForm.Height - 20;
oText.Left = 10;
oForm.Controls.Add(oLabel);
oForm.Controls.Add(oText);
ListOfTB.Add(oText);
oForm.Height += 20;
}
Button oButton = new Button();
oButton.Top = oForm.Height - 20;
oButton.Left = 10;
oForm.Height += 10;
oForm.Show();
//Here I wish to wait for oButton to be clicked
我想您要找的是oForm.ShowDialog()
而不是.Show()
。
ShowDialog
将以模式显示对话框,与MessageBox.Show()
相同。
如果您想通过按钮关闭表单,可以使用DialogResult
属性:
oButton.DialogResult = DialogResult.OK;
或者,如果您确实想使用".Show()",您可以使用事件处理程序来处理按钮单击并关闭表单:
oButton.Click += (s, e) => { oForm.Close(); };