For example I have a nested if-cycle in console like
if (answer == "one")
{
Console.WriteLine("one or two?");
string answer = Console.ReadLine();
if (answer == "one")
{
Console.WriteLine("pressed1");
}
else
Console.WriteLine("pressed2")
//....and so on
}
Everything works fine. But if I try to do the same in winform, it turns out that by pressing Button1 I get all the nested results marked as "btn1".So I am getting "pressed 1 pressed 1". How can I make my program stop the same way it stops in console with Console.ReadLine()?
if (btn.Name == "btn1")
{
richTextBox1.AppendText("Pressed1");
if (btn.Name == "btn1")
{
richTextBox1.AppendText("pressed1");
}
else (btn.Name == "btn2")
{
richTextBox1.AppendText("pressed2");
}
The posted code fragment isn't full, but it's easy to speculate what the confusion is.
Basically, your console application is being processed in a sequential order, with certain blocking calls where input is injected into the flow.
On your WinForm application on the other hand, flow is event based. There is no predetermined sequence of events, and no blocking calls.
Your console code inspects the value of the
answer
variable in the outerif
and then waits for a new input from the user for further processing. Your WinForm code inspectsbtn.Name
, appends some text to a text box, and then immediately moves on to the nextif
statement. If you want to stop this sequence,return
from your event handler, and continue your flow in a different event handler.A note for potential -1's: Yes, I know a console application can be event based, doesn't have to be sequential in nature and so on. This answer is supposed to help the OP understand the logic & problem.