C#如何实现等待界面上任意一个按钮按下再执行语句后面的操作呢,界面有5个按钮,要判断是那个按钮按下的
2个回答
展开全部
public Form1()
{
InitializeComponent();
Button bt1 = new Button();
bt1.Click += new EventHandler(bt1_Click);
Button bt2 = new Button();
bt2.Click += new EventHandler(bt2_Click);
Button bt3 = new Button();
bt3.Click += new EventHandler(bt3_Click);
Button bt4 = new Button();
bt4.Click += new EventHandler(bt4_Click);
Button bt5 = new Button();
bt5.Click += new EventHandler(bt5_Click);
Button bt_start = new Button();
bt_start.Click += new EventHandler(bt_start_Click);
}
/// <summary>
/// 按下了某个按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
bt2_Click(sender,e);//换5个按钮单击试试
}
/// <summary>
/// 开始等待按下某个按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void bt_start_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(a =>
{
while (flag == 0)
{
//等待按钮单击事件发生
Thread.Sleep(200);
}
Invoke((EventHandler)delegate {
switch (flag)
{
case 1:
MessageBox.Show("单击了按钮1");
break;
case 2:
MessageBox.Show("单击了按钮2");
break;
case 3:
MessageBox.Show("单击了按钮3");
break;
case 4:
MessageBox.Show("单击了按钮4");
break;
case 5:
MessageBox.Show("单击了按钮5");
break;
}
});
flag = 0;
}, null);
}
int flag = 0;
void bt5_Click(object sender, EventArgs e)
{
flag = 5;
}
void bt4_Click(object sender, EventArgs e)
{
flag = 4;
}
void bt3_Click(object sender, EventArgs e)
{
flag = 3;
}
void bt2_Click(object sender, EventArgs e)
{
flag = 2;
}
void bt1_Click(object sender, EventArgs e)
{
flag = 1;
}
追问
这个程序可以运行么?怎么不能运行,我是要实现这个程序串口发送一个数,等待屏幕点击一下,接着再发下一个数,等待点击,直到5个数都发送一遍
追答
可以运行啊 只是这五个按钮没显示到界面而已 可以做到的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询