Button.Visible = true;(Button.Visible = true; fails to set the button to visible when activated within a function)

我一直无法找到其他人有这个相同的问题,所以希望有人可能有一些想法或能够指出我另一个答案。

当按下窗体上的按钮来运行某个功能时,应该可以看到另一个按钮。 但是,即使该函数是该函数中的第一个,该按钮也不会显示。 该函数中的所有其他代码都可以正常工作。

这是代码:

private void trackbar_Change(object sender, EventArgs e) { button.Visible = true; progressbar.Visible = true; ... progressbar.Visible = false; button.Visible = false; }

进度条显示并且工作正常,函数中的所有其他代码也可以正常工作,但按钮从不显示。

如果我删除button.Visible = false; 从函数结尾开始,那么按钮会出现,但只有在所有其他代码执行完毕后才会显示。 喜欢这个:

private void trackbar_Change(object sender, EventArgs e) { button.Visible = true; progressbar.Visible = true; ... progressbar.Visible = false; //button.Visible = false; }

注释掉该行会导致按钮显示。 现在,如果我在按钮行后面添加一个消息框,那么它也可以工作。

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; }

在按钮行之后添加消息框会使按钮在正确的时间出现。

有没有人有任何想法,为什么这个按钮是这样的行为?

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:

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?

最满意答案

听起来像GUI线程很忙。 尝试通过调用Application.DoEvents()强制屏幕更新,例如:

button.Visible = true; progressbar.Visible = true; Application.DoEvents();

DoEvents()将强制处理消息队列中的所有消息。

更好的解决方案是移动主UI线程的长时间运行的线程。 使用BackgroundWorker完成任务。

它将使整个表单更具响应性。 例如,您将能够与表单进行交互,并且它不会变为“白色”。 在主UI线程上实现BackgroundWorker很简单,也是必须的,用于长时间运行的进程,

It sounds like the GUI thread is busy. Try forcing the screen update by calling Application.DoEvents(), for example:

button.Visible = true; progressbar.Visible = true; Application.DoEvents();

DoEvents() will force all the message in the message queue to be processed.

A better solution would be to move the long running thread of main UI thread. Use a BackgroundWorker for the task.

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,

更多推荐