Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法强制转换到按钮_C# - Fatal编程技术网

C# 无法强制转换到按钮

C# 无法强制转换到按钮,c#,C#,我得到了这个错误 发生类型为“System.InvalidCastException”的未处理异常 在WindowsFormsApplication2.exe中的其他信息:无法强制转换 类型为“System.Windows.Forms.Button”的对象,以键入 “System.Windows.Forms.WebBrowser” 这是密码 private void button6_Click(object sender, EventArgs e) { int count

我得到了这个错误

发生类型为“System.InvalidCastException”的未处理异常 在WindowsFormsApplication2.exe中的其他信息:无法强制转换 类型为“System.Windows.Forms.Button”的对象,以键入 “System.Windows.Forms.WebBrowser”

这是密码

private void button6_Click(object sender, EventArgs e)
    {
        int counter = 0;
        string line;
        System.IO.StreamReader file = new System.IO.StreamReader("test.txt");

        while ((line = file.ReadLine()) != null)
        {
            textBox6.Text = "";
            textBox5.Text = "";
            textBox4.Text = "";
            textBox2.Text = "";

            textBox6.Text = counter.ToString();
            string link = line.ToString() ;
            textBox5.Text = link;
         //   WebRequest req = HttpWebRequest.Create(link);
         //   req.Method = "GET";


            WebBrowser wb = (WebBrowser)sender;
            wb.Navigate(link);
            wb.Document.ExecCommand("SelectAll", false, null);

            wb.Document.ExecCommand("Copy", false, null);

            string html = wb.DocumentText;
            textBox1.Text = html;
            textBox2.Text = Clipboard.GetText();
            textBox3.Text = "done";

            if (html.Contains("<title>"))
            {
                string title = ExtractFromString(html, "<title>", "</title>");
                textBox4.Text = title;
            }
            else
            {
                string title = ExtractFromString(html, "<TITLE>", "</TITLE>");
                textBox4.Text = title;
            }

            wb.Dispose();
            createpage(textBox4.Text, textBox2.Text, textBox5.Text);


            counter++;
        }
        file.Close();
        textBox3.Text = counter.ToString();


    }
  }
}
private void按钮6\u单击(对象发送者,事件参数e)
{
int计数器=0;
弦线;
System.IO.StreamReader file=new System.IO.StreamReader(“test.txt”);
而((line=file.ReadLine())!=null)
{
textBox6.Text=“”;
textBox5.Text=“”;
textBox4.Text=“”;
textBox2.Text=“”;
textBox6.Text=counter.ToString();
字符串链接=line.ToString();
textBox5.Text=链接;
//WebRequest req=HttpWebRequest.Create(链接);
//req.Method=“GET”;
WebBrowser wb=(WebBrowser)发送方;
wb.导航(链接);
wb.Document.ExecCommand(“SelectAll”,false,null);
wb.Document.ExecCommand(“复制”,false,null);
字符串html=wb.DocumentText;
textBox1.Text=html;
Text=Clipboard.GetText();
textBox3.Text=“完成”;
if(html.Contains(“”)
{
字符串标题=ExtractFromString(html,“,”);
textBox4.Text=标题;
}
其他的
{
字符串标题=ExtractFromString(html,“,”);
textBox4.Text=标题;
}
wb.Dispose();
createpage(textBox4.Text、textBox2.Text、textBox5.Text);
计数器++;
}
file.Close();
textBox3.Text=counter.ToString();
}
}
}

发送方几乎可以肯定是一个
按钮(从函数名“button6\u click”)

按钮
无法强制转换为
WebBrowser
(原因很明显)。在本例中,您可能需要按名称引用
WebBrowser
控件

WebBrowser wb = someExistingNamedWebBrowser;

您的问题在于这一行:

WebBrowser wb = (WebBrowser)sender;
由于这是在按钮单击事件中,因此
发送方
将是一个
按钮
。试试这个:

WebBrowser wb = new WebBrowser();
或者,如果您的表单上已经有一个,请参考它:

WebBrowser wb = webBrowser1;

事件处理程序由按钮引发。你试着把它扔给网络浏览者。这显然行不通。尝试修复WebBrowser wb=(WebBrowser)发送方;给wb分配一些权利。我刚刚做了这个,循环只是通过循环,从未执行过导航链接或文档完成:s@AdamMoore如果我在没有发件人的情况下使用webbrowser,则在System.Windows.Forms.dll中出现“System.Reflection.TargetInvocationException”类型的未处理异常。其他信息:无法获取“WebBrowser”控件的窗口句柄。不支持无窗口ActiveX控件。您正在运行一个。具体描述一下,你想做什么。
WebBrowser wb = webBrowser1;