简体   繁体   English

如何在C#中关闭MessageBox后强制按钮,TextBoxes在窗体上重绘

[英]How to force Buttons, TextBoxes to repaint on form after a MessageBox closes in C#

I have a form with buttons, textBoxes and userControls on it. 我有一个带有按钮,textBoxes和userControls的表单。 When a button is clicked, it calls a method in another class. 单击按钮时,它会调用另一个类中的方法。 In this class a Message box opens. 在此类中,将打开一个消息框。 When user clicks OK, the messageBox closes, and the class method proceeds for 10 or so seconds before ending. 当用户单击“确定”时,messageBox将关闭,并且类方法将在结束前持续10秒左右。 During this 10 seconds, what ever textBox, or button the message box was over still displays the messageBox (They are not getting repainted). 在这10秒内,消息框所在的textBox或按钮仍然显示messageBox(它们没有重新绘制)。

The question is how can I force everything to repaint on the form. 问题是我如何强制在表格上重绘一切。 The problem is the class that has the messageBox does not have any knowledge of the form that called it 问题是具有messageBox的类不具有调用它的表单的任何知识

Frank 坦率

You can try with 你可以试试

Application.DoEvents()

right after the message box closes. 消息框关闭后。 However - unless you execute the method that you are calling in that other class in a background thread - your UI will not be responsive for 10 seconds. 但是 - 除非您在后台线程中执行您在其他类中调用的方法 - 否则您的UI将无法响应10秒。

Is that 10 seconds spent doing work entirely in the UI? 完全在UI中完成工作需要10秒吗? If not, it should really be done on a separate thread. 如果没有,它应该在一个单独的线程上完成。 Even if you refresh the form itself, you're still going to have a unresponsive UI for 10 seconds, which isn't ideal. 即使你自己刷新表单,你仍然会在10秒内没有响应的UI,这是不理想的。

See my threading tutorial for an example of executing code in a different thread and calling back to the UI thread. 有关在不同线程中执行代码并回调UI线程的示例,请参阅我的线程教程 Be aware that this is the "old" way of doing things - BackgroundWorker makes things somewhat simpler. 请注意,这是“旧”的做事方式 - BackgroundWorker使事情变得更简单。

The problem here is that you have processing that is occurring on the UI thread and blocking the paint message. 这里的问题是您在UI线程上进行了处理并阻止了绘制消息。 Calling Refresh or Invalidate isn't going to fix this, as you are still blocking on the thread that would perform those operations. 调用Refresh或Invalidate不会解决这个问题,因为您仍在阻塞执行这些操作的线程。

Rather, you should take that processing and move it to another thread, and then update your UI thread (appropriately, through the Invoke method, more likely than not) as you perform the work on the background thread. 相反,您应该执行该处理并将其移动到另一个线程,然后在后台线程上执行工作时更新您的UI线程(适当地,通过Invoke方法,更有可能)。

要强制重绘所有内容,可以在主窗体上调用Invalidate()。

Your problem is that you are doing your work on the UI thread. 您的问题是您正在UI线程上工作。 The UI will not get repainted until your method returns which will allow the Windows message loop to continue. 在方法返回之前,UI不会重新绘制,这将允许Windows消息循环继续。

The solution is to run your work method on a different thread. 解决方案是在不同的线程上运行您的工作方法。 Perhaps the BackgroundWorker class will solve your problem nicely. 也许BackgroundWorker类可以很好地解决您的问题。

Edit: See this article for an in depth explanation: 编辑:有关深入解释,请参阅此文章:
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表单加载后C#MessageBox弹出窗口 - C# MessageBox popup AFTER form load C# windows 表单,如何将单选按钮/复选框值输入到多个文本框中? - C# windows form, how to have radio buttons/checkboxes values input into multiple textboxes? 如何在文本框中输入一些文本之前禁用c#窗口表单应用程序中的按钮 - How to disable buttons in c# window form application before entering some text in textboxes 单击“消息框”按钮之后,但此消息框关闭之前该怎么办? - How to do something after clicking on MessageBox button, but before this MessageBox closes? 在C#中加载表单后,如何使消息框立即显示 - How would I make a messagebox appear right after the form loads in c# 单击“下一步”时,如何用一个消息框验证多个文本框? 视觉工作室(C#Winforms) - how to validate multiple TextBoxes with a MessageBox when clicked on “next”? visual studio(c# Winforms) C#表单数据集在子表​​单关闭后获取数据 - C# form dataset get data after child form closes 在C#中的MessageBox之后提交 - Commit after MessageBox in C# 如何隐藏文本框,标签和按钮C#WPF - How to hide textboxes, labels and buttons C# WPF C#从表单获取信息(就像MessageBox按钮一样,仅由我创建的表单) - C# Get information from a form (Like MessageBox buttons just with a form made by me)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM