如何制作等待按钮cilck的方法?

时间:2016-01-02 19:49:23

标签: c# winforms

如何使方法等待按钮被单击创建的表单(不用停止应用程序本身)并从中获取值?与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

1 个答案:

答案 0 :(得分:2)

我认为您正在寻找oForm.ShowDialog()而不是.Show()

ShowDialog将以模态方式显示对话框,与MessageBox.Show()相同。

如果您想要按钮关闭表单,可以使用DialogResult属性:

oButton.DialogResult = DialogResult.OK;

或者,如果您确实想使用&#39; .Show()`,您可以使用事件处理程序来处理按钮单击并关闭表单:

oButton.Click += (s, e) => { oForm.Close(); };
相关问题