如何使用在另一个Button语句中检查Button? C#

时间:2017-11-29 13:06:44

标签: c#

我遇到了问题。我想使用if语句检查是否单击了一个按钮。例如:

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

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

但它没有像这样工作,因为" .click"只能在" + ="的左侧或" - ="。知道如何检查Button2是否被点击了吗?

代码是这样的:我想检查button2以停止" programm"。 对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);
        }*/
    }

3 个答案:

答案 0 :(得分: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)
   {
      //...
   }
}

答案 1 :(得分:0)

您可以使用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
    {

    }
}

答案 2 :(得分:0)

除了你想要的奇怪行为之外......由于你没有使用Threads,你有以下选择:

本地功能(.Net&gt; 4.7)

@PowerMockIgnore({ "javax.xml.*", "org.xml.sax.*", "com.sun.xml.*", "com.sun.org.*"})

线程

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
}

异步方法。

我希望你找到解决方案。欢呼声。

编辑: 因为你想要的是中断你正在做的任何事情的过程,你唯一的选择是如上所示的局部功能,如果你没有绑定到特定的框架版本。

BackgroundWorker并检查按钮2是否被其他答案中提到的标志物按下的每一步。

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

编辑2: 使用Threads更新了答案,我建议如果你使用这个选项,那么使用BackgroundWorker要好得多,因为你将完全控制这个过程,只有在可以打破它的地方打破它。 / p>