views:

36

answers:

3

I'm on a thread other than the UI thread and need to display a modal form that's centered on the application's main form. What I usually do is use the width and height of the main form and the modal form to calculate the location, then use the PointToScreen method of the main form to get the location of the modal form. Since I'm on another thread I need to use Control.Invoke to call this method. I just can't figure out how to pass a parameter of type Point to Control.Invoke (params object[]). Value types and String works fine.

Or, if someone can find a better way to display a form centered on the main form regardless of thread, that would be great. MessageBox seems to be able to do this (although not modally).

EDIT

I have no problem calling something like this.

mainform.Invoke(..., object[] { someString });

The problem is this:

public Point Control.PointToScreen(Point p)

This method takes a Point as a parameter and returns a Point. How can I call this via Control.Invoke?

A: 

myWhatever.Invoke(myDelegate, new object[] { myParam1, myParam2, myParam3 });

thelost
What's the type of myParam1? Try this.myWhatever.Invoke(myDelegate, new object[] { new Point(5, 5) });If you can make it work, please tell me how.
Rubio
FWIW, you don't have to create the object array yourself, you could just Invoke(del, p1, p2, p3) and let the compiler do it for you. Yay for params!
James Manning
A: 

Take a look at this, you can adapt the function UpdateStatusBarMessage_OnNewStatusMessage, to display your form instead of writing a message.

Sres
Can you do this?public delegate Point PointToScreenDelegate (Point point);...private static Point ThreadSafePointToScreen(Point point){ if (mainwin != null else ...}
Rubio
Oops, that would be return mainwin.Invoke(...). The point is (pun intended), PointToScreen requires a Point as a parameter and return a Point.
Rubio
That shouldn't be a problem, I pass my own class objects in the same way.In the section new object[] { new Point(5,5) }); you should be able to do new object[] {ptObj}); that you pass through the function to return the value you want back to the form.Don't forget you need reference to the mainwin as the parent object. Can you not pass details through and create the form back in the main thread?
Sres
A: 

WRT "a better way to display a form centered on the main form regardless of thread, that would be great" - you indeed don't need to do this centering manually. :)

  1. Before displaying (calling ShowDialog, presumably), set the StartPosition property on the child dialog to CenterParent.
  2. In the case the active window (has the button that creates the dialog) isn't the intended parent (sounds unlikely given your scenario), you can call the ShowDialog overload that lets you specify the parent. I don't think you'll need this, but this lets you parent the dialog somewhat it arbitrarily. :)
James Manning
The parent seems to be null so CenterParent doesn't work. The Location property of my form ends up being 0,0. But I had totally forgotten about the ability to specify parent in ShowDialog. Thanks, James.
Rubio