0

Here's what H have so far:

private void button1_Click(object sender, EventArgs e)
{
    button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}

private void button2_Click(object sender, EventArgs e)
{           
    button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}

private void button3_Click(object sender, EventArgs e)
{ 
    audio.Stop();
    if (button1.Enabled == true)
    {       
        timer1.Stop();
        pictureBox1.Visible = false;
        System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");

         if (button2.Enabled == true)
         { 
             timer1.Stop();
             pictureBox1.Visible = false;
             System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
         }
    } 
} 

this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
rtj3000
  • 21
  • 2

3 Answers3

0

You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.

private string _address = null;

private void button1_Click(object sender, EventArgs e)
{
    // do other stuff
    _address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}

private void button2_Click(object sender, EventArgs e)
{
    // do other stuff
    _address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}

private void button3_Click(object sender, EventArgs e)
{
    if (_address != null)
    {
        audio.Stop();
        if (button1.Enabled || button2.Enabled)
        {
            timer1.Stop();
            pictureBox1.Visible = false;
            System.Diagnostics.Process.Start(_address);
        }
    }
} 

I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.

Kapol
  • 6,383
  • 3
  • 21
  • 46
0

button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.

protected void Button1_Click(object sender, EventArgs e)
{
    ViewState["Button1Clicked"] = true;
}

protected void Button2_Click(object sender, EventArgs e)
{
    ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
    if ((bool)ViewState["Button1Clicked"])
    {
        //open webpage2 code comes here
    }
    else 
    {
        //open webpage2 code comes here
    }
}
0

Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.

also:if (button2.Enabled == true) is nested in the first conditional, I'm not sure if that's what you want.

You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)

More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:

private void button1_Click(object sender, EventArgs e)
    {
        button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
        button3.Click += new EventHandler(this.Button3_Click_First);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
        button3.Click += new EventHandler(this.Button3_Click_Second);
    }

    void Button3_Click_First(Object sender,
                       EventArgs e)
    {
        // When the button is clicked,
        // change the button text, and disable it.

        timer1.Stop();
        pictureBox1.Visible = false;
        System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
    }

    void Button3_Click_Second(Object sender,
                       EventArgs e)
    {
        timer1.Stop();
        pictureBox1.Visible = false;
        System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
    }

You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers

Community
  • 1
  • 1
Chad McGrath
  • 1,561
  • 1
  • 11
  • 17
  • thank you this worked perfectly, i can now have 10 buttons displayed , to show what they need to see , once one is clicked , the other 2 buttons open up for 1st and second series perfectly thank u – rtj3000 Sep 10 '15 at 10:51