【发布时间】:2014-06-28 03:44:19
【问题描述】:
我的 win 应用中有 2 个按钮。
Button1 做一个任务:
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
String[] a = textBox7.Text.Split('#');
progressBar1.Maximum = a.Length;
for (var i = 0; i <= a.GetUpperBound(0); i++)
{
ansh.Close();
progressBar1.Value++;
}
}
按钮 2 执行以下操作
private void button2_Click(object sender, EventArgs e)
{
foreach (string item in listBox2.Items)
textBox7.Text += item.Contains("@") ? string.Format("{0}#", item.Split('@')[0]) : string.Empty;
}
我只想为两个事件使用一个按钮。
但我希望在 button1 调用的事件之前调用 button2 的事件。
意味着我只想使用一个按钮而不是按钮 1 和 2。当我单击时,我想做的第一件事就是在文本框中获取列表框项目。
{
foreach (string item in listBox2.Items)
textBox7.Text += item.Contains("@") ? string.Format("{0}#", item.Split('@')[0]) : string.Empty;
}
然后是启动进度条和关闭连接x的事件。
progressBar1.Value = 0;
String[] a = textBox7.Text.Split('#');
progressBar1.Maximum = a.Length;
for (var i = 0; i <= a.GetUpperBound(0); i++)
{
ansh.Close();
progressBar1.Value++;
}
【问题讨论】:
-
一个好的解决方案是将功能提取到方法中并调用按钮处理程序中的方法。这样你就不需要触发点击事件,而是调用一个方法。也更容易维护。
-
@pasty 非常感谢