C#消息框出现两次
C# message box appears twice
我正在尝试做一个简单的魔兽世界头像"extractor"。这是我的代码:
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;
}
}
}
}
问题是我单击了一次图像,如果字符是正确的,我单击 "Yes" 一切正常。但是,如果我第二次单击该图像,消息框将弹出,然后我选择了我的答案,但消息框再次弹出。如果我第三次单击图像,消息框将出现 3 次...
有什么想法吗?
每次在以下行中单击该事件,您都会额外订阅一次该事件:
webBrowser1.DocumentCompleted += GetImg;
我正在尝试做一个简单的魔兽世界头像"extractor"。这是我的代码:
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;
}
}
}
}
问题是我单击了一次图像,如果字符是正确的,我单击 "Yes" 一切正常。但是,如果我第二次单击该图像,消息框将弹出,然后我选择了我的答案,但消息框再次弹出。如果我第三次单击图像,消息框将出现 3 次... 有什么想法吗?
每次在以下行中单击该事件,您都会额外订阅一次该事件:
webBrowser1.DocumentCompleted += GetImg;