且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c #webbrowser文档为null

更新时间:2023-12-06 15:24:04

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

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

 List< GeckoWebBrowser&gt ; 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");