永不言弃,简单就好
每一个付出,都有回报,永远不放弃
随笔- 110  文章- 0  评论- 29 
博客园  首页  新随笔  联系  管理  订阅 订阅
使 TextBox1 的 UI 可以更新

Timer timer1;
int curNum;
System.Threading.Thread thread;


/// <summary>
/// 自动增加 (使 TextBox1 的 UI 可以更新)
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i <= 1000; i++)
    {
        this.textBox1.Text = i.ToString();
        this.Refresh(); //刷新控件
        Application.DoEvents(); //延时20毫秒, 一般情况下跟上面的语句一起使用
    }
}

/// <summary>
/// 时钟自动增加
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button2_Click(object sender, EventArgs e)
{
    curNum = 0;
    timer1 = new Timer();
    timer1.Tick += new EventHandler(timer1_Tick);  //增加一个timer1.Tick事件
    timer1.Interval = 1000;  //时间间隔,默认为毫秒 (到了这个时间就执行一个Tick事件)
    timer1.Enabled = true;  //设置计时器在运行
    timer1.Start();  //启动计时器
}
private void timer1_Tick(object sender, EventArgs e)
{
    if (curNum < 10)
    {
        curNum = curNum + 1;
        this.textBox1.Text = curNum.ToString();
    }
    else
    {
        timer1.Enabled = false;  //设置计时器不在运行
        timer1.Stop();  //停止计时器
        MessageBox.Show("计时结束", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

/// <summary>
/// 线程自动增加
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button3_Click(object sender, EventArgs e)
{
    //如果有两个或多个线程操作某一控件的状态,则可能会迫使该控件进入一种不一致的状态
    Control.CheckForIllegalCrossThreadCalls = false;  //访问控件的方法或属性之一的线程是不是创建该控件的线程(本例中为FALSE表示不是创建textBox1的线程也可以访问它)
    thread = new System.Threading.Thread(AutoAddNumber);           
    thread.Start();
}
private void AutoAddNumber()
{
    for (int i = 0; i <= 1000; i++)
    {
        this.textBox1.Text = i.ToString();
    }           
}

绿色通道:好文要顶关注我收藏该文与我联系
posted on 2008-11-24 23:37 嘎子 阅读(137) 评论(0) 编辑 收藏
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· 最想要的Entity Framework功能
· 专访Jeffrey Richter:Windows 8是微软的重中之重
· 《福布斯》:谷歌进军硬件产品 难撼动苹果地位
· 美国空军拟最多购买1.8万台iPad 2
· 分析称专利之争让谷歌苹果两败俱伤
» 更多新闻...
最新知识库文章:
· 高级编程语言的发展历程
· 如何学习一门新的编程语言?
· 学习不同编程语言的重要性
· 为什么我喜欢富于表达性的编程语言
· 计算机专业的女生为什么要学编程
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
<2008年11月>
日一二三四五六
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
昵称:嘎子
园龄:3年3个月
粉丝:0
关注:0

搜索

 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论
  • 我的标签

随笔分类

  • .NET学习笔记(4)
  • .NET源码(8)
  • UNIX、LINUX资料(28)
  • 其它(32)
  • 数据库(41)

随笔档案

  • 2011年11月 (1)
  • 2011年10月 (1)
  • 2011年9月 (2)
  • 2011年8月 (2)
  • 2011年7月 (1)
  • 2011年6月 (1)
  • 2011年5月 (2)
  • 2011年4月 (1)
  • 2011年1月 (1)
  • 2010年10月 (4)
  • 2010年9月 (2)
  • 2010年8月 (1)
  • 2010年7月 (1)
  • 2010年6月 (1)
  • 2010年5月 (4)
  • 2010年3月 (5)
  • 2010年2月 (5)
  • 2010年1月 (2)
  • 2009年12月 (4)
  • 2009年11月 (1)
  • 2009年10月 (6)
  • 2009年9月 (11)
  • 2009年8月 (2)
  • 2009年7月 (17)
  • 2009年6月 (2)
  • 2009年5月 (3)
  • 2009年4月 (4)
  • 2009年3月 (4)
  • 2009年2月 (2)
  • 2009年1月 (2)
  • 2008年12月 (7)
  • 2008年11月 (11)

好友Blog

  • eygle
  • 疯子
  • 浪曦视频在线

最新评论

阅读排行榜

评论排行榜

推荐排行榜

Copyright ©2012 嘎子