Hi All,
I've got a problem with form.ShowDialog :
I've got a first form (F1)witch call another with F1.Show (call F2 in exemple), this form call another with F2.ShowDialog.
The problem is that I want use F1 in same time that F3 but F1 is frozen because of the F2.ShowDialog (and I need F3 in Modal mode with F2)
Someone can help me ? |
| ElDim Thursday, August 23, 2007 6:12 AM |
Add a new class to your project and paste this code:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices;
public static class Util { private static List<Form> mKeepEnabled = new List<Form>(); public static DialogResult ShowDialog(Form dlg, Form owner, params Form[] keepEnabled) { foreach (Form frm in keepEnabled) mKeepEnabled.Add(frm); mKeepEnabled[0].BeginInvoke(new MethodInvoker(EnableForms)); return dlg.ShowDialog(owner); } private static void EnableForms() { foreach (Form frm in mKeepEnabled) EnableWindow(frm.Handle, true); mKeepEnabled.Clear(); } // P/Invoke declarations [DllImport("user32.dll")] private static extern bool EnableWindow(IntPtr hWnd, bool enable); }
Display the dialog with code like this:
Form3 f3 = new Form3(); DialogResult res = Util.ShowDialog(f3, this, Application.OpenForms[0]); // Use the dialog results //... f3.Dispose();
The first argument of Util.ShowDialog is the instance of the form you want to display. The second argument is the dialog owner, it can be null. The third and subsequent arguments are references to the forms you want to keep enabled while the dialog is active. I just passed a reference to the startup form.
Note that the user can now do naughty things like starting another instance of Form2 and start the dialog again. It is up to you to prevent this. If this is starting to sound like a bad idea, I couldn't disagree. |
| nobugz Thursday, August 23, 2007 4:01 PM |
You can't use ShowDialog(). This is the closest equivalent:
Form3 mForm3; private void button1_Click(object sender, EventArgs e) { if (mForm3 == null) { mForm3 = new Form3(); mForm3.FormClosing += Form3_Closing; } mForm3.Show(this); this.Enabled = false; } private void Form3_Closing(object sender, FormClosingEventArgs e) { this.Enabled = true; mForm3 = null; }
|
| nobugz Thursday, August 23, 2007 8:13 AM |
Thank you for response,
It's not realy the same... but it can work...
is it possible to keep showdialog but with F1 make a new thread to show F2 ? |
| ElDim Thursday, August 23, 2007 9:02 AM |
With new thread it works
|
| ElDim Thursday, August 23, 2007 9:22 AM |
nobugz, your solution doesn't take care about automatic dialogresult... and new thread cause some bugs on tooltips... someone can help me ?
|
| ElDim Thursday, August 23, 2007 2:12 PM |
Just assign the DialogResult property in Form3's OK button's event handler and read it back in the Closing event handler. |
| nobugz Thursday, August 23, 2007 2:23 PM |
instead of that, can you help me to OverLoads ShowDialog method ?
(this because that I need to use it more than one time)
|
| ElDim Thursday, August 23, 2007 2:29 PM |
Add a new class to your project and paste this code:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices;
public static class Util { private static List<Form> mKeepEnabled = new List<Form>(); public static DialogResult ShowDialog(Form dlg, Form owner, params Form[] keepEnabled) { foreach (Form frm in keepEnabled) mKeepEnabled.Add(frm); mKeepEnabled[0].BeginInvoke(new MethodInvoker(EnableForms)); return dlg.ShowDialog(owner); } private static void EnableForms() { foreach (Form frm in mKeepEnabled) EnableWindow(frm.Handle, true); mKeepEnabled.Clear(); } // P/Invoke declarations [DllImport("user32.dll")] private static extern bool EnableWindow(IntPtr hWnd, bool enable); }
Display the dialog with code like this:
Form3 f3 = new Form3(); DialogResult res = Util.ShowDialog(f3, this, Application.OpenForms[0]); // Use the dialog results //... f3.Dispose();
The first argument of Util.ShowDialog is the instance of the form you want to display. The second argument is the dialog owner, it can be null. The third and subsequent arguments are references to the forms you want to keep enabled while the dialog is active. I just passed a reference to the startup form.
Note that the user can now do naughty things like starting another instance of Form2 and start the dialog again. It is up to you to prevent this. If this is starting to sound like a bad idea, I couldn't disagree. |
| nobugz Thursday, August 23, 2007 4:01 PM |
GREAT JOB, THANK YOU !
Very good, I'vetranslated it to my form class with another overload
I work in vb.net and it's nearly the same code.
It works properly. Bye |
| ElDim Friday, August 24, 2007 6:34 AM |