Podolski
Podolski

Reputation: 85

How do I use a while with when a button is clicked?

I have a piece of code where I want it so that it won't execute that piece of code until the necessary button is clicked, in this case a play again button. Here is my piece of code:

private void ShowResultsOfThrow()
{
    TwoUp.SetUpCoins();
    TwoUp.ThrowCoins();
    TwoUp.OutputWinner();

    do
    {
       TwoUp.ThrowCoins();
       TwoUp.OutputWinner();
       TwoUp.HoldPoints();
    } while //when the button is clicked to play again

    TwoUp.ResetGlobals();
}

Upvotes: 0

Views: 161

Answers (3)

Ravindra Bagale
Ravindra Bagale

Reputation: 17673

Just put that code in button_click event

private void  button1_click(object sender, EventArgs e)
{
   TwoUp.ThrowCoins();
   TwoUp.OutputWinner();
   TwoUp.HoldPoints();
}

Upvotes: 2

Anirudha
Anirudha

Reputation: 32817

If you have access to the button.you can do this

let us assume the button name is btnClick

let there be a method that would get called when the button is clicked.

private void  TwoUpMethod(object sender, EventArgs e)
{
     TwoUp.ThrowCoins();
     TwoUp.OutputWinner();
     TwoUp.HoldPoints();
     TwoUp.ResetGlobals(); // Optional - not sure what this does...
}

Now register this method with the Click event of the button.Put this below code in InitializeComponent() method of the form

btnClick.Click+=TwoUpMethod;

Upvotes: 0

Blachshma
Blachshma

Reputation: 17405

Based on your comments:

Don't use the ShowResultsOfThrow() method. Instead, if your button is named Button1, for instance, try this:

private void Initalize()
{
     TwoUp.SetUpCoins();

}

private void  Button1_click(object sender, EventArgs e)
{

     TwoUp.ThrowCoins();
     TwoUp.OutputWinner();
     TwoUp.HoldPoints();
     TwoUp.ResetGlobals(); // Optional - not sure what this does...
}

Initalize() should only be called ONCE (or whenever you need to setup the coins)

Upvotes: 0

Related Questions