Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Access textbox in the document and get or change the text, also mouseclick and submit actions on it, also focus on it and select input.
Access the URL of current page and put it in a textbox on the AddressChanged event of Chromium browser (another thread that the main).

Also how to suppress script errors in it?

TabControlMain has tabPageGoogleTranslate that has chromeBrowser.

System.InvalidOperationException: 'Cross-thread operation not valid: Control 'tabControlMain' accessed from a thread other than the thread it was created on.'


The code below shows the error above in debugging because it can't access tabControlMain from the thread of chromeBrowser.

What I have tried:

ChromiumWebBrowser chromeBrowser;
        void InitChromeBrowser()
        {
            Cef.Initialize(new CefSettings());
            chromeBrowser = new ChromiumWebBrowser("http://translate.google.com");
            tabPageGoogleTranslate.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
            chromeBrowser.AddressChanged += ChromeBrowser_AddressChanged;
        }

        void ChromeBrowser_AddressChanged(object sender, AddressChangedEventArgs e)
        {
            if (tabControlMain.SelectedTab == tabPageGoogleTranslate)
            {
                textBoxURL.Invoke(new Action(() => textBoxURL.Text = chromeBrowser.Address));
            }
        }
Posted
Updated 19-Dec-18 0:09am
v10
Comments
F-ES Sitecore 19-Dec-18 5:46am    
We don't know what "tabControlMain" is, and you haven't posted the error message so I doubt there is much anyone can do to help you.
john1990_1 19-Dec-18 5:58am    
Thx, updated.
F-ES Sitecore 19-Dec-18 6:15am    
I'm assuming there is some multi-threading going on here that you haven't mentioned and isn't in the code you posted. If you want to manipulate elements on your form you can only do it from code running on the same thread as the UI is on. That's why you are doing "txtBoxUrl.Invoke", you are causing that Action to run on the same thread that txtBoxUrl was created on. You have to do that with all code that accesses UI elements. Maybe create an "AddressChanged" method on your form and in the ChromeBrowser_AddressChanged do nothing but invoke it on the right thread.
john1990_1 19-Dec-18 6:21am    
Thx, how do I invoke a long code block to the main thread from the thread of the chromeBrowser.
F-ES Sitecore 19-Dec-18 6:30am    
Put the code in a method and use Invoke to call that method. There is an example of using delegates to invoke methods here

https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls

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