gab gab - 2 months ago 4x
C# Question

Is there a way to disallow clicking outside of winforms (c#) until user presses close buton?

I'm currently building a winforms application. The idea is to force the user to press Close or Cancel button and will not remove focus from the winform or be overlapped by the (whatsoever clicked item/apps/) if the user clicks outside of its winform border/size.

* Update *

I am not trying to create a ransomware. We are trying to copy the behavior of bootstrap modal that we created in our web app that the only way to close the modal is by pressing our "x" button. We are focusing on consistency. Sorry


I hope that you are talking about a modal dialog - one that blocks interaction with the rest of your application, rather than all applications running on the PC.

Assuming this is the case, do the following when opening your dialog:

using (var frm = new MyCustomForm())
    //This is a blocking call - the execution stops here until that
    //form is closed and you can then examine the result
    var result = frm.ShowDialog();  

See for more information on ShowDialog method.