windowsform中的控件调用--2

昨天百度到这篇文章感觉很不错(给一个我的博客的连接吧~~~http://blog.youkuaiyun.com/deandingding/article/details/39742985)今天自己在这儿玩着玩着,又玩出了一个关于这个问题的新方法,而且写起来也很简单。。。。也许很多弊端,但是目前我还不知道,如果有大牛能看出来,感激不敬!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public delegate void Test();
       
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Thread tr = new Thread(CrossThread);
            Thread tr = new Thread(new ThreadStart(CrossThread));
            tr.IsBackground = true;
            tr.Start();

        }
        private void CrossThread()
        {  
            
            
            Class1 c1 = new Class1(this);
            Test t1=new Test(c1.Function);
            
            while (true)
            {
                Thread.Sleep(1000);
                this.textBox1.Invoke(t1);
            }
        }

        
    }
}
Class1类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace WindowsFormsApplication1
{
    class Class1
    {
        Form1 f1;
        public Class1(Form1 f1)
        {
            this.f1 = f1;
        }
        public Class1()
        {
        }
        public void Function()
        {
            f1.textBox1.Text = DateTime.Now.ToString();//这个TextBox1这个东西记得在<span style="font-family: Arial, Helvetica, sans-serif;">InitializeComponent()这个里面把它改成public</span>

        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值