interop >> ShowDialog and Event Handling

by Sm9obk1TeXJhc29mdA » Wed, 28 Mar 2007 08:17:45 GMT

Hi, I'm having an issue handling events when I use ShowDialog() on my
WinForm. I have a COM-visible class. This COMWrapper exposes a public
method that launches a WinForm. I want my end-users to have functionality
like Tab when they navigate this .NET WinForm from the COM client. To enable
this, I have to use Form.ShowDialog().

In the WinForm, I have a public event and I raise this event when a button
on the form is clicked. The COMWrapper class instantiates a module-scope
instance of the WinForm class using the WithEvents keyword. COMWrapper also
exposes a public event. In COMWrapper's event handler for the WinForm event,
I raise the COMWrapper's local event. In this way, I expect to notify the
COM client when an event is raised on the WinForm even though there is no
direct instance of the WinForm in COM.

My expected behavior works if I call the WinForm using Show(). However,
ShowDialog() blocks this message. Since I need the TAB behavior as described
above, how can I get the 'best of both worlds'? Thanks in advance.

Similar Threads

1. Form.ShowDialog() / ShowDialog(IWin32Window) - CSharp/C#

2. Custom Event handling + thread handling.

I've written an event that tells me that I've connected properly to a 
remote telnet location.  When I know that I'm connected I fire the event 
which is being listened for in a bit of code that then goes on and does 
some work based on this event.

My problem is that once I fire that event, the thread that fired it 
waits until the thread that received it finishes.  I fixed the problem 
by making the bit of code that is listening for the event continue on 
with what it is doing in the background, but I'm wondering if there is a 
different way.

Specifically, the code is going through a csv file and then doing a few 
things on a remote system to get information based on something in the 
csv, and then processing that information once we get it.

Everything works fine, but I'd like to know what I'm doing wrong, in 
that I don't think that the thread should be tied up waiting for 
whatever was listening for the event to finish.

Any help, or pointing in the right direction is as usual, very much 
appreciated.

regards,
M.

3. Calling ShowDialog on MouseDown event

4. ShowDialog, Events and Control Invoke

Hi,

I have 2 forms that subscribe to the same event published by the same
instance of an object. In each case the event  handler invokes the
invalidation of a member panel using a reference to initial form in the
application - ie definatley the GUI thread.

The problem arises when I launch form B from form A with ShowDialog(). Now
when the event is fired my invoke is called on the first form to subscribe -
A, this call never completes and B's handler is not called by the event
delegate. One can assume that a control cannot be invoked when it is waiting
for ShowDialog to return.

Whilst I think I understand the problem I am suprised at the conclusion I
must draw. In order to remain vsually responsive to a particular event forms
may not be shown modally.

Has anyone else come up against this problem? There must be a work around
there somewhere. Is this what BeginInvoke in the full framework is for?



5. SerialPort event not firing after ShowDialog - C# - .NET Windows Forms

6. ShowDialog and Load Event

I am trying to troubleshoot an application I inherited.  On a form, there is 
a button, that when clicked loads a user control with ShowDialog;

btn_Click(object sender, System.EventArgs e)
{
   ucForm1 UCForm1 = new ucForm1();
   UCForm1.ShowDialog(this.ParentForm);
....
}

in the ucForm1.cs there is a Load event defined;

ucForm1_Load(object sender, System.EventArgs e)
{
    .....
}

When the app runs and the user clicks the button, the dialog form appears 
but the the load event is never triggered.  Does the Load event not get fired 
when a form is presented using the ShowDialog method?  If it doesn't, is 
there another event I can use to run the code that I wanted to run in the 
load event?  Or am I missing something?

thanks for any help you can provide.

Dan 
dchman

7. How to catch KeyUp or KeyPress event in a modal window (ShowDialog) - .NET Windows Forms

8. Events fired when Form.ShowDialog is called

Hello everybody,

The MSDN says about ShowDialog that

"When a form is displayed as a modal dialog box, clicking the Close button 
(the button with an X at the upper-right corner of the form) causes the form 
to be hidden and the DialogResult property to be set to DialogResult.Cancel. 
Unlike modeless forms, the Close method is not called by the .NET Framework 
when the user clicks the close form button of a dialog box or sets the value 
of the DialogResult property. Instead the form is hidden and can be shown 
again without creating a new instance of the dialog box. Because a form 
displayed as a dialog box is not closed, you must call the Dispose method of 
the form when the form is no longer needed by your application."

The way I understand this is that even if I re-instantiate the class that 
implements my form, the framework will reuse some previously created 
structures and that's how I would explain why the Load event is not fired for 
subsequent calls to ShowDialog.

So here is my question: What is the pattern I need to use to display one 
form over and over and make sure that all internal controls are initialized 
to proper values every time the form is shown? What is the event that's fired 
every time ShowDialog is called, if there is such an event? If not, how else 
should I approach this?

TIA,
Eddie