Is there an equivalent to Mac OS X Document modal sheet in .NET?

In my application, there were more and more requests that some dialogs behave similarly to the functionality of the Mac OS X Document modal Sheet , where the dialog is modal only to the parent control / dialog, and not the entire application (see http://en.wikipedia.org / wiki / Window_dialog ).

The current ShowDialog () windows are not sufficient for the needs of my application, since I need the dialog to be modal for another dialog in the application, but still allow the user to access other areas of the application.

Is there an equivalent to Document Modal Sheet in C # .NET? Or even the cramped implementation someone made, or am I on my own to try to implement this functionality? I tried searching Google and SO to no avail.

Thank,

Kyle

+2
c # modal-dialog
Oct 12 2018-11-12T00:
source share
2 answers

After revising this problem, I worked a little and found a hybrid solution that would work for my needs.

I accepted p-daddy's suggestion : https://stackoverflow.com/a/166778/

And I changed the code to work for 32-bit and 64-bit compilers, using the hans-passant clause : https://stackoverflow.com/a/4646262

The result is the following:

const int GWL_STYLE = -16; const int WS_DISABLED = 0x08000000; public static int GetWindowLong(IntPtr hWnd, int nIndex) { if (IntPtr.Size == 4) { return GetWindowLong32(hWnd, nIndex); } return GetWindowLongPtr64(hWnd, nIndex); } public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong) { if (IntPtr.Size == 4) { return SetWindowLong32(hWnd, nIndex, dwNewLong); } return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); } [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)] private static extern int GetWindowLong32(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)] private static extern int GetWindowLongPtr64(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)] private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)] private static extern int SetWindowLongPtr64(IntPtr hWnd, int nIndex, int dwNewLong); public static void SetNativeEnabled(IWin32Window control, bool enabled) { if (control == null || control.Handle == IntPtr.Zero) return; NativeMethods.SetWindowLong(control.Handle, NativeMethods.GWL_STYLE, NativeMethods.GetWindowLong(control.Handle, NativeMethods.GWL_STYLE) & ~NativeMethods.WS_DISABLED | (enabled ? 0 : NativeMethods.WS_DISABLED)); } public static void ShowChildModalToParent(IWin32Window parent, Form child) { if (parent == null || child == null) return; //Disable the parent. SetNativeEnabled(parent, false); child.Closed += (s, e) => { //Enable the parent. SetNativeEnabled(parent, true); }; child.Show(parent); } 
+2
Apr 18 '13 at 16:45
source share

The Form.ShowDialog method allows Form.ShowDialog to specify the owner when you call it. In this case, the form is modal only for this owner.

EDIT: I tried this with mixed results. I created a simple Windows Forms application with a main form and two others. By clicking on the main form, I opened Form2 using the Show method. Form2 also has a button on it, and when clicked, Form3 opens using the ShowDialog method, passing Form2 as the owner. While Form 3 was indeed modal for Form2, I couldn't go back to Form1 until I closed Form3.

+1
Oct 12 '11 at 14:33
source share



All Articles