How to create a non-modal form but blocking?

You can solve this quite easily. Create and use a modal dialog but override the WndProc of the dialog and process the WM_MOUSEDOWN event. Check the position of the mouse down and if it is over the parent window but not over the dialog itself then simply dismiss the dialog.


Essentially you can't do this in a 'blocking' call easily. What you could do easily enough is to either pass the information required to perform the delete, or a delegate to perform the operation, to the form. When they click Ok you simply perform the operation. If they activate the parent form, then just close the child.


You want the user to be able to click the background window to cancel the dialog box? Add a handler to the background window so that when the user clicks on it you check to see if the non-modal window is displayed, if so close it.

Sounds easy, but you will need to be careful to handle every possible click on the background window and child windows. That sounds like a can of worms I wouldn't want to go down.

Perhaps instead you could detect if the non-modal dialog box loses focus and automatically close it. I can see this behavior making sense for a simple "confirm delete" dialog box, but as a user my first reaction is going to be to spam the ESC key to close the dialog box.


Another way of handling this is by manually enabling the parent form when calling ShowDialog, from here

[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hWnd, bool enable);

internal static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
    parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
    formToBeShown.ShowDialog(parent);

    return formToBeShown.DialogResult;
}

Just call the extension method from any parent form like this:

var f = new Form();
f.ShowDialogSpecial(this);
//blocks but parent window will be active.

Of course you need to handle the clicks on parent form to close child form.


You could do something like:

public void ShowMe() {
    Show();
    while (!_receivedDeactivateEvent)
        Application.DoEvents();
}

I'm not sure I'd recommend it, though -- I'm not sure how stable it would be, nor am I sure whether it would behave the way you want if you click the Delete button on the parent form while the 'dialog' is up (would it close the first dialog first, or leave it up? might be the latter, which could get messy).


Comments

  1. Caden

    • 2015/10/30

    You can solve this quite easily. Create and use a modal dialog but override the WndProc of the dialog and process the WM_MOUSEDOWN event. Check 

  2. Trace

    • 2021/4/11

    JotForm helps you create online forms, collect and download your data.

  3. Damian

    • 2017/8/18

    Should sound weird, but this is just for my hobby. I would want a (custom) messagebox to pop up with a YesNo buttons which should ideally block the code.

  4. Daxton

    • 2016/12/15

    The main feature of a modal dialog is that it is blocking. The main feature of the non-modal dialog is that it is non-blocking. You want blocking non-modal dialog box which seems like a contradiction of sorts. Popup menus automatically close when they lose focus and that seems very close to what you describe. – Brian Ensink Dec 20 '11 at 14:45

  5. Lucca

    • 2020/9/13

    A modal window blocks the user interface, and does not allow the user to interact with any other part Use setModal(false) to create a non-modal dialog.

  6. Ismael

    • 2015/1/12

    Show a modal form without blocking execution. balrom asked on 8/30/2006. Delphi. 17 Comments 1 Solution 3994 Views Last Modified: 4/16/2010. Hi experts, i have an urgent question. Can i do a showmodal call that return immediately the control to the caller, without waiting the closing of the modal form?

  7. Riva

    • 2015/11/5

    NET event handlers to create and display additional non-modal .NET forms (or ABL windows) after the WAIT-FOR statement blocks for events.

  8. Kabir

    • 2017/9/14

    As long as you do asynchronous operations during the time that the modal dialog is opened, you can do it as simply as shown below, assuming button1_Click() is the event handler for a button. private async void button1_Click (object sender, EventArgs e) {// create and display modal form Form2 modalForm = new Form2 (); BeginInvoke ((Action

  9. Justice

    • 2016/3/16

    Is there a way to open a modal dialog and only have it block the form that the dialog not block any threads at all by simply running Form D's ShowDialog 

  10. Mariani

    • 2020/12/31

    Create your own message box and in the in the Properties Window for the UserForm set the ShowModal to False. In the Activate event insert the code below to show your own MsgBox for 5 seconds. Private Sub UserForm_Activate()

  11. Logan

    • 2020/9/23

    Windows, including standards for modal and non-modal windows Module properties establish an overall framework for the look and feel of each form.

  12. Alaric

    • 2019/11/25

    This is a very simple modal and does not cover making the modal accessible to keyboard users. To make your modal more accessible, check out this article. Next, we will use this component to create our confirmation modal. Creating the Confirmation Modal. In this section, we will separate the React and non-react route blocking implementations.

  13. Derrick

    • 2019/8/12

    When a non-modal dialog is active the user can continue viewing and are used for non-workflow related tasks, it is likely a user will start ignoring or 

  14. Taylor

    • 2020/3/9

    To make it stay always on top, you need to make it system modal. This will actually make it sit on top of ALL applications your User is running but, hey, nothing is perfect .. This will actually make it sit on top of ALL applications your User is running but, hey, nothing is perfect ..

  15. Keenan

    • 2018/9/18

    The following example displays a form as a modal dialog box and evaluates the This example requires that a Form named testDialog is created and that it 

  16. Troy

    • 2016/5/10

    what I wanted to create was a utility or other form that could stay open. while the main application is running so that the Users could refer to. the information on it. I thought I could do this by showing a form but. in a non-modal state so it would be independant to the main processing. of the application.

  17. Kason

    • 2020/1/5

    Qt::ApplicationModal The window is modal to the application and blocks input to all windows. Application-level modal dialog box that will block all the windows 

  18. Vincent

    • 2018/2/17

    JotForm helps you create online forms, collect and download your data.

  19. Ace

    • 2015/6/16

    Form create a non-modal form with two buttons that looks behaves like a Prepares and displays the specified form in a wait (blocking) state until the .

Comments are closed.

Recent Posts