1个按钮,2个网页,但一次只有一个网页?

时间:2015-09-08 11:25:08

标签: c# function button

这是H到目前为止所做的:

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");
         }
    } 
} 

这只是我的测试到目前为止,但我想做的是改变按钮3的作用,即如果点击按钮1按钮3将打开网页1,如果按钮2被点击按钮3将打开网页2,按钮3的图像将改变取决于,但我发现我迄今为止所做的是它同时打开两页页面...如何防止这种情况?我试过if,else和if if,每次都是相同的结果。

3 个答案:

答案 0 :(得分:0)

您可以通过将网页的网址存储在私人字段中,在点击按钮1或2时设置它并在点击按钮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);
        }
    }
} 

我不确定button3_Click中的所有代码是否都是必要的,所以我清理了一下。不过,我可能有点偏僻。

答案 1 :(得分:0)

button.Enabled默认情况下对所有按钮都是true,除非您将其设置为false。因此,您无法使用button1.Enabled属性来检查按下了哪个按钮。尝试以下方法。

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
    }
}

答案 2 :(得分:0)

您的两个按钮都已启用,您正在检查按钮是启用还是禁用(可点击与否),而不是单击哪一个。

还:if(button2.Enabled == true) 嵌套在第一个条件中,我不确定这是否是你想要的。

您可以:在点击后禁用按钮1和2,以便例如button2.Enabled现在= false; (但之后你将无法重新点击该按钮)

更复杂但更好的是使用button3的委托,并在button1_Click和button2_Click事件中分配它们。像这样:

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");
    }

您可能还需要检查并确保先前未分配事件处理程序,在calse中有人单击button1,然后单击button2,然后单击button1等。这在此处描述:Removing event handlers