我正在尝试做一个简单的魔兽世界头像“提取器”。这是我的代码:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("https://worldofwarcraft.com/en-gb/search?q=" + textBox4.Text);
webBrowser1.DocumentCompleted += GetImg;
}
}
private void GetImg(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string result = "";
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "Avatar-image")
{
result = (el.OuterHtml).Substring(el.OuterHtml.IndexOf("https"));
result = result.Substring(0, result.IndexOf(")"));
pictureBox1.ImageLocation = result;
DialogResult YESNO = MessageBox.Show("Is this your character?", "Select your char", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (YESNO == DialogResult.Yes)
{
break;
}
}
}
}
问题是我单击图像一次,如果字符是正确的,我点击“是”,一切都很好。但是,如果我第二次单击图像弹出消息框,那么我选择了我的答案但是再次弹出消息框。如果我第三次点击图像,消息框将出现3次... 有什么想法吗?
答案 0 :(得分:7)
每次在此行上点击该活动时,您都会订阅该活动:
webBrowser1.DocumentCompleted += GetImg;