wpf中textbox控件动态显示赋予的内容

t415088620 2013-05-06 10:23:19
当我点击一个button时候,执行下面的循环,并执行4次,也就是说,第一次循环,ret=-32,textbox应该显示“请放入手指!”,然后,第2次循环,ret=-33,textbox显示“请拿开手指!”,。。。第4次ret=-33,textbox显示“请拿开手指!”并跳出循环。
试了好多种方法,可是最后都是在while循环完毕后,才显示textbox的内容,也就是第4次的结果。
请各位大侠帮帮忙!

while(true)
{
if (ret == -32)
textBox1.Text = "请放入手指!";
else if (ret == -33)
textBox1.Text = "请拿开手指!";
}
...全文
715 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
t415088620 2013-05-06
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace test
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//public delegate void deleTest();
private TextBox tb;
public MainWindow()
{
InitializeComponent();
//FileType = "nihao";
this.tb = textBox1;
}
private void button1_Click(object sender, RoutedEventArgs e)
{

int a=4;
// FileType = "hello";
while (a >= 0)
{
if (a == 4)
{
Thread thread1 = new Thread(new ThreadStart(this.ForFun2));
thread1.Start();
}
if (a == 3)
{
Thread thread1 = new Thread(new ThreadStart(this.ForFun3));
thread1.Start();
}
if (a == 2)
//textBox1.Text = "你在哪";
if (a == 1)
this.tb.Text = "大爷成功了";
a--;
}
}

private void ForFun2()
{
this.tb.Text = "原来如此!";
Thread.Sleep(3000);
}
private void ForFun3()
{
this.tb.Text = "大爷郁闷!";
Thread.Sleep(3000);
}
}
}


这个是线程更新的方法吧?我运行总会出现下面这样的错误:

引用 1 楼 findcaiyzh 的回复:
程序一直在执行,知道循环结束后UI才刷新,当然只能看到最后一条了。

一般向进度条等,需要使用线程更新文字或者图形。

宝_爸 2013-05-06
  • 打赏
  • 举报
回复
程序一直在执行,知道循环结束后UI才刷新,当然只能看到最后一条了。 一般向进度条等,需要使用线程更新文字或者图形。
t415088620 2013-05-06
  • 打赏
  • 举报
回复
引用 3 楼 findcaiyzh 的回复:
.net中禁止其他线程直接更新UI,要通过delegate,参考代码:

public delegate void UpdateTextCallback(string message);

private void TestThread()
{
    for (int i = 0; i <= 1000000000; i++)
    {
        Thread.Sleep(1000);                
        textBox1.Dispatcher.Invoke(
            new UpdateTextCallback(this.UpdateText),
            new object[] { i.ToString() }
        );
    }
}
private void UpdateText(string message)
{
    richTextBox1.AppendText(message + "\n");
}

private void button1_Click(object sender, RoutedEventArgs e)
{
   Thread test = new Thread(new ThreadStart(TestThread));
   test.Start();
}
代码来自: http://stackoverflow.com/questions/4253088/c-updating-gui-wpf-using-a-different-thread
非常感谢,问题解决,受益匪浅!
宝_爸 2013-05-06
  • 打赏
  • 举报
回复
.net中禁止其他线程直接更新UI,要通过delegate,参考代码:

public delegate void UpdateTextCallback(string message);

private void TestThread()
{
    for (int i = 0; i <= 1000000000; i++)
    {
        Thread.Sleep(1000);                
        textBox1.Dispatcher.Invoke(
            new UpdateTextCallback(this.UpdateText),
            new object[] { i.ToString() }
        );
    }
}
private void UpdateText(string message)
{
    richTextBox1.AppendText(message + "\n");
}

private void button1_Click(object sender, RoutedEventArgs e)
{
   Thread test = new Thread(new ThreadStart(TestThread));
   test.Start();
}
代码来自: http://stackoverflow.com/questions/4253088/c-updating-gui-wpf-using-a-different-thread

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧