1
votes

I have an application that has 3 attached forms. I have set it all Top Most, so every form will look like the extension of parent form. Now users doensn't want it to be on top. When I set the TopMost to false, the forms seems to be separated. I want to bring the all the forms to front, if any of these forms come on top (by clicking, clicking on the taskbar icon, or even using ALT TAB). I think, if there was a bring to front event, that will solve my issue.

2
I think your abusing TopMost completely, you shouldn't ever need to set them to top most, just set the dialogs parent instead - Sayse
The TopMost property gets abused too much. It certainly isn't a cure for what you are complaining about, it doesn't solve a Z-ordering problem. What you are looking for is an owned window. It is always on top of its owner, it minimizes along with its owner. You already know them well, the various tool windows inside Visual Studio are owned windows. You create an owned window by using the Show(owner) overload to display it. Or by setting the Owner property explicitly. - Hans Passant
Thanks for the suggestions friends... Now, I get the clue.. - Yesudass Moses

2 Answers

0
votes

WinForm suppose we have a mainform, and two other forms

mainForm otherForm1 otherForm2

Set the Owner property

otherForm1.Owner = mainForm;

otherForm2.Owner = mainForm;

These three forms will now always come to the foreground top-most together. Further useful setting:

otherForm1.ShowInTaskBar = False;

otherForm2.ShowInTaskBar = False;

The group of three forms share a single icon on the taskbar.

0
votes

Try setting the parent in the ctor of every child form:

ChildForm frm = new ChildForm(parentForm);

Try to use Win32-functions to hack the foreground behavior:

public static class User32
{
    public const int SW_HIDE = 0;
    public const int SW_SHOW = 5;
    public const int SW_SHOWNORMAL = 1;
    public const int SW_SHOWMAXIMIZED = 3;
    public const int SW_RESTORE = 9;

    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern bool AllowSetForegroundWindow(uint dwProcessId);
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}

User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);