本文介绍了在按钮单击时在C#更新UI中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我点击了一个按钮,运行了很多代码。我希望在代码运行时更新我的​​UI。它从按钮单击返回后更新。我可以在按钮点击时显示消息吗? 老家伙。I have a button click that runs a lot of code. I would like to updata my UI while that code is running. It updates after the return from the button click. Can I display messages while in the button click?Old guy.推荐答案 public Form1(){ InitializeComponent(); backWkr.WorkerReportsProgress = true; backWkr.ProgressChanged += backWkr_ProgressChanged; backWkr.DoWork += backWkr_DoWork;}BackgroundWorker backWkr = new BackgroundWorker();private void button1_Click(object sender, EventArgs e){ backWkr.RunWorkerAsync();}void backWkr_DoWork(object sender, DoWorkEventArgs e){ // the code that take tsome time to execute. int percentProgress = 0; do { percentProgress++; // simulate some long operation... Thread.Sleep(100); object whatEverIsNeeded = "blah blah blah"; backWkr.ReportProgress(percentProgress, whatEverIsNeeded); } while (percentProgress < 100 );}void backWkr_ProgressChanged(object sender, ProgressChangedEventArgs e){ // get the data... object whatEverStatepassed = e.UserState; int percent = e.ProgressPercentage; // update ui, do something... eyc... this.BackColor = Color.FromArgb(this.BackColor.R, this.BackColor.G, percent);} Valery。Valery. 这篇关于在按钮单击时在C#更新UI中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 19:03