0

我正在制作一个表单,如果单击按钮,它将转到我的列表框并运行我在其中的功能,尽管我对如何在单击按钮时实现它以及列表框工作感到有点困惑. 这是我的代码>_>

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

public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    Ping.PlayConsole();
}
4

4 回答 4

1

您只需要:

private void button1_Click(object sender, EventArgs e)
{
    Ping.PlayConsole();
}

在不同的处理程序下调用相同的函数是可以的。

于 2012-10-26T17:43:34.567 回答
0

两个事件处理程序共享相同的签名void (object , EventArgs),因此它们是调用兼容的。

如果您使用表单设计器直观地连接事件:

转到 Property Inspector 的 Events 窗格,而不是双击它来创建事件处理程序存根,而是button1.Click单击右侧出现的下拉框图标。Visual Studio 将显示表单中存在的所有具有兼容签名的事件处理程序,您应该可以选择ListBox_SelectIndexChanged处理button1.Click程序。他们将共享同一个处理程序。

如果您通过代码连接处理程序,那么这也应该有效:

ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);
于 2012-10-26T18:07:12.067 回答
0

尝试这个 :

private void button1_Click(object sender, EventArgs e)
{
   ListBox_SelectedIndexChanged(sender,e);
}

祝你好运 !!

于 2012-10-26T17:35:47.380 回答
0
private void button1_Click(object sender, EventArgs e)
{
    ListBox.Focus();
    Ping.PlayConsole();
}
于 2012-10-26T17:47:51.943 回答