扔掉按钮的点击声 [英] throwing button clicks away

查看:92
本文介绍了扔掉按钮的点击声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将首先告诉您我是C#的新手.我在ANSI C方面有很多经验.

我有一个表单,当单击一个按钮时,将启动测试,并且我希望放弃其他按钮的单击.令人遗憾的是,测试开始了,并且在等待用户按下EXIT的同时.测试完成后,表单立即退出,他们看不到结果.

我试过了

I will start with telling you that I am new to C#. I have a lot of experience with ANSI C.

I have a form that when a button is clicked a test is started and I want to throw away clicks on other buttons. What is happeining is the test is started and while waiting the user presses EXIT. Immediatly when the test is done the form exits and they can not see the results.

I have tried

this.button2.Enabled = false;


并且该按钮显示为灰色,但仍然可用.

我试过了


and the button is grayed out, but still usable.

I have tried

if (ignoreButton2)
{
    return;
}


在该方法中并且该按钮仍处于使用状态.

有没有简单的方法可以丢弃按钮?

这是一些简单的示例代码.


in the method and the button is still used.

Is there a simple way to throw away a button?

Here is some simple example code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ButtonTestForm
{
    public partial class Form1 : Form
    {
        int count1 = 0;
        int count2 = 0;

        bool ignoreButton1 = false;
        bool ignoreButton2 = false;

        public Form1()
        {
            InitializeComponent();
            updateLabels();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ignoreButton1)
            {
                return;
            }
            ignoreButton2 = true;

            this.button2.Enabled = false;
            count1++;
            updateLabels();
            System.Threading.Thread.Sleep(5000);
            this.button2.Enabled = true;

            ignoreButton2 = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ignoreButton2)
            {
                return;
            }
            ignoreButton1 = true;

            count2++;
            updateLabels();
            System.Threading.Thread.Sleep(2000);

            ignoreButton1 = false;
        }

        private void updateLabels()
        {
            label1.Text = "count 1 is " + count1.ToString();
            label2.Text = "count 2 is " + count2.ToString();
        }
    }
}



我可以单击按钮1,然后在睡眠状态下单击按钮2.按钮2在按钮1完成后执行.



I can click on button 1 and while sleeping click on button 2. Button 2 is executed after button 1 is finished.

推荐答案

为什么在不将其设置为Visible = false的情况下该过程的开始,然后在最后显示Visible = true?如果您想保持位置,则可以用类似大小的空白图形替换它.只是一个简单的想法.
Why not set it to Visible = false at the start of the process and then Visible = true at the end? If you wanted to maintain positions then you could replace it with a similarly sized empty graphic of some kind. Just a quick thought.


最简单的方法是在进入事件处理程序时设置一个标志(布尔),并在处理其他事件中的任何代码之前先检查该标志处理程序.我认为这不是一个好的解决方案.更好的方法是禁用按钮,然后在代码完成后重新启用.
The easyist would be to have a flag (bool) that you set when you enter the event handler, and just check this flag before processing any code in the other event handlers. This is not what I would consider a good solution. A better one would be to disable the buttons and then re-enable after the code finishes.


根据您发布的代码,很容易看到为什么看到您所看到的行为.在button1的click事件之后,正在处理button2的click事件.也就是说,当button1的click事件完成且button2可以自由处理该点击时,启用button2.如果未在button1 click事件中重新启用button2,则将获得您想要的行为.

就像测试一样,我在禁用按钮的情况下尝试了这种情况,通过变量来使单击单击并使按钮不可见.奇怪的是,该按钮不可见,它仍然可以单击鼠标.我什至尝试删除了click委托,然后重新应用它,没有任何正常"的工作.

可能的解决方案是在后台线程上运行测试或进行任何测试,然后在线程完成时重新启用button2.
Based on the code that you posted, it''s easy to see why you are seeing the behavior that you are seeing. The click event for button2 is being processed after button1''s click event. That is to say that button2 was enabled when button1''s click event finished and button2 was free to process the click. If button2 was not re-enabled within the button1 click event that you would get the behavior you seek.

Just as a test I tried this scenario with the button diabled, igoring the click via a variable and making the button invisible. Oddly enough with the button invisible it still received the mouse click. I even tried removing the click delegate and then reapplying it and nothing "normal" worked.

A possible solution would be to run your test or whatever it is on a background thread and re-enable button2 on the thread completion.


这篇关于扔掉按钮的点击声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆