我是新手,刚接触C#
我做一个程序,用户输入3组数据,Name;Brand;Size;存入一个类。
然后将这个类的实例存入一个Checklistbox。
现在要加一个线程,每分钟都存入checklistbox一个类。这三组数据都随机,随便输入什么都行。
还能添加到这个checklistbox中。
线程的问题,我没接触过。望高手说明一下。谢谢啦!跪谢。
public class Car
    {
        public Name name;
        public Brand brand;
        public Size size;        
        public Car(Name n1,Brand b1,Size s1)
        {
            name = n1;
            brand = b1;
            size = s1;
        }
    }
public class Name 
    {
        public string type;
        public Name(string myname)
        {
            type = myname;
        }
    }public void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
                return;
            Name n1 = new Name(this.textBox1.Text);
            Brand b1 = new Brand(this.textBox2.Text);
            Size s1 = new Size(this.textBox3.Text);
            Car car1 = new Car(n1, b1, s1);
            Cangku.al_Car.Add(car1);
            fm1.showCar();
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.Visible = false;
        }public void showCar()
        {
            this.checkedListBox1.Items.Clear();
            for (int i = 0; i < Cangku.al_Car.Count; i++)
            {
                Car mycar = (Car)Cangku.al_Car[i];
                this.checkedListBox1.Items.Add(mycar.name.type+ "的车");
            }
        }
谢谢各位大哥了!  求能写的详细一点!!!
新手 跪谢!

解决方案 »

  1.   

    Timer啊,时间到到时候取一下文本框的内容,然后add进list,如果timer的回掉里不涉及ui操作的话,这样就可以了啊
      

  2.   


    您能具体说说么...
    我不太明白  谢谢啦!public delegate void AddCheck(string val);//委托
            public int flag = 0;
            void ExecuteWork(object sender, ElapsedEventArgs e)
            {
                flag++;
                this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() });
            }
            void AddCheckHandler(string val)
            {
                checkedListBox1.Items.Add(val);
            }
            public Formo()
            {
                InitializeComponent();            System.Timers.Timer tm = new System.Timers.Timer(6000);//6秒
                tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork);
                tm.AutoReset = true;
                tm.Enabled = true;
    }该例演示了 线程对UI线程控件checkedListBox1 定时添加项操作
      

  3.   


    您能具体说说么...
    我不太明白  谢谢啦!public delegate void AddCheck(string val);//委托
            public int flag = 0;
            void ExecuteWork(object sender, ElapsedEventArgs e)
            {
                flag++;
                this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() });
            }
            void AddCheckHandler(string val)
            {
                checkedListBox1.Items.Add(val);
            }
            public Formo()
            {
                InitializeComponent();            System.Timers.Timer tm = new System.Timers.Timer(6000);//6秒
                tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork);
                tm.AutoReset = true;
                tm.Enabled = true;
    }该例演示了 线程对UI线程控件checkedListBox1 定时添加项操作添加命名空间引用using System.Timers;
      

  4.   

    Control.CheckForIllegalCrossThreadCalls = false;加上这句话
      

  5.   


    您能具体说说么...
    我不太明白  谢谢啦!public delegate void AddCheck(string val);//委托
            public int flag = 0;
            void ExecuteWork(object sender, ElapsedEventArgs e)
            {
                flag++;
                this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() });
            }
            void AddCheckHandler(string val)
            {
                checkedListBox1.Items.Add(val);
            }
            public Formo()
            {
                InitializeComponent();            System.Timers.Timer tm = new System.Timers.Timer(6000);//6秒
                tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork);
                tm.AutoReset = true;
                tm.Enabled = true;
    }该例演示了 线程对UI线程控件checkedListBox1 定时添加项操作添加命名空间引用using System.Timers;我做的这个 需要加一个线程啊...
    还要注意 线程的安全
    我照着您这个 做出来  是每一段时间 添加一个字符串 1 2 3 4 5......
    不是一个类的实例您能方便给我说说 怎么做线程 每段时间添加一个类的实例
    大恩大德 没齿难忘了!!!
      

  6.   


    您能具体说说么...
    我不太明白  谢谢啦!public delegate void AddCheck(string val);//委托
            public int flag = 0;
            void ExecuteWork(object sender, ElapsedEventArgs e)
            {
                flag++;
                this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() });
            }
            void AddCheckHandler(string val)
            {
                checkedListBox1.Items.Add(val);
            }
            public Formo()
            {
                InitializeComponent();            System.Timers.Timer tm = new System.Timers.Timer(6000);//6秒
                tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork);
                tm.AutoReset = true;
                tm.Enabled = true;
    }该例演示了 线程对UI线程控件checkedListBox1 定时添加项操作添加命名空间引用using System.Timers;我做的这个 需要加一个线程啊...
    还要注意 线程的安全
    我照着您这个 做出来  是每一段时间 添加一个字符串 1 2 3 4 5......
    不是一个类的实例您能方便给我说说 怎么做线程 每段时间添加一个类的实例
    大恩大德 没齿难忘了!!!基于上述 代码 整体还是这个架子啊, 只是你现在 是  添加字符串,
    就是 赋值的问题
    你类的实例 要添加到哪?
      

  7.   


    您能具体说说么...
    我不太明白  谢谢啦!public delegate void AddCheck(string val);//委托
            public int flag = 0;
            void ExecuteWork(object sender, ElapsedEventArgs e)
            {
                flag++;
                this.Invoke(new AddCheck(AddCheckHandler), new object[] { flag.ToString() });
            }
            void AddCheckHandler(string val)
            {
                checkedListBox1.Items.Add(val);
            }
            public Formo()
            {
                InitializeComponent();            System.Timers.Timer tm = new System.Timers.Timer(6000);//6秒
                tm.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteWork);
                tm.AutoReset = true;
                tm.Enabled = true;
    }该例演示了 线程对UI线程控件checkedListBox1 定时添加项操作添加命名空间引用using System.Timers;我做的这个 需要加一个线程啊...
    还要注意 线程的安全
    我照着您这个 做出来  是每一段时间 添加一个字符串 1 2 3 4 5......
    不是一个类的实例您能方便给我说说 怎么做线程 每段时间添加一个类的实例
    大恩大德 没齿难忘了!!!基于上述 代码 整体还是这个架子啊, 只是你现在 是  添加字符串,
    就是 赋值的问题
    你类的实例 要添加到哪?我要将类的实例 存到checklistbox中啊
    存入的类的实例的三组数据 什么都行
    随机的
    我还有一个按钮 是可以查看 勾选的某个实例的三组数据的
      

  8.   


    给您看看我的代码
    public class Car
        {
            public Name name;
            public Brand brand;
            public Size size;        
            public Car(Name n1,Brand b1,Size s1)
            {
                name = n1;
                brand = b1;
                size = s1;
            }
        }
    public class Cangku
        {
           public static ArrayList al_Car = new ArrayList();
        }
    public class Name 
        {
            public string type;
            public Name(string myname)
            {
                type = myname;
            }
        }
    public class Brand 
        {
            public string type; 
             public Brand(string mybrand)
            {
                type = mybrand;
            }
        }
    public class Size 
        {
            public string type { get; set; }
            public Size(string mysize)
            {
                type = mysize;
            }
        }
    public void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0 || textBox3.Text.Length == 0)
                    return;
                Name n1 = new Name(this.textBox1.Text);
                Brand b1 = new Brand(this.textBox2.Text);
                Size s1 = new Size(this.textBox3.Text);
                Car car1 = new Car(n1, b1, s1);
                Cangku.al_Car.Add(car1);
                fm1.showCar();
                this.textBox1.Clear();
                this.textBox2.Clear();
                this.textBox3.Clear();
                this.Visible = false;
            }
    public void showCar()
            {
                this.checkedListBox1.Items.Clear();
                for (int i = 0; i < Cangku.al_Car.Count; i++)
                {
                    Car mycar = (Car)Cangku.al_Car[i];
                    this.checkedListBox1.Items.Add(mycar.name.type+ "的车");
                }
            }我要做的就是 用一个线程  每一段时间存入一个Car  然后能查看 这个Car的name,brand,size
    我之前存的 不是用户输入的一些信息么 作为Name Brand Size的构造函数的参数么 
    线程做的时候 可以随便什么信息都可以 
    存入之后  能查看 这个Car的3组信息
    具体是什么 不用用户输入 随机 什么都行我新手!!!不好意思!!!谢谢大家了!!!
    我不知道 我能不能形容清楚...
    跪谢!