【问题标题】:How to use check a Button in another Button statement? c#如何在另一个 Button 语句中使用检查 Button? C#
【发布时间】:2017-11-29 13:06:43
【问题描述】:

我有问题。我想使用 if 语句来检查是否单击了按钮。例如:

public void button1_Click(object sender, EventArgs e)
{
    while (1)
    {
         ...
         ...
         ...

         if (Button2 == clicked)
         {
              break;
         }
    }
}

但它不是这样工作的,因为“.click”只能在“+=”或“-=”的左侧。知道如何检查 Button2 是否被点击?

代码是这样的:我想检查 button2 以停止“程序”。 对 Button2 的检查几乎在代码的末尾;)

    public void button1_Click(object sender, EventArgs e)
    {
        Random rnd = new Random();
        int EmFilterPos;
        int ExFilterPos;
        string String1;
        int[] EmLB = new int[126];
        int[] ExLB = new int[126];
        int LBEmAnzahl = 0;
        int LBEmTot = 0;
        int LBExAnzahl = 0;
        int LBExTot = 0;

        UInt32 C_Zyklen;
        UInt32 Zyklen;

        Roche.DetectionControl2.Device_Filterwheels.ELBPowerState LB_On = Roche.DetectionControl2.Device_Filterwheels.ELBPowerState.LBOn;
        Roche.DetectionControl2.Device_Filterwheels.ELBPowerState LB_Off = Roche.DetectionControl2.Device_Filterwheels.ELBPowerState.LBOff;
        Roche.DetectionControl2.Device_Filterwheels.fiweGetLBResponse LightBarrier;

        string Text = String.Format("Filterrad-Dauertest\r\nGestart am {0:d} um {0:t}\r\n\r\n", DateTime.Now);
        System.IO.File.WriteAllText(@"TestLogFile\Filterrad_Dauertest1.txt", Text);

        Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweInitFilter();
        System.Threading.Thread.Sleep(50);

        while (Zyklen <= 20)
        {
            for (int q=1;q<8;q++)
            {
                Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweMove(q,q);
                System.Threading.Thread.Sleep(50);
                Zyklen++;


            }

            for (int w=0;w<7;w++)
            {
                ExFilterPos = rnd.Next(1,8);
                EmFilterPos = rnd.Next(1,8);
                Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweMove(ExFilterPos,EmFilterPos);
                System.Threading.Thread.Sleep(50);
                Zyklen++;
            }

            C_Zyklen = Zyklen;
            if ((C_Zyklen % 2) < 14)
            {
                Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweInitFilter();
                System.Threading.Thread.Sleep(50);
                using (System.IO.StreamWriter file = new System.IO.StreamWriter (@"TestLogFile\Filterrad_Dauertest1.txt", true))
                {
                    file.Write("Init bei: ");
                    String1 = String.Format("{0,7}",Zyklen);
                    file.Write(String1);
                    file.Write(file.NewLine);
                }

                ExFilterPos = 60;
                EmFilterPos = 60;
                Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweRawMove(ExFilterPos,EmFilterPos);
                System.Threading.Thread.Sleep(50);
                Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweSetLB(LB_On);

                while (EmFilterPos != -60)
                {
                    LightBarrier = Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweGetLB();
                    if (LightBarrier.LBEm == Roche.DetectionControl2.Device_Filterwheels.ELBState.LBbright)
                    {
                        LBEmAnzahl++;
                        LBEmTot += EmFilterPos;
                    }
                    if (LightBarrier.LBEx == Roche.DetectionControl2.Device_Filterwheels.ELBState.LBbright)
                    {
                        LBExAnzahl++;
                        LBExTot += ExFilterPos;
                    }
                    ExFilterPos--;
                    EmFilterPos--;
                    Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweRawMove(ExFilterPos,EmFilterPos);
                }
                EmFilterPos = LBEmTot / LBEmAnzahl;
                ExFilterPos = LBExTot / LBExAnzahl;

                using (System.IO.StreamWriter file = new System.IO.StreamWriter (@"TestLogFile\Filterrad_Dauertest1.txt", true))
                {
                    file.Write("Nullstelle Mittelposition Em-Filter: ");
                    file.Write(EmFilterPos);
                    file.Write(file.NewLine);
                    file.Write("Nullstelle Mittelposition Ex-Filter: ");
                    file.Write(ExFilterPos);
                    file.Write(file.NewLine);
                    file.Write(file.NewLine);
                }

                Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweSetLB(LB_Off);
            }

            if (Button2 == clicked)   // or something like this
                 break;
        }

        using (System.IO.StreamWriter file = new System.IO.StreamWriter (@"TestLogFile\Filterrad_Dauertest1.txt", true))
        {
            file.Write("Beendet am {0:d} um {0:t}\r\n", DateTime.Now);
        }*/
    }

【问题讨论】:

  • Button2 没有被点击。 Button1 被点击。因为您在 Button1 的单击处理程序中。还是两个按钮都使用同一个处理程序? (在这种情况下,它可能应该重新命名。)您到底想完成什么?
  • @AlekDepler:您的猜测可能恰好是正确的。但 OP 仍应在问题中澄清。
  • 你得到那个错误是因为 .Click 是一个事件,而不是一个属性,你已经得到了答案,你有一个选项是使用一个标志。
  • no 带有标志的东西不起作用,因为如果程序在 Button1 语句中,它不会离开它,直到它完成孔过程,所以如果我按 Button1 启动程序它会完成它,只有现在我才能将 button2 标志设置为 true

标签: c#


【解决方案1】:

嗯……

bool b1clicked = false, b2clicked = false;
public void button2_Click(object sender, EventArgs e)
{
   b2clicked = true;
}

public void button1_Click(object sender, EventArgs e)
{
   b1clicked = true;
   if (b1clicked && b2clicked)
   {
      //...
   }
}

【讨论】:

    【解决方案2】:

    除了你想要的怪异行为......而且由于你没有使用线程,你有以下选择:

    本地函数 (.Net > 4.7)

    private void B_Click(object sender, EventArgs e)
    {
        bool clickFlag = false;
        void Click(object sender2, EventArgs e2)
        {
            clickFlag = true;
        }
    
        b2.Click += Click;
    
        while (!clickFlag)
        {
            Thread.Sleep(1);
        }
    
        b2.Click -= Click;
    
    
        //Continue with your stuff
    }
    

    线程

    Thread newThread;
    private void Button1_Click()
    {
        newThread = new Thread(YourBreakableProcess);
        newThread.Start();
    }
    private void Button2_Click()
    {
        newThread.Join();
    }
    private void YourBreakableProcess()
    {
        //Your breakable process
    }    
    

    异步方法。

    希望你能找到解决办法。干杯。

    编辑: 由于您想要的是中断您正在做的任何事情的过程,如果您没有绑定到特定的框架版本,那么您唯一的选择就是如上所示的本地功能。

    BackgroundWorker 并检查每个步骤是否按下按钮 2 并使用其他答案中提到的标志。

    线程,并创建一个线程。按下按钮 2 时加入。

    编辑 2: 使用 Threads 更新答案,我建议如果您使用此选项,最好使用 BackgroundWorker,因为您将拥有对进程的整个控制权,仅在可以破坏它的地方破坏它。

    【讨论】:

    • 它工作正常,我用线程做的,非常感谢@TareqB。
    • 很高兴听到这个消息,但我真的建议您改用 backgroundworker,它很容易实现,而且没有任何不良行为,因为您可以选择何时中断循环,只需检查在您的流程中的任何步骤之前的工人。 Join 将在任何步骤中破坏线程,例如,假设您在文件创建过程中执行 join。
    【解决方案3】:

    您可以使用flag 变量来实现此目的。声明并将flag值初始化为false。在button2点击更改标志值到true如下,

    private bool flag= false;
    
    private void button2_Click(object sender, EventArgs e)
    {
        flag= true;
    }
    
    public void button1_Click(object sender, EventArgs e)
    {
        //Use flag to check whether button 2 has clicked or not
        if (flag)
        {
    
        }
        else
        {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      相关资源
      最近更新 更多