views:

1210

answers:

3

I have problems with bringing a windows mobile 6 form to the front. I tried things like this already

Form1 testForm = new Form1();
testForm.Show();
testForm.BringToFront();
testForm.Focus();

But it's always behind the form that includes that code. The only things that have worked for me are

testForm.TopMost = true;

or Hide(); the old form and then show the new one, but i want to avoid hiding the other form. TopMost isn't very clean anyway with using multiple other forms.

The other thing that works is

testForm.ShowDialog();

but I don't want to show the form modal.

To cut it short. I just want to show the new form in front of another form, and if I close it, I want to see the old form again.

Maybe someone can help me with this problem. Thank you.

+2  A: 

I haven't tried it in WM6, but you can use some pinvoke to call Win32 functions:

[DllImport("coredll.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("coredll.dll", EntryPoint="SetForegroundWindow")]
private static extern int SetForegroundWindow(IntPtr hWnd);

Call FindWindow to get the handle and then call SetForegroundWindow. Other functions you may found useful:

ShowWindow, BringWindowToTop, SetWindowPos

kgiannakakis
A: 

This works very well, thanks. And now I found out why the simple Show()-Method doesn't worked. I tried to show the new form while the other was still loading. After loading, the old form comes back to front.

Any tips on showing a form directly after another form was loaded? Load- and GotFocus-Events don't work because of the problem above. Timer should work, but thats ugly.

[EDIT] Found a solution that works for me now. I'm showing the new form first, and after closing that one the other gets displayed.

Problem solved.

b1tsn4ck
Why not wait for your parent form to load (using Load and GotFocus events) and then show the new form? In other words remove the show command from the constructor and place it in one of the event handlers.
kgiannakakis
The problem is, when I put the show method into the Load or GotFocus-Event, the new form will be shown and comes to the front. And when the Load/GotFocus reaches its end, the "caller"-form is shown and covers the new one.
b1tsn4ck
A: 

Try this:

Put a timer on the form.
Set it's tick short say 100ms.
In the timer_Tick event
- disable the timer (so it doesn't tick again) then
- load the child form.

Also you might want to look at the form.owner property:
"When a form is owned by another form, it is minimized and closed with the owner form. For example, if Form2 is owned by form Form1, if Form1 is closed or minimized, Form2 is also closed or minimized. Owned forms are also never displayed behind their owner form."

MBoy