C# Checking if button was clicked

前端 未结 4 1927
情书的邮戳
情书的邮戳 2020-12-30 12:03

I am making a program that should just continue if 2 conditions are given.

The first one, 2 TextBoxs have the same word in and a Button wa

4条回答
  •  我在风中等你
    2020-12-30 12:24

    Click is an event that fires immediately after you release the mouse button. So if you want to check in the handler for button2.Click if button1 was clicked before, all you could do is have a handler for button1.Click which sets a bool flag of your own making to true.

    private bool button1WasClicked = false;
    
    private void button1_Click(object sender, EventArgs e)
    {
        button1WasClicked = true;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox2.Text == textBox3.Text && button1WasClicked)
        { 
            StreamWriter myWriter = File.CreateText(@"c:\Program Files\text.txt");
            myWriter.WriteLine(textBox1.Text);
            myWriter.WriteLine(textBox2.Text);
            button1WasClicked = false;
        }
    }
    

提交回复
热议问题