Reputation: 340
I am currently working on a small program for a school project, but can't seem to find a solution at all.
I have been working on a windows forms with a label that displays a text. Then there are two buttons. If you a any of these buttons are clicked, the other is hidden and the text of the button that has been clicked changes.
Now I want to check if the text of any of these buttons has been changed and upon a second click on the now changed button close the application. Below is what I have so far.
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "You wake up.\n\n"+
"What would you like to do?";
button1.Text = "I'd like to eat some cerials!";
button2.Text = "I need to go to the bathroom!";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "You stand up, go to the kitchen and eat some cerials. Nice!";
button1.Text = "Hurray, you ate some cerials";
button2.Hide();
// if (button1.TextChanged +=)
// {
//
// }
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "You don't finde the Bathroom in this house and have to use the garden.";
button2.Text = "You get arested afterwars!";
button1.Hide();
}
public void EndGame()
{
Close();
}
Is there a way to actually do this or do I need some workarounds?
Upvotes: 0
Views: 972
Reputation: 944
Edit:
Double click on button to add click event
public Form1()
{
InitializeComponent();
}
//copy from here
public class AppState
{
public string labelText { get; set; }
public bool endGame { get; set; }
public bool success { get; set; }
public List<AppState> answers { get; set; }
}
AppState level = new AppState()
{
labelText = "You are ... you can go 1.left 2.right 3.further ",
answers = new List<AppState>{
new AppState() {
labelText = "You are dead",
endGame = true,
},
new AppState() {
labelText = "You are winner",
endGame = true,
success = true
},
new AppState() {
labelText = "You are bla bla.. 1. left 2. right",
answers = new List<AppState>{
new AppState() {
labelText = "You are dead",
endGame = true,
},
new AppState() {
labelText = "You are winner",
endGame = true,
success = true
}
}
}
}
};
public void Render()
{
label1.Text = level.labelText;
if (level.endGame)
{
button1.Enabled = false;
if (level.success)
{
MessageBox.Show("winner");
}
else
{
MessageBox.Show("looser");
}
}
}
public void Answer()
{
int ans = int.Parse(textBox1.Text);
level = level.answers[ans - 1];
Render();
}
// copy to here
//this you need to do manualy with form editor
private void Form1_Load(object sender, EventArgs e)
{
Render();
}
//this you need to do manualy with form editor
private void button1_Click(object sender, EventArgs e)
{
Answer();
}
Upvotes: 1
Reputation: 41
There are few ways of doing this easily. One way is just have a global boolean value that shifts when I button is clicked. If your boolean is named boolean endProgram = false
, then your if statement would look like this:
if(endProgram)
{
EndGame()
}
Just in case you don't know this, if(endProgram)
is the same thing is if(endProgram == true)
. This only work with boolean values.
Just put the same thing into the other button click event and that should do the trick.
Upvotes: 1