Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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");



       }
Posted
Updated 4-Jul-13 20:50pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Jul-13 16:36pm    
Please add some tags: platform, UI library used, or application type. In particular, when you talking about WebBrowser control — which one? Fully qualified name, please.
—SA
AlphaDeltaTheta 5-Jul-13 2:46am    
He is talking about Skybound.Gecko a winforms version of Gecko layout engine
AlphaDeltaTheta 5-Jul-13 2:52am    
Put a breakpoint on the line, debug and see.

1 solution

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:
C#
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:
C#
Skybound.Gecko.GeckoWebBrowser Browser = new Skybound.Gecko.GeckoWebBrowser();
BrowserList.Add(Browser);

In the SetText1() function, you should pass the selected tab index
C#
void SetText1(int index, string attribute, string attName, string value)
{
Skybound.Gecko.GeckoElementCollection tagsCollection = BrowserList[index].Document.GetElementsByTagName("input");
...

}

When you call SetText1() function
C#
SetText1(tabControl2.SelectedIndex, "name", "income", "aaa");
 
Share this answer
 
v2
Comments
Soloveikok 5-Jul-13 5:51am    
Okay this solved, but how to manipulate to code so I could make multiple browser tabs, in each tab I define a new browser, so it didnt disapear when i create a new tab?
thanh_bkhn 5-Jul-13 5:59am    
You can use a List of browser to store browsers for each tab.
Soloveikok 5-Jul-13 8:05am    
could you give me a code sample please?
thanh_bkhn 5-Jul-13 11:03am    
See my updated solution :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900