MessageBox in c#

99封情书 提交于 2019-12-10 14:53:43

问题


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...


回答1:


You will have to create your own form, make it modal, change the z-order to make it always on top, and capture all keystrokes and mouse clicks.

Always on top: http://www.codeguru.com/cpp/w-d/dislog/article.php/c1857




回答2:


Take a look at this article

MessageBox.Show Examples in Windows Forms C#

Edit:

You can also use the topmost property of a form to make it on top of all windows in a given application.

How to: Keep a Windows Form on Top

To display a form as a modal dialog call the ShowDialog method.

Form frmAbout = new Form();
frmAbout.ShowDialog();



回答3:


If standard implementation of MessageBox doesn't do what you need, you will have to create your own form and use ShowDialog() method.




回答4:


I think you are looking for a System Modal dialog.

Previous thread:

How to display a message box in C# as system modal?




回答5:


It sounds like the messagebox is being displayed on another thread. You need to make sure that you call MessageBox.Show on the main UI thread. Below is a code snippet that illustrates a way to achieve this:

public class FooForm: Form
{

   //This is just a button click handler that calls ShowMessage from another thread.
   private void ButtonShowMessage_Click(object sender,EventArgs e)
   {
     //Use this to see that you can't interact with FooForm before closing the messagebox.
     ThreadPool.QueueUserWorkItem(delegate{ ShowMessage("Hello World!");});

     //Use this (uncomment) to see that you can interact with FooForm even though there is a messagebox.
     //ThreadPool.QueueUserWorkItem(delegate{ MessageBox.Show("Hello World!");});
   }

   private void ShowMessage(string message)
   {
     if( InvokeRequire)
     {
       BeginInvoke(new MethodInvoker( () => MessageBox.Show(message))); 
     }
     else
     {
       MessageBox.Show(message);
     }
   }    
} 

I am assuming that you don't have a scenario where you have multiple UI threads and when one of them pops a messagebox, you want that messagebox to be modal for the entire UI. That's a more complicated scenario.



来源:https://stackoverflow.com/questions/1391952/messagebox-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!