|
I want to show messagebox to the user, such that the user cannot refuse to confirm the message box. User should not be allowed to do anything else in the screen until he confirms the messagebox. This is a windows based c# application. The main thing is, even if i use windows message box. Some times it is hiding behind some screen. But for my case, i want message box to be on top most whenever it appears. I am using some other third party applications, which over rides my message box. I want to overcome this. How to do this... | | karthik.sr Thursday, September 10, 2009 2:05 AM | First of all, let me state that this answer is bad design practice . As it has been said, if you need to block the user from interacting with other forms besides the one calling the message box, chances are high there is something wrong on your application design (I'll add some tips on this topic after the answer). To achieve what you are asking for, the simplest approach I can suggest is this:
- Add a static/shared field on your program class, of type Form, and with a default value of null (C#) or Nothing (VB). On C#, you'd declare such a member as "internal static Form BlockingForm = null;". You can use any name you please; I'm using "BlockingForm" for code examples to follow, so adapt them accordingly if you use a different name.
- Just before showing the blocking dialog form, save a reference to it on the field created in the previous step. After the dialog is dismissed, reset the field back to null. Example:
Program.BlockingForm = MyDialog; MyDialog.ShowDialog(); Program.BlockingForm = null;
- Finally, add an Activated event handler for all forms you want to have blocked. On the event, check if Program.Blocking form is non-null, and activate it:
private void MyForm_Activated(object sender, EventArgs e) { if(Program.BlockingForm==null) return; Program.BlockingForm.Activate(); }
And that's it. Whenever the user tries to reach any other form, it will send focus back to your "supermodal" dialog. Oh, and BTW, you might want to have the dialog's TopMost property set to true. This is an answer to what you asked , but is it really what you need ? Most modern applications follow a quite similar structure: a main MDI parent form with MDI children forms within, and some (modal) dialogs for different things. With such a setup, blocking only the topmost dialog or the MDI parent (if there is no dialog currently open) is enough to keep the entire application blocked. In addition, this is the format users will be most used to, so it's worth sticking to it even if just to make the program easier to use. Some applications (for example, MS Office XP and onwards, depending on your setup) will use multiple independent windows (in the case of Office, that's one for each separate document), but they are separate enough so that a blocking dialog on one of the windows doesn't interfere with the others. What in your application is separate enough to deserve an independent window (and another button in the taskbar) but still dependent enough to require blocking when the main window is also blocked? If you can answer this question (no need to answer here, just answer it to yourself), then go ahead and use the trick I posted above or any other you happen to find; if you can't answer it you should consider re-organizing your forms to be more user-friendly. Regards, Herenvardö - Proposed As Answer byGeert van Horrik Wednesday, September 16, 2009 5:18 AM
- Marked As Answer byKira QianMSFT, ModeratorWednesday, September 16, 2009 9:24 AM
-
| | herenvardo Thursday, September 10, 2009 5:45 PM | A messageblock will only block the window/dialog/form that called the code. You should give more information about what you want to achieve, because there are too many opportunities to solve this problem (and maybe the best one is to reconsider your application design).
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a way to deploy your updates to all your clients? Try Updater! | | Geert van Horrik Thursday, September 10, 2009 5:19 AM | You cant do that easily. Main reason is the messagebox blocks only your applications calling form. What if you created an application that uppon launch displays such a messagebox but without any of the closing buttons. And you cant access anything on your screen?? Obviously thats why you cant do such a thing (easily).
In the common practice of designing applications you wouldnt need something like that anyway. You should 1st try to find why you would need such a thing, and then try to fix thesource.
VB.NET to C# http://www.developerfusion.com/tools/convert/vb-to-csharp/ | | Se3ker385 Thursday, September 10, 2009 5:49 AM | First of all, let me state that this answer is bad design practice . As it has been said, if you need to block the user from interacting with other forms besides the one calling the message box, chances are high there is something wrong on your application design (I'll add some tips on this topic after the answer). To achieve what you are asking for, the simplest approach I can suggest is this:
- Add a static/shared field on your program class, of type Form, and with a default value of null (C#) or Nothing (VB). On C#, you'd declare such a member as "internal static Form BlockingForm = null;". You can use any name you please; I'm using "BlockingForm" for code examples to follow, so adapt them accordingly if you use a different name.
- Just before showing the blocking dialog form, save a reference to it on the field created in the previous step. After the dialog is dismissed, reset the field back to null. Example:
Program.BlockingForm = MyDialog; MyDialog.ShowDialog(); Program.BlockingForm = null;
- Finally, add an Activated event handler for all forms you want to have blocked. On the event, check if Program.Blocking form is non-null, and activate it:
private void MyForm_Activated(object sender, EventArgs e) { if(Program.BlockingForm==null) return; Program.BlockingForm.Activate(); }
And that's it. Whenever the user tries to reach any other form, it will send focus back to your "supermodal" dialog. Oh, and BTW, you might want to have the dialog's TopMost property set to true. This is an answer to what you asked , but is it really what you need ? Most modern applications follow a quite similar structure: a main MDI parent form with MDI children forms within, and some (modal) dialogs for different things. With such a setup, blocking only the topmost dialog or the MDI parent (if there is no dialog currently open) is enough to keep the entire application blocked. In addition, this is the format users will be most used to, so it's worth sticking to it even if just to make the program easier to use. Some applications (for example, MS Office XP and onwards, depending on your setup) will use multiple independent windows (in the case of Office, that's one for each separate document), but they are separate enough so that a blocking dialog on one of the windows doesn't interfere with the others. What in your application is separate enough to deserve an independent window (and another button in the taskbar) but still dependent enough to require blocking when the main window is also blocked? If you can answer this question (no need to answer here, just answer it to yourself), then go ahead and use the trick I posted above or any other you happen to find; if you can't answer it you should consider re-organizing your forms to be more user-friendly. Regards, Herenvardö - Proposed As Answer byGeert van Horrik Wednesday, September 16, 2009 5:18 AM
- Marked As Answer byKira QianMSFT, ModeratorWednesday, September 16, 2009 9:24 AM
-
| | herenvardo Thursday, September 10, 2009 5:45 PM |
|