在Windows窗体应用程序C#中运行时更改按钮属性不反映在UI中 [英] Changing Button Property not reflecting in UI during Runtime in Windows Form Application C#

查看:361
本文介绍了在Windows窗体应用程序C#中运行时更改按钮属性不反映在UI中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以可视方式交互式更改UI中的按钮属性。



我的Windows窗体应用程序中有一个唯一的按钮,这是我点击按钮的代码



  private   void  button1_Click( object  sender,EventArgs e)
{

button1.BackColor = Color.Red;
button1.Text = 1;
Thread.Sleep( 2000 );
button1.BackColor = Color.Purple;
button1.Text = 2;

Thread.Sleep( 2000 );
button1.BackColor = Color.White;
button1.Text = 3;
}







但是,在线程休眠操作期间,该按钮处于默认状态颜色,只有在整个代码运行完毕后才会变为最终颜色。我在这做错了什么?这是线程的工作方式吗?



当我检查保持调试器时,属性设置正确,但它的更改只有在提交时才会提交给UI整个代码完成执行。有没有办法动态更新UI。



我的目标是多次更改按钮的颜色和大小,这些更改应该反映在UI

解决方案

简单:根本不使用Sleep。



睡眠暂停当前线程,所以它在时间段到期之前绝对没有任何作用。由于它是UI线程(或者你不会在Click事件处理程序中),这意味着更新显示的线程被挂起 - 因此在Click even处理程序结束之前显示将不会更新 - 这是在你之后再次更改它,并再次暂停它!



如果你真的需要这样做,那么设置一个Timer,间隔为(比方说)100(或者1/10秒)并将更改为紫色计数器设置为20.在Timer Tick事件处理程序中,检查计数器,如果它是非零,则将其减1。当它达到零时,更改按钮属性。 (并设置一个更改为白色计数器 - 您在同一个Tick事件处理程序中以相同的方式处理)。



听起来很复杂,但它非常简单。


每次更改颜色后,应在 Thread.Sleeep之前调用 button1.Refresh() () <!/ BLOCKQUOTE>

I am trying to interactively change button properties in the UI visually.

I have a sole button in my Windows Forms application and this is my code for the button click

private void button1_Click(object sender, EventArgs e)
        {

            button1.BackColor = Color.Red;
            button1.Text = "1";
            Thread.Sleep(2000);
            button1.BackColor = Color.Purple;
            button1.Text = "2";

            Thread.Sleep(2000);
            button1.BackColor = Color.White;
            button1.Text = "3";
        }




However, during the Thread Sleep operations, the button is in its default color and it changes to its final color only when the whole code has finished running. What am I doing wrong here ? Is this the way that threads work ?

When i checked keeping debugger, the property is getting set correctly, but its like the changes are submitted to the UI only when the whole code finishes executing. Is there a way to update the UI dynamically.

My aim is to change the color and size of the button multiple times and those changes should get reflected in the UI

解决方案

Simple: don't use Sleep at all.

Sleep suspends the current thread, so it does absolutely nothing until the time period has expired. Since it is the UI thread (or you wouldn't be in the Click event handler) that means that the thread that updates the display is suspended - so the display won't get updated until the Click even handler ends - which is after you have changed it again, and suspended it again!

If you really need to do this, then set up a Timer, with an Interval of (say) 100 (or 1/10th of a second) and set a "change to Purple" counter to 20. In the Timer Tick event handler, check the counter, and if it is non-zero reduce it by one. When it reaches zero, change the button properties. (And set a "change to White" counter - which you handle in the same way in the same Tick event handler).

Sounds complex, but it's pretty simple realy.


You should invoke button1.Refresh() after each change of color, but before Thread.Sleeep()!


这篇关于在Windows窗体应用程序C#中运行时更改按钮属性不反映在UI中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆