如何在C#中显示没有任何按钮的消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11502338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display message box without any buttons in C#
提问by user1520860
I try to show a MESSAGE to the user while an operation is executed. The MESSAGE won't show any button. Just a MESSAGE (text) and maybe a background image.
我尝试在执行操作时向用户显示 MESSAGE。MESSAGE 不会显示任何按钮。只是一条消息(文本),也许还有一个背景图片。
The problem is the following:
问题如下:
- MessageBox does not seem to be the good control (because of button and it blocks the running process).
- Form.ShowDialog() also blocks the running process. I don't know what to do.
- I want to show the message, run my process, and dispose the message when the process is done.
- MessageBox 似乎不是很好的控件(因为按钮并阻止了正在运行的进程)。
- Form.ShowDialog() 也会阻塞正在运行的进程。我不知道该怎么办。
- 我想显示消息,运行我的进程,并在进程完成后处理该消息。
How to achieve this in C#?
如何实现这一点C#?
回答by burning_LEGION
create custom form, and write own behavior
创建自定义表单,并编写自己的行为
回答by RobIII
Create a simple form with the message (or expose a public property to be able to change the message, or a constructor with message parameter to pass it in) and show the form using this Show overload. Then disable the (entire) original (owner) form (or just disable the controls you don't want accesible).
使用消息创建一个简单的表单(或公开一个公共属性以能够更改消息,或使用带有消息参数的构造函数将其传入)并使用此 Show 重载显示该表单。然后禁用(整个)原始(所有者)表单(或仅禁用您不想访问的控件)。
So, in your "main" form do this:
因此,在您的“主要”表单中执行以下操作:
Form f = new MessageForm();
f.Show(this); //Make sure we're the owner
this.Enabled = false; //Disable ourselves
//Do processing here
this.Enabled = true; //We're done, enable ourselves
f.Close(); //Dispose message form
Also, consider using a BackgroundWorker.
另外,请考虑使用BackgroundWorker。
回答by Davos555
Create a custom form set it to minimal styles etc. Make sure it knows when the process is complete, say by passing in a reference to the process and checking it every now and then.
创建一个自定义表单,将其设置为最小样式等。确保它知道流程何时完成,比如传递对流程的引用并时不时地检查它。
回答by Stuart
One way you could do it is create a worker class that raises an event when it is finished.
一种方法是创建一个工作类,在它完成时引发一个事件。
execute the worker class in a new thead so it runs it the backgroud.
在新的 thead 中执行 worker 类,以便它在后台运行它。
create the modal form with a method to close it as the event handler for the "finished" event. This way the ui still works and responds and the form will close once the worker thread is finished.
使用一种方法创建模态表单以将其关闭为“finished”事件的事件处理程序。这样,ui 仍然可以工作并做出响应,一旦工作线程完成,表单就会关闭。

