Incorporating WebView2 in a WinForm Custom Control - c#

I'm trying to incorporate a WebView2 control in a WinForm application. This application, uses a web browser in multiple pages (tabs, dialog forms) and then I have created a custom control (named WebViewCtrl in my sample code) to incorporate the WebView2 control together with some buttons (address text, go next, go previous and refresh).
Until I added the custom control to the main window, all seemed to work fine. When I put the control in a modal dialog window, when I need to close the dialog (named TestDialog) the webView2 control loses is father remaining suspended in the screen.
You can check this behaviour in a very simple working sample that I have loaded in github with the essential code to reproduce the issue:
https://github.com/LeonardoDaga/WebView2-Dialog-Sample
I don't understand if I need to do something to kill the control before I dispose the dialog form. Please help if you have any suggestion.

I downloaded your code and found out that you have to Dispose your UserControl when you close your dialog.
In the TestDialog form, add the following event handler to the Form Closed event:
private void TestDialog_FormClosed(object sender, FormClosedEventArgs e)
{
this.userControl11.Dispose();
}
Now the WebView2 will vanish, when you close the dialog.

Related

C# - Using a NotifyIcon from an User Control

This is something that surprisingly wasn't able to find a solution online.
I have in my main form a couple of user control, that after doing a certain action, I want to show a notification icon.
The examples shown about doing from a different form don´t apply to User Controls, as you can't directly reference the form that is using it.
In the case the question is a bit confusing:
Application
- Main Form
--NotifyIcon
--UserControl
---Button that when pressed, show the NotifyIcon
--UserControl
---Button that when pressed, show the NotifyIcon
I´m using VS2010 by the way.

drop window in application into the main window like tab

i am working on application with multiple windows, but i want to drop any window in the application into the main window like tab and can drop it back like window.
i have tried to used drop event in C# and MouseDown event in xaml but didn't work
so any idea how to do that?
You can't have a child "Window" within a window.
You can have a child user control.
If you still want to have a child window, it would have to appear as a popup
Or you can do a custom implementation like
http://clipflair.codeplex.com/
http://www.c-sharpcorner.com/uploadfile/mahesh/wpf-child-window/
Updated Answer
For docking window you can use third party tools like
Devexpress
Avalon
Telerik
Or for a custom implementation you can try these examples
http://www.codeproject.com/Articles/140209/Building-a-Docking-Window-Management-Solution-in-W
http://www.codeproject.com/Articles/439873/Simple-Visual-Studio-like-Pane-Resizing-Docking-an
http://www.codeproject.com/Articles/22927/Multiple-Window-Interface-for-WPF
http://www.codeproject.com/Articles/18812/WPF-Docking-Library

How to capture the WebBrowser control close event?

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).

How can I get MenuStrip work when its parent form is embedded in another application

Now that i have a WinFormApp with MenuStrip called "TargetForm", and it's embedded in another WinFormApp which's "HostForm", by calling the API function SetParent. But i found the MenuStrip cease to function no matter how i click on it
Seems that the TargetForm's MenuStrip has not realized that the mouse has been click when it was hosted by HostForm.
I'm using SPY++ to monitor the Windows messages on the TargetForm, and found WM_PARENTNOTIFY was raised when i click on the menu
Is it possible that i can post the Windows message WM_PARENTNOTIFY to the MenuStrip from HostForm?
I would put the contents of the main form of the embedded app into a WinForms User Control (put it into a separate project), and embed the user control into both applications.

.NET (C#) passing messages from a custom control to main application

A custom windows form control named 'tweet' is in a dll. The custom control has couple of basic controls to display a tweet. I add this custom control to my main application. This custom control has a button named "retweet", when some user clicks this "retweet" button, i need to send some message to the main application. Unfortunately the this tweet control has no idea about this main application (both or in their own namespaces)
How can i send messages from this custom control to the main application?
One way is to add an event to your control and have the control fire that event when it needs to send a message. The main form can add an event handler when it creates the form and be notified of the messages. This way your control does not need to have any hard-coded reference to the main form.
You raise and handle the event. See the code sample on this page (you might have to choose the language on the page to be C#)
http://msdn.microsoft.com/en-us/library/9aackb16(v=VS.71).aspx

Categories