How to capture the WebBrowser control close event? - c#

I have a WebBrowser control in a WPF application. The browser control is in a grid and when the WebBrowser control renders some webpages, in one of the pages there is popup alert box from the webpage, and when the user taps on the popup the WebBrowser control seems to be closed. I need to capture this close event and take some action.
I already tried subscribing to the unload event but it does not seem to fire. Is there any way to get this event captured?

There is WindowClosing event on the underlying WebBrowser ActiveX control for that. I haven't tried it myself, but I think it should work.
Here's a full-fledged sample showing how handle the "underlying" WebBrowser events like that. It's for WPF, but it can be easily adapter for WinForms (using WebBrowser.ActiveXInstance).

Related

How use AddressChanged event in CefSharp WPF?

I try to develop WebBrowser on c#, wpf and CefSharp for experience.
Use tabcontrol for tabs and TextBox for URL textbox.
I want, when address of page is changed, textboxurl was updated too.
So in CefSharp for WinForms i find AddressChanged event.
Like in this video https://www.youtube.com/watch?v=itpEj1yB_Tg
But when i want use it with CefSharp WPF i cant use it.
So maybe i can add it by myself?
I find ChromiumWebBrowser.OnAddressChanged Method in APIDoc.
http://cefsharp.github.io/api/51.0.0/html/M_CefSharp_Wpf_ChromiumWebBrowser_OnAddressChanged.htm
How can i use it?

How to enable keyboard events in WebBrowser control in Office custom task pane

I have a WebBrowser control shown in a custom task pane in an Microsoft Office Application-level add-in created in Visual Studio. The web page shown in the WebBrowser doesn't receive keyboard events that can be handled by JavaScript code, such as KeyUp. The same page shown in a comparable WebBrowser in a Form does receive keyboard events. The WebBrowser control itself doesn't seem to expose any events related to keyboard input, and I don't seem to be able to handle keyboard events by adding event handlers to the UserControl added as a custom task pane.
I have created a minimal example – a Visual Studio solution – which recreates the problem.
Is there any way to pass on keyboard events to the web page?
Yup, you can do it. You'll have to hook into the HTML DOM events (like onclick, onmouseover, etc.). Take a look here: http://www.w3schools.com/jsref/dom_obj_event.asp.
And there's an example here:
http://www.codeproject.com/Articles/547451/WebBrowser-Element-Events-and-Values
I think you'll have to add a reference to the MSHTML library. BTW, put the WebBrowser control in a Panel control or it will act goofy - especially with keyboard events.
Actually I found a solution is to use the WebBrowser under Excel namespace
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx
Obviously it is subclassing the windows form WebBrowser and fix the keyboard issues by handing some windows message manually.

Handling the events of a Interop control in C#

In a Win32 C# App, we are using a AxInterop control, it has some buttons on it like Ok, Cancel,etc...now in the C# code when I call and open the window of that AxInterop I want to see how can I know that user clicked on Ok buttton for example in that control so I can handle some stuff in C# code....
You can only use whatever the control exposes. You cannot capture the control events if they do not capture your click and translate it to a custom click that you can handle.
If you don't have the control documentation you can examine the controls methods/properties/events ... pressing F2.

WinForms Web Browser control forcing refocus?

I'm trying to automate a web process where I need to click a button repeatedly. When my code "clicks" that button (an HtmlElement obtained from the WebBrowser control I have on my form) then it brings focus back to my application, more specifically the WebBrowser control. I wish to better automate this process so that the user can do other things while the process is going on, but that can't happen if the window is unminimizing itself because it's attaining focus.
The code associated with the clicking is:
HtmlElement button = Recruiter.Document.GetElementById("recruit_link");
button.InvokeMember("click");
I've also tried button.RaiseEvent("onclick") and am getting the exact same results, with focus problems and all.
I've also tried hiding the form, but when the InvokeMember/RaiseEvent method is called, whatever I was working on loses focus but since the form is not visible then the focus seems to go nowhere.
The only non-default thing about the webbrowser is it's URI being set to my page and ScriptErrorsSuppressed being set to True.
Why do You need to click this button? It's sending some form?
If yes, you can use WebClient and simulate sending form without graphic interface.
Basicly almost anything significant requires connection to website using Get Post or multipart/post so WebClient will be perfect. You can simply get the site wich is this button on and parse it to get what clicking it do. And then simulate your own action.
In IE7 or higher you can implement IProtectFocus on the webbrowser site and deny the focus change. You would be much better off if you use the raw ActiveX or its wrappers that support this kind of customization, e.g. csexwb. If you have to use the winform webbrowser control, you need to create your own webbrowser site.

RDP ActiveX Ctrl+Alt+Del

In our C# application we use the RDP viewer as an ActiveX control. The application has its own toolbar with a Ctrl+Alt+Del button. There does not seem to be a method on the ActiveX control to perform this function. I know you can hit Ctrl+Alt+End on your keyboard which is fine but how do I do that from the toolbar button click?
Try to use the SendKeys class.
In this example, you could send CTRL+ALT+END as this:
SendKeys.Send("(^%{END})");

Categories