How can I access a component from a MudDialog? - c#

The following code returns an IDialogReference:
IDialogReference myComponentDialog = DialogService.Show<MyComponent>("MyComponent", parameters, options);
I can't pass a reference as an argument, so I'm hoping there is some way to extract "MyComponent from the IDialogReference.
This way I can call a method from MyComponent.

The MudDialog does not internally maintain a component reference to the component used as content.
If you really must communicate TO the content component FROM outside the dialog, then you could use an EventBus.
Create a Wasm Event Bus: https://github.com/cpear/BlazorComponentBus
Publish the event from outside the MudDialog
Subscribe to the event from your content component
Respond to the event in the content component by calling the method that you'd like to call from outside the MudDialog.
Opinion:
When a MudDialog is shown then the only interaction should be with that UI component, so the user should not be able to initiate an activity from outside the content component, so not sure why you'd need to send a message to an active dialog from outside.
Perhaps that dialog should be updated based on an event (maybe SignalR from server or from a wasm background service). In which case, your content component could subscribe directly to that event instead of another component outside the dialog handling the event and then trying to call a method on the active dialog content component.

Related

Perform dragdrop implementation after DoDragDrop method is called?WPF

I've been struggling with this for quite a while
My wpf application contains a list view, populated with file-names, which are located on a server.
I'm trying to implement drag and drop functionality, so the user can drag files from my application into his/her's local computer.
In order to do this, first i'm downloading the files into a temporary location, and then calling my application's DoDragDrop() method.
The problem is that I want to perform the download process only after the DoDragDrop method is called.
I've tried every event related to drag drop methods (GiveFeedback, ItemDrag, etc...) but nothing works
so basically what I need is an event, raised after the DoDragDrop is done
any ideas?
The current scheme is to drag a file with a special suffix, and then turn on global file monitoring to obtain the drag location of the file through monitoring.

Event-Handling buttons on WinForm from a DLL

I have written a C# utility class (DLL), which gets called by a windows application. Within the windows application, I have setup a backgrounderWorker to allow for time-consuming code not to hog the windows application. The time-consuming code is in the utility class DLL. Now, I setup two buttons on the windows application a 'submit' button - where the time-consuming code gets called in the utility class and a 'cancel' button.
I would like the 'cancel' button to stop the backgroundWorker code if clicked. Problem is the the 'Cancel' button is in the windows application and the code is in the DLL. So is there a way for me to maybe attach the 'cancel' button's 'onClick' eventHandler to the DLL and then have the DLL periodically check to see if the button was pressed?
BTW, the cancel button does work up until the DLL code gets initiated.
Am I correct in my thoughts or is there a better way? Any help would be appreciated.
Yes, It is possible to register to events between projects. all you have to do is have a reference of the winforms application inside your dll, and have the cancel button public. then you can register to any of it's events inside the dll code. However, I'm not sure that this is the best way to do what you want. I think a better way to do it is have a method called Cancel inside the dll and have the cancel button click event call this function. This way your dll is less dependent on your winforms application.
I actually went back and looked at some documentation I had about events and event handling. I did the following in the DLL:
Made a public class an added an eventHandler, my own custom eventArgs, and a subclass (eventWatcher) to host an event (call it onCancel)
I registered eventWatcher to listen for the OnCancel event in the DLL.
I made the eventWatcher public so it can be consumed in the client app.
In the client app, I did the following:
I modified the cancel button to raise the onCancel event. This allowed me to assign a method in the DLL to handle the graceful exiting of the time-consuming code in the DLL.
It now works as expected!

Getting events of another application

In my c# application I need to detect when a user clicks one of two buttons in a different, third party application.
I am able to get the Handle of the application but the MainWindowHandle returns 0.
I tried WndProc but for some reason the event will not fire in my application.
How can I get/intercept the button click event from that application into mine?
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult to implement in C#. This project on Codeplex attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue. Put simply, this lets you implement any type of global Windows hook from managed code: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx

Intercepting click event for all controls in an app in C# (WinForms)

I want to make an application to intercept all UI events in all the forms of my application and to write them to a log. This data can than be used to see which controls are the most used, in what order, etc. The problem is that I want this to happen automatically, without modifying the existing classes.
I made a prototype that attaches a method to a click event for all controls in a form, but how can this be done for all forms? Reflection needs a target object when manipulating events, but only the startup form can be easily accessed.
Is there a way to hook the constructor of an object? Then I could "inject" my method in all the events of the new form. Or maybe there is another way to do this.
Thanks in advance!
You can install a message filter.
A message filter is an object that implements IMessageFilter. WinForms calls your PreFilterMessage method for every message that passes through your thread's message loop. This is enough to monitor user input across the application (and gives you the option of manipulating it).
In Windows API this is done using local hooks (you can set local mouse hook using SetWindowsHookEx function). This is the proper way to do your task. In C# you need to use P/Invoke in order to get access to SetWindowsHookEx.
One more task would be to match the HWND (windows handle) to corresponding WinForms control.
Read this article for how to do this (via WM_GETCONTROLNAME message).
Also see this question which is a duplicate of yours.
You will have to work with the Win32's API Messages, I guess.
Here's a little example under the form of tutorial.
You should be able to achieve what you want with message filters - no direct P/Invoke to Win32-APIs required!
See the help on the IMessageFilter interface for more info.

Get Navigating event when JS opens a new browser window using WebBrowser control

I'm using C# / .NET 4. I have a form with a WebBrowser component on it. I have loaded an external web page to the WebBrowser component.
I have an event handler attached to the Navigating event. This works for most things. however, one part of the web site I'm loading executes a window.open(url) javascript command. This has the effect of opening a new IE window with the URL, and my event handler does not get called.
I need a solution that either:
Calls my event handler with the URL that is about to be opened, allowing me to capture the URL and cancel the event as usual.
Replace the Javascript function (there are many JS functions) that calls window.open(url) with some JS equivalent that will open the new page in the same window, thereby calling my event handler.
Im not versed with the Webbrowser-control, but there is a NewWindow-Event, sounds like it's the thing you are looking for.

Categories