c #webbrowser文档为null [英] c# webbrowser document is null

查看:117
本文介绍了c #webbrowser文档为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段。首先我添加GeckoWebbrowser。然后我启动表单,我访问一些页面,等到消息框文件加载弹出,然后单击按钮调用功能Settext(),所以文件肯定完成,当我点击按钮我得到一个错误, Browser.Document未设置为object的实例。

任何想法?



  void  SetText1( string 属性,字符串 attName, string   value 
{





// 获取名称为input的所有标签的集合;

Skybound.Gecko .GeckoElementCollection tagsCollection = Browser.Document.GetElementsByTagName( input);

foreach (Skybound.Gecko.GeckoElement currentTag in tagsCollection)
{


// 如果当前标签的属性具有名称attName

if (currentTag.GetAttribute(attribute).Equals(attName))
{
// 然后设置其属性value。

currentTag.SetAttribute( value value );

currentTag.Focus();
}
}



// SendKeys.Send({ENTER});
}

private void button6_Click( object sender,EventArgs e)
{

tabControl2.SelectedTab.Controls 。明确();
Skybound.Gecko.GeckoWebBrowser Browser = new Skybound.Gecko.GeckoWebBrowser();
Browser.Navigated + = new Skybound.Gecko.GeckoNavigatedEventHandler(Browser_Navigated);
Browser.Navigating + = new Skybound.Gecko.GeckoNavigatingEventHandler(Browser_Navigating);
Browser.DocumentCompleted + = new EventHandler(Browser_DocumentCompleted);
tabControl2.SelectedTab.Controls.Add(Browser);

Browser.Parent = tabControl2.SelectedTab;
Browser.Dock = DockStyle.Fill;
Browser.Navigate(textBox1.Text);
tabControl2.SelectedTab.Text = textBox1.Text;
}
void Browser_DocumentCompleted( object sender,EventArgs e)
{
MessageBox.Show( Doc已加载);
}
私有 void button7_Click( object sender,EventArgs e)
{
SetText1( name income aaa);



}

解决方案

在你的 button6_Click(),你创建了一个新的GeckoWebBrowser实例,但它是一个本地对象。所以它在SetText1函数中为null。

你应该将GeckoWebBrowser Browser的声明作为GeckoWebBrowser类型的类数组:

 List< GeckoWebBrowser> ; BrowserList =  new 列表< GeckoWebBrowser>(); 



然后每次创建新标签时,创建一个新的浏览器对象,然后选项卡索引将等同于列表中浏览器的索引:

 Skybound.Gecko.GeckoWebBrowser Browser =  new  Skybound.Gecko.GeckoWebBrowser(); 
BrowserList.Add(浏览器);



SetText1()函数中,你应该通过选定的选项卡索引

  void  SetText1( int  index, string 属性,字符串 attName, string   value 
{
Skybound.Gecko.GeckoElementCollection tagsCollection = BrowserList [index] .Document.GetElementsByTagName( input);
...

}



当你调用SetText1()函数时

 SetText1(tabControl2.SelectedIndex,  name 收入  aaa ); 


I have the following code snippets. First I add the GeckoWebbrowser. Then I launch the form, I visit some page, wait till messagebox "Doc loaded pops up", then Click the button to call Function Settext(), So the document is definitely completed, When I click the button I get an error that the Browser.Document is not set to instance of object.
Any ideas?

void SetText1(string attribute, string attName, string value)
       {





               // Get a collection of all the tags with name "input";

               Skybound.Gecko.GeckoElementCollection tagsCollection = Browser.Document.GetElementsByTagName("input");

               foreach (Skybound.Gecko.GeckoElement currentTag in tagsCollection)
               {


                   // If the attribute of the current tag has the name attName

                   if (currentTag.GetAttribute(attribute).Equals(attName))
                   {
                       // Then set its attribute "value".

                       currentTag.SetAttribute("value", value);

                       currentTag.Focus();
                   }
               }



           //  SendKeys.Send("{ENTER}");
       }

private void button6_Click(object sender, EventArgs e)
       {

           tabControl2.SelectedTab.Controls.Clear();
           Skybound.Gecko.GeckoWebBrowser Browser = new Skybound.Gecko.GeckoWebBrowser();
           Browser.Navigated += new Skybound.Gecko.GeckoNavigatedEventHandler(Browser_Navigated);
           Browser.Navigating += new Skybound.Gecko.GeckoNavigatingEventHandler(Browser_Navigating);
           Browser.DocumentCompleted += new EventHandler(Browser_DocumentCompleted);
           tabControl2.SelectedTab.Controls.Add(Browser);

           Browser.Parent = tabControl2.SelectedTab;
           Browser.Dock = DockStyle.Fill;
           Browser.Navigate(textBox1.Text);
           tabControl2.SelectedTab.Text = textBox1.Text;
       }
void Browser_DocumentCompleted(object sender, EventArgs e)
       {
           MessageBox.Show("Doc has loaded");
       }
 private void button7_Click(object sender, EventArgs e)
       {
           SetText1("name", "income", "aaa");



       }

解决方案

In your button6_Click(), you create a new instance of GeckoWebBrowser, but it is a local object. So it's null in SetText1 function.
You should put the declaration of GeckoWebBrowser Browser as a class array of type GeckoWebBrowser:

List<GeckoWebBrowser> BrowserList = new List<GeckoWebBrowser>();


Then each time you create a new tab, create a new browser object, then the tab index will be equivalent to the browser's index in the list:

Skybound.Gecko.GeckoWebBrowser Browser = new Skybound.Gecko.GeckoWebBrowser();
BrowserList.Add(Browser);


In the SetText1() function, you should pass the selected tab index

void SetText1(int index, string attribute, string attName, string value)
{
Skybound.Gecko.GeckoElementCollection tagsCollection = BrowserList[index].Document.GetElementsByTagName("input");
...

}


When you call SetText1() function

SetText1(tabControl2.SelectedIndex, "name", "income", "aaa");


这篇关于c #webbrowser文档为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆