Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Checking if button was clicked

Tags:

c#

click

button

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 was clicked, which opens a new Form. Now I have the event for the "complete" button.

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

My problem is, I can't find a method that gives something like `button1.Clicked or something similar.

I hope someone can help me here..

like image 891
ItsLuckies Avatar asked May 14 '13 22:05

ItsLuckies


1 Answers

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;
    }
}
like image 145
Jan Dörrenhaus Avatar answered Nov 19 '22 15:11

Jan Dörrenhaus