Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个Windows窗体中打开Windows窗体,然后单击“检测”按钮_C#_Forms_Winforms_Windows Forms Designer - Fatal编程技术网

C# 从另一个Windows窗体中打开Windows窗体,然后单击“检测”按钮

C# 从另一个Windows窗体中打开Windows窗体,然后单击“检测”按钮,c#,forms,winforms,windows-forms-designer,C#,Forms,Winforms,Windows Forms Designer,我目前正在学习如何使用Windows窗体。我有一个主窗口窗体(MainForm),可以控制一切。在执行过程中的某个时刻,我想打开另一个Windows窗体,其中包含一个Listbox(ListboxForm)和一个按钮(DoneButton),指示用户何时完成ListboxForm。我已经设法让ListboxForm显示出来,但我不确定如何让MainForm等待并在用户按下DoneButton后继续执行 现在,当ListboxForm弹出并且用户单击DoneButton时,ListboxForm仍

我目前正在学习如何使用Windows窗体。我有一个主窗口窗体(MainForm),可以控制一切。在执行过程中的某个时刻,我想打开另一个Windows窗体,其中包含一个Listbox(ListboxForm)和一个按钮(DoneButton),指示用户何时完成ListboxForm。我已经设法让ListboxForm显示出来,但我不确定如何让MainForm等待并在用户按下DoneButton后继续执行

现在,当ListboxForm弹出并且用户单击DoneButton时,ListboxForm仍然显示,并且没有发生任何事情。我不确定为什么会发生这种情况-我在DoneButton的按钮代码中有一个this.close

以下是我的ListboxForm代码:

public partial class ListboxForm : Form
{
    List<string> _items = new List<string>();

    public UpdateGhubAndWebConfigForm()
    {
        InitializeComponent();

        _items.Add("Option 1");
        _items.Add("Option 2");
        _items.Add("Option 3");

        listBox1.DataSource = _items;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { }

    // This is for 'DoneButton'
    private void button1_Click(object sender, EventArgs e)
    {
        LoggingProvider.Log.Info("ListboxForm DoneButton clicked with index: " + listBox1.SelectedIndex.ToString());

        DoStuff();

        this.Close();
    }

    private void Form1_Load(object sender, EventArgs e) { }

    private void label1_Click(object sender, EventArgs e) { }

    public static void DoStuff() { }
}

如果您想要收听表单的任何事件,您需要订阅该委托,基本上,您的MainForm会创建一个ListboxForm实例,该实例具有您想要单击的按钮。将按钮的可见性设置为公共

public Button button1....
然后从您的方法订阅事件

if(displayListboxForm == true)
{
    var listboxForm = new ListboxForm();
    //subscribing to the click event
    listboxForm.button1.Click += YourMethod;
    listboxForm.ShowDialog();

    // Now I want MainForm to wait until the user clicks DoneButton in ListboxForm
    MessageBox.Show("User has selected an option from ListboxForm");
}

public void YourMethod(EventArgs e)
{
   //your logic here when the button is clicked
}

希望这有帮助

ListBoxForm是如何从MainForm触发的?你能发布整个方法吗?我没有看到任何异常情况会导致ListBoxForm在单击“完成”按钮后保持显示。它包含在任务中。文章底部显示了触发ListboxForm的块。DoneButton的按钮单击事件是否实际触发?日志条目是否已创建?请在ListBoxForm的设计器中验证DoneButton的click事件是否由button1_click方法实际处理。是的,在表单上复制会导致异常。一旦您正确设置它们并触发事件,如果您仍然需要帮助使其成为主窗体的模式,请告诉我谢谢您的答复。然而,我有点困惑。你的“公共按钮1”到底是如何工作的?如何链接到ListboxForm中显示的实际按钮。ListboxForm中的按钮名为“DoneButton”(我可以通过Intellisense找到它)。有没有办法将“public Button button1”连接到“DoneButton”?@Roka545这就是DoneButton,我从你输入的代码中取了这个名称,但它应该作为一个局部类生成,在这个类中,你在设计器中放置的所有变量都会生成,请更改它对public的访问权限
if(displayListboxForm == true)
{
    var listboxForm = new ListboxForm();
    //subscribing to the click event
    listboxForm.button1.Click += YourMethod;
    listboxForm.ShowDialog();

    // Now I want MainForm to wait until the user clicks DoneButton in ListboxForm
    MessageBox.Show("User has selected an option from ListboxForm");
}

public void YourMethod(EventArgs e)
{
   //your logic here when the button is clicked
}