HowTo stop a form from being blocked when another modal form is sh

G

Guest

I have a main application (Form1) that can among other things, display a
customers details. the customer details form is non modal, and the code that
launched the customer details form keeps a reference of all the customer
details forms being shown and then either brings the form to the front if the
customer is already being displayed, or creates a new form and displays the
customer details.

I now have a modal form (Form2) that can be launched from Form1. From Form2
I also somtimes want to display a customer details. If the customer details
are already displayed I can bring the customer details form to the front, but
the form is blocked. How can I make the Customer details forms not be
blocked by modal forms (however I do want Form 1 to be blocked by modal Form
2)

Thanks, Giles.
 
G

Guest

Hi Jeffrey,

Thank you very much for the detailed reply.

I have tried this and it works as you say. Unfortunatly it does throw up
some potential concerns about multi-threading (with the code I execute within
the forms), which make me shy away from this solution at the moment.

Do you know if there is any way to keep this all on the same thread and to
somehow 'unblock' the custom details form that has been blocked by the modal
form.

Thanks again,

Giles.
 
J

Jeffrey Tan[MSFT]

Hi Giles,

Thanks for your feedback.

Yes, I see your concern, we usually do our best to avoid multithreading.
However, just as I original stated, .Net winform internal code will disable
all the windows in the current thread of owner, so without touching the
source code there is no way for us to change this behavior.

Maybe we can wokaround using ShowDialog mehtod, just implementing a
customized modal dialog behavior ourselves. For example, when showing the
"customized modal" dialog, we just show out this form, and just ONLY
disable the Form1, do not disable the other forms. Like this:
public CustomForm cf=null;
private void button1_Click(object sender, System.EventArgs e)
{
cf=new CustomForm();
cf.Show();
}

private void button2_Click(object sender, System.EventArgs e)
{
Form2 f=new Form2();
f.Owner=this;
f.Show();
this.Enabled=false;
}

Currently, this works well on my side. If you have other concern, please
feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

I forget to paste the Form2 code:

private void button1_Click(object sender, System.EventArgs e)
{
Form1 f=this.Owner as Form1;
if(f!=null)
{
f.cf.BringToFront();
}
}

private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Form1 f=this.Owner as Form1;
if(f!=null)
{
f.Enabled=true;
}
}

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Thanks Jeffrey for your reply,

There are more than one forms that would need to be disabled in the project,
so I would need to iterrate through all the windows and programatically
disable them - which is a possibility - but not nice.

Another option that I have come up with is that when I want to show a custom
details form that is already shown (but being blocked) - I programatically
close it and then create a new one and show this - I would obviously only
want to do this if the existing form IS being blocked by a modal form.
Without keeping tabs on what modals were displayed when - is there any way of
programatically establishing if a form is currently being blocked?

Of course If I do ultimatley have to go with the multi-threading option this
does raise one other problem that I would need to overcome... Sometimes
from the custom forms I am going to have to show a modal dialogue - now this
modal I would want to block everything - on both threads. - is there any way
to do this - Ie make both threads wait on the same dialogue.

thanks,

Giles
 
J

Jeffrey Tan[MSFT]

Hi Giles,

Thanks for your feedback.

Sorry, but based on your reply, we seem to make the design much complex
now.

First, I do not encourage you to close/re-open the custom details form to
bring it to top. Because if there is any state information on the form, it
will be close to lose.

Second, for the second solution I provide you, I do not understand your
concern very well. What does "There are more than one forms that would need
to be disabled in the project, so I would need to iterrate through all the
windows and programatically disable them - which is a possibility - but not
nice." mean? In our scenario, I assume that only the Form1-- the main GUI
form need to be disabled, yes? So we only need to disable Form1?

Third, currently, I can think of a third solution to you:
In Form1, we still use ShowDialog method to show our Form2, which blocks
both Form1 and custom details form.
Then in form2 code, we can find custom form reference and use ShowWindow
Win32 API to enable it and bring it to front. Code like this:
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool EnableWindow(IntPtr hWnd, bool enable);


private void button1_Click(object sender, System.EventArgs e)
{
Form1 f=this.Owner as Form1;
if(f!=null)
{
EnableWindow(f.cf.Handle, true);
f.cf.BringToFront();
}
}
This works well on my side. Anyway, it is your choice for taking any
solution into your project.

Hope it helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Dear Jeffrey,

Fantastic, - This is EXACTLY what I needed - Thank you so much!
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool EnableWindow(IntPtr hWnd, bool enable);


private void button1_Click(object sender, System.EventArgs e)
{
Form1 f=this.Owner as Form1;
if(f!=null)
{
EnableWindow(f.cf.Handle, true);
f.cf.BringToFront();
}
}

Cheers,

Giles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top