Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
hello, i want to use the if else construct on forms/buttons. here is the situation, on a form i have a textbox,save button and start button, the save button saves user input that has been provided through the textbox, now when the user clicks the start button,the button opens another form. i want the start button to make sure that the user has clicked the save button.
code samples would help....

thanks in advance
Posted

Lets take two buttons cmdSave and cmdStart (for the Save and Start buttons).

I would have a class level boolean flag (lets call it flag).

On the cmdSave click method, I would set this flag to true.

C#
public void OncmdSaveClick(object sender, EventArgs e)
{
flag = true;
}



Whenever the cmdStart button is clicked I would check for this flag. Only if this flag is set (the Save button had been clicked earlier, I would run the form). If not, I would just step out of the method.

C#
public void OncmdStartClick(object sender, EventArgs e)
{
   //Call the new form if the flag is set
   if (flag)
    {
      CallNewForm(); //Calling the new form
    }
}


This way, you will be sure the Save button has been clicked at least once.
 
Share this answer
 
v3
Comments
Uday P.Singh 3-Sep-11 13:26pm    
my 5!
Abhinav S 3-Sep-11 13:28pm    
Thanks Uday.
Wendelius 3-Sep-11 13:26pm    
Easy to agree, my 5 :)
From what I gathered:
- in your form load, set a form level variable isSaved to false:
C#
private bool isSaved = false;

- in the save button click handler, change the state of the variable:
C#
isSaved = true;

- in the start button click handler, check that isSaved is true:
C#
if (!isSaved) [
   System.Windows.MessageBox.Show("Save first or something");
   return;
}
// do the actual start code...
 
Share this answer
 
Comments
Abhinav S 3-Sep-11 13:25pm    
My 5. Your answer is quite similar to mine.
You are a minute later however. :)
Wendelius 3-Sep-11 13:26pm    
Yes, I noticed. Actually it was only 37 seconds late :)
Abhinav S 3-Sep-11 13:27pm    
Ha ha. :)
Uday P.Singh 3-Sep-11 13:26pm    
my 5!
Wendelius 3-Sep-11 13:28pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900