Winform 执行application.doevent()之后的问题

yxtyxt3311 2009-12-07 02:34:15
窗体中放两个按钮

protected bool isexit = false;

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("ddd");
isexit = true;
}

private void button1_Click(object sender, EventArgs e)
{
while (!isexit)
{
Application.DoEvents();
}
}


运行窗体,先点击button1
button2要等第二次点击才获得焦点,触发click事件,如何解决?
...全文
643 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsnakebtf 2010-08-19
  • 打赏
  • 举报
回复
看看咋解决
INTTNY 2009-12-08
  • 打赏
  • 举报
回复
非要这么写吗?可不可以改下?

//string str = string.Empty;
private void buttonArray_Click(object sender, EventArgs e)
{
if(sender is Button)
{
str += ((Button)sender).Text;
}
}

然后将所有的按钮的Click事件都指向这个

再或者就新建个线程,这种问题最合适用监听线程来做了
yxtyxt3311 2009-12-08
  • 打赏
  • 举报
回复
不管怎么样,我在执行doevents之前加了this.focus或者button.focus都不行

我自己做的另一个实例,与目前做的类似就行,不知道为什么...
现在这个,就是非要点击两次...
yxtyxt3311 2009-12-08
  • 打赏
  • 举报
回复
我主要实现的是
obj=null;
for(int i=0;i <3;i++)
{
while (true)
{
Application.DoEvents();
if(obj!=null)
{
break;
}
}
str+=obj.value;
obj=null;
}

点击界面随便哪个按钮,都会给obj赋值
主要是获取界面上,任意几个按钮的值
yxtyxt3311 2009-12-08
  • 打赏
  • 举报
回复
我主要实现的是
obj=null;
for(int i=0;i<3;i++)
{
while (!isexit)
{
Application.DoEvents();
if(obj!=null)
{
break;
}
}
str+=obj.value;
obj=null;
}

点击界面随便哪个按钮,都会给obj赋值
主要是获取界面上,任意几个按钮的值
memoCKH 2009-12-08
  • 打赏
  • 举报
回复
doevent(),就是让程序一直在执行。。。所以就这样了。
INTTNY 2009-12-08
  • 打赏
  • 举报
回复
你这个结构不大对啊,你为何要在按钮的click事件中写一个循环?如果是一个单线程的程序那么这个主线程一直不能结束又如何响应你的按钮事件?加了Application.DoEvents也不管用的吧?不管你这个按钮事件里边的while要做什么你都应该交由一个线程单独去做,然后窗体就可以正常响应你的其他按钮的点击事件了
Crystalzhou 2009-12-08
  • 打赏
  • 举报
回复
make
yxtyxt3311 2009-12-08
  • 打赏
  • 举报
回复
字写反了...Focus()
yxtyxt3311 2009-12-08
  • 打赏
  • 举报
回复
我在执行循环前加了this.foucs();


this.foucs();
while (!isexit)
{
Application.DoEvents();
}


就是不行,始终窗体要先随便哪个地方点一下,这个form上的控件,才可以点击.
不知道为什么?
shuifengcun 2009-12-08
  • 打赏
  • 举报
回复
楼上正解
wartim 2009-12-08
  • 打赏
  • 举报
回复
可以看看这篇文章,消息循环的问题

Application.Run and Application.DoEvents
http://blog.hi.mop.com/GetEntry.do?id=4317921&owner=8856309

它给了一个例子,也是要按两次才有对话框

bool inButtonClick = false;
private void button1_Click(object sender, System.EventArgs e) {
if (inButtonClick) {
MessageBox.Show("Being called when I'm already in here!!!");
}
inButtonClick = true;
for (int i = 0; i < 10000000; i++) {
listBox1.Items.Add(i.ToString());
Application.DoEvents(); // <-- this call here allows the second click to process
}
inButtonClick = false;
}


给出了执行过程

Application.Run
Process mouse up on button
Fire ButtonClick event
Add some items to listbox
Call Application.DoEvents to make it look more responsive
Process mouse up on close box
Dispose the form
Add remaining items to listbox
Update the first control on the form -- BOOM! Controls collection has been cleared!

This is just one example of codepath gone bad, there’s plenty of others – say the user clicked the button a second time – the button click event would be called again while you’re still processing the first!



cppfaq 2009-12-08
  • 打赏
  • 举报
回复
好像窗体失去了焦点,把form的代码都贴上来看看
yxtyxt3311 2009-12-08
  • 打赏
  • 举报
回复
不是这样,界面没有焦点,界面非要点击一下,才可以点button...不然点button是没有任何效果的...
wuyq11 2009-12-08
  • 打赏
  • 举报
回复
所有按钮调用统一事件
Button btn =sender as Button;
实现取值
yuanhuiqiao 2009-12-08
  • 打赏
  • 举报
回复
TabIndex?
yxtyxt3311 2009-12-07
  • 打赏
  • 举报
回复
这个问题我可能没问好,如果我不只button2,还有button3,button4....

我要单击其它button就不行了。。。

如何能可以随便单击如个按钮都行.....
APP开发王 2009-12-07
  • 打赏
  • 举报
回复
    protected bool isexit = false;

private void button2_Click(object sender, EventArgs e)
{
Application.DoEvents();
MessageBox.Show("ddd");
isexit = true;
}

private void button1_Click(object sender, EventArgs e)
{

button2.Focus();
while (!isexit)
{
Application.DoEvents();
}



}

简单。测试OK
yxtyxt3311 2009-12-07
  • 打赏
  • 举报
回复
意思就是:

执行Application.DoEvents();

该窗体上其它按钮,击要点两次才会触发事件
ztenv 2009-12-07
  • 打赏
  • 举报
回复
没看懂什么意思。。。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧