.net – Set a window as modal after it is visible – Education Career Blog

Is it possible to toggle a window between modal and non modal after it is visible?

This is what I need… call a method passing the window handle and set if I want it to be modal or not.

If possible, any sample will help a lot!

.net, c++ etc

thanks!

EDIT:
Why?
In this case, I’m using TWAIN to scan images, and “some” data sources doesn’t show its dialog as modal, even if I set it to do so, and if it’s not modal, the user can mess with the background window…

,

Window modality is primarily driven by window handle ownership. Disabling the parent window and setting up a local message loop are secondary artifacts.

Setting the modal window handle’s owner to the application main window creates the behavior that clicking on the main window sends focus to the modal child window. Without the owner set correctly, the main window can be focused while the “modal” window is showing, which is not modal behavior.

I seem to vaguely recall that you will also need to disable the owner window to prevent mouse clicks from clicking button or selecting menus on the owner window while the modal window is active.

Modal windows are usually also associated with a local message loop, but this has little to do with the modal UI behavior of the window and everything to do with the modal state of the executing code. Running a local message loop inside Form.ShowDialog() makes the function call synchronous with the modal window lifetime – it prevents the function from returning to the caller until the modal window is closed.

Setting the owner of a window handle can only be one when the child window handle is created. The owner cannot be changed after the child window handle exists. See MSDN on Owned Windows

So you cannot simply flip a switch and make a non-modal window modal.

Your best bet is to destroy the window handle and recreate it with the desired owner. This can be done with little or no screen flicker, but the larger issue is preserving any local state (text in edit boxes, checkbox states etc) of child controls on the window. Destroying the window will lose any state information in the child windows, so you will need to be sure to extract all the state first, then recreate the window handle, then restore the child window data afterwards.

,

You can’t do this with the standard WinAPI, and I’m fairly sure you can’t do this with .NET framework. You could fake it by creating your own message pump routine that is called when switching from non-modal to modal.
However, it sounds like this is an answer to the wrong question. Can you clarify why you need it to switch between modal and non-modal?

,

With the .NET Framework, I know of no way to achieve such behaviour. The way you make a form non-modal or modal in .NET is by showing it through either Form.Show() or Form.ShowDialog() methods respectively.

I don’t get the fact that you need to show it non-modal, then make it modal afterwards. What is it are you trying to accomplish? Perhaps further details could help us help you or find another way out which will drive you to a better solution, I hope.

,

I wondered why the OP asked this question, but it got me to thinking. I have something that may be a start toward finding an answer.

I created two forms: Form1 and Form2. Form1 has a button; Form2 has a textbox.

I tried the following code for Form1:

    private void button_Click(object sender, EventArgs e)
    {
        form2.Show(this);            
        System.Threading.Thread thread = new System.Threading.Thread(showModal);
        thread.Start();
    }        

    private void showModal()
    {
        System.Threading.Thread.Sleep(2000);
        form2.Visible = false;
        form2.ShowDialog(this);
    }

When the button on Form1 is clicked, it shows Form2 as a non-modal box; it waits two seconds; then it hides Form2 and then shows it as a modal box. If I type in Form2’s textbox before it switches, the text is preserved. However, when I click on the x in the upper right-hand corner of Form2, I can’t close it. Since it is modal, I have to kill the program thru Task Manager to stop it.

Leave a Comment