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

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

Related

Incorporating WebView2 in a WinForm Custom Control

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.

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.

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.

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.

how to get the control of the UI Element Clicked in a external application

How can i get the control (and possibly its text value) of a control which was clicked in another third party application (its not a .net or wpf application for which there are answers which did not solve my problem)
I can get the click event in my app (using the global hooks as mentioned here) i want the control/handle of that particular UI Element
Example : I have opened Notepad and when i click on File Menu, i want the control of that File button.
Look at the documentation for SetWindowsHookEx using the WH_CALLWNDPROC param. This will let you intercept messages in Notepad window procedure. You'll need to figure out which messages get generated from clicking the menu items in Notepad, you can use Spy++ for that. But there is no real control for the File button, it is part of a menu.

Categories