C# Button.Visible = true; 在功能内激活时无法将按钮设置为可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9441084/
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
Button.Visible = true; fails to set the button to visible when activated within a function
提问by blue
I haven't been able to find anyone else having this same problem, so hopefully someone might have some ideas or be able to point me to another answer.
我找不到其他人有同样的问题,所以希望有人可能有一些想法或能够为我指出另一个答案。
When a function is run by pressing a button on the form, another button should become visible. However, the button never shows up even though it is the first thing in the function. All of the other code in the function works perfectly.
当通过按下窗体上的一个按钮来运行一个功能时,另一个按钮应该变得可见。但是,即使按钮是功能中的第一件事,它也永远不会出现。该函数中的所有其他代码都可以完美运行。
Here is the code:
这是代码:
private void trackbar_Change(object sender, EventArgs e)
{
button.Visible = true;
progressbar.Visible = true;
...
progressbar.Visible = false;
button.Visible = false;
}
The progress bar shows up and works fine, and all other code in the function works fine as well, but the button never shows up at all.
进度条显示并工作正常,函数中的所有其他代码也工作正常,但按钮根本不显示。
If I remove button.Visible = false;from the end of the function, then the button DOES show up, but only after all other code has executed. Like this:
如果我button.Visible = false;从函数的末尾删除,那么按钮会出现,但只有在所有其他代码都执行之后。像这样:
private void trackbar_Change(object sender, EventArgs e)
{
button.Visible = true;
progressbar.Visible = true;
...
progressbar.Visible = false;
//button.Visible = false;
}
Commenting out that line causes the button to show up. Now if I add a message box after the button line, then it also works.
注释掉该行会导致按钮出现。现在,如果我在按钮行之后添加一个消息框,那么它也可以工作。
private void trackbar_Change(object sender, EventArgs e)
{
button.Visible = true;
MessageBox.Show("Button should be visible now");
progressbar.Visible = true;
...
progressbar.Visible = false;
button.Visible = false;
}
Adding the message box after the button line caused the button to show up at the right time.
在按钮行之后添加消息框会导致按钮在正确的时间显示。
Does anyone have any ideas why this button is behaving this way?
有没有人知道为什么这个按钮会这样?
采纳答案by Philip Fourie
It sounds like the GUI thread is busy. Try forcing the screen update by calling Application.DoEvents(), for example:
听起来 GUI 线程很忙。尝试通过调用Application.DoEvents()强制屏幕更新,例如:
button.Visible = true;
progressbar.Visible = true;
Application.DoEvents();
DoEvents()will force all the message in the message queue to be processed.
DoEvents()将强制处理消息队列中的所有消息。
A better solutionwould be to move the long running thread of main UI thread. Use a BackgroundWorkerfor the task.
一个更好的解决方案将是移动主UI线程的长时间运行的线程。为任务使用BackgroundWorker。
It will make the form more responsive overall. For example you would be able to interact with the form and it won't turn 'white'. Implementing the BackgroundWorker is simple and a must for long running processes on the main UI thread,
它将使表单整体更具响应性。例如,您将能够与表单交互,并且它不会变成“白色”。实现 BackgroundWorker 很简单,对于在主 UI 线程上长时间运行的进程来说,这是必须的,
回答by Mike Perrenoud
The issue is that you're executing a long running process on the UI thread and so setting the button to visible won't happen until the thread is free. However, by the time the thread is free you have set visible to false.
问题是您正在 UI 线程上执行长时间运行的进程,因此在线程空闲之前不会将按钮设置为可见。但是,当线程空闲时,您已将可见设置为 false。
The best way to do this is to execute the long running process on a background worker so that the UI thread isn't blocked. And the reason the progress bar works is because it runs on a different thread.
最好的方法是在后台工作者上执行长时间运行的进程,这样 UI 线程就不会被阻塞。进度条工作的原因是因为它在不同的线程上运行。
There is one other way -- but probably less correct -- and that's to issue a Refresh on the form after setting visible to true.
还有另一种方式——但可能不太正确——那就是在将visible 设置为true 后在表单上发出Refresh。
Let me know if you need help with the BackgroundWorker but it's pretty straight forward.
如果您需要有关 BackgroundWorker 的帮助,请告诉我,但这非常简单。
回答by Mayur Borad
When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.
当您运行 Windows 窗体时,它会创建新窗体,然后等待事件处理。每次表单处理一个事件时,它都会处理与该事件关联的所有代码。所有其他事件在队列中等待。当您的代码处理事件时,您的应用程序没有响应。例如,如果将另一个窗口拖到顶部,则该窗口不会重新绘制。
If you call DoEvents in your code, your application can handle the other events.
Use Application.DoEvents();
如果您在代码中调用 DoEvents,您的应用程序可以处理其他事件。用Application.DoEvents();

