1个按钮,2个网页,但一次一个网页
本文关键字:网页 一次 一个 按钮 1个 2个 | 更新日期: 2023-09-27 18:11:37
这是我们目前的情况:
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被点击按钮三将打开网页1,如果按钮2被点击按钮3将打开网页2,按钮3的图像将根据情况而改变,但我发现我所做的是什么到目前为止,它打开两个页面在同一时间…如何预防这种情况?我试过if, else和else if,每次结果都一样。
您的两个按钮都是启用的,您正在检查按钮是否启用或禁用(可单击或不可单击),而不是哪个按钮已被单击。
:如果(button2。启用== true)嵌套在第一个条件句中,我不确定这是否是你想要的。
你可以:禁用按钮1和2后,他们点击,例如button2。启用将现在= 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");
}
您可能还必须检查并确保先前没有分配事件处理程序,以防有人单击button1,然后单击button2,然后单击button1等。下面描述:删除事件处理程序
您可以通过将网页的URL存储在一个私有字段中,在单击按钮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
中的所有代码是否都是必要的,所以我稍微清理了一下。我可能有点偏离了。
按钮。默认情况下,所有按钮的Enabled总是为true,除非将其设置为false。所以你不能使用button1。属性检查按下哪个按钮。试试下面的方法。
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
}
}