补充:将modify中的Label lb = new Label();放到public partial class Form1 : Form 下面,是可以修改的,但我需要的是在子线程中创建控件并显示,比如我第一次点创建label并显示,第二次点创建textbox并显示,两次是不一样的,不能放在外面实例化,必须在子线程中初始化并加到主窗体中显示,这样该如何,csdn的各位朋友有什么好的意见?
1楼的代码示意如下 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 multiThreadTest { public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } public delegate void chageMain(string str); private void childThread() { modify("yang"); i++; //modify("beijing"); } private void modify(string str) { this.Invoke(new System.Action<object> (delegate { this.label1.Text = "窗口中已有控件" + str + i.ToString(); if (i == 0) { Label lb = new Label(); lb.Text = "自创控件" + str + i.ToString();//自己创建的控件 this.Controls.Add(lb); } else { TextBox lb = new TextBox(); lb.Text = "自创控件" + str + i.ToString();//自己创建的控件 this.Controls.Add(lb); } } ), 1); } private void button1_Click(object sender, EventArgs e) { //this.label1.Text = i.ToString(); var t1 = new Thread(childThread); t1.IsBackground = true; t1.Start(); } } }开始时创建的是label,第二次点button创建的是textbox,这种该如何实现?
foreach (Control c in this.Controls) { if (c is Label) this.richTextBox1.Text += ((Label)c).Text + Environment.NewLine; } 查看了下,控件已经添加了 莫非是被第一个给挡住了还是?
lb.AutoSize = true; lb.Location = new System.Drawing.Point(i , i * 20);加个这个就可以了,还是挡住了
十分感谢,自己糊涂了,确实是位置的问题,加分,贴上修改后的代码,希望对后面的人有所帮助,结帖 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 multiThreadTest { public partial class Form1 : Form { int i = 0; public Form1() { InitializeComponent(); } public delegate void chageMain(string str); private void childThread() { modify("Test"); i++; } private void modify(string str) { this.Invoke(new System.Action<object> (delegate { this.label1.Text = "窗口中已有控件" + str + i.ToString(); foreach (Control c in this.Controls) { if (c is Label) this.Controls.Remove(c); } Label lb = new Label(); lb.Text = "自创控件" + str + i.ToString();//自己创建的控件 this.Controls.Add(lb); //lb.AutoSize = true; //lb.Location = new System.Drawing.Point(i, i * 20); } ), 1); } private void button1_Click(object sender, EventArgs e) { var t1 = new Thread(childThread); t1.IsBackground = true; t1.Start(); } } }
下面,是可以修改的,但我需要的是在子线程中创建控件并显示,比如我第一次点创建label并显示,第二次点创建textbox并显示,两次是不一样的,不能放在外面实例化,必须在子线程中初始化并加到主窗体中显示,这样该如何,csdn的各位朋友有什么好的意见?
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 multiThreadTest
{
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
} public delegate void chageMain(string str); private void childThread()
{
modify("yang");
i++;
//modify("beijing");
} private void modify(string str)
{
this.Invoke(new System.Action<object>
(delegate
{
this.label1.Text = "窗口中已有控件" + str + i.ToString();
if (i == 0)
{
Label lb = new Label();
lb.Text = "自创控件" + str + i.ToString();//自己创建的控件
this.Controls.Add(lb);
}
else
{
TextBox lb = new TextBox();
lb.Text = "自创控件" + str + i.ToString();//自己创建的控件
this.Controls.Add(lb);
}
}
), 1);
} private void button1_Click(object sender, EventArgs e)
{
//this.label1.Text = i.ToString();
var t1 = new Thread(childThread);
t1.IsBackground = true;
t1.Start();
}
}
}开始时创建的是label,第二次点button创建的是textbox,这种该如何实现?
{
if (c is Label)
this.richTextBox1.Text += ((Label)c).Text + Environment.NewLine;
}
查看了下,控件已经添加了
莫非是被第一个给挡住了还是?
lb.Location = new System.Drawing.Point(i , i * 20);加个这个就可以了,还是挡住了
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 multiThreadTest
{
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
} public delegate void chageMain(string str); private void childThread()
{
modify("Test");
i++;
} private void modify(string str)
{
this.Invoke(new System.Action<object>
(delegate
{
this.label1.Text = "窗口中已有控件" + str + i.ToString();
foreach (Control c in this.Controls)
{
if (c is Label)
this.Controls.Remove(c);
} Label lb = new Label();
lb.Text = "自创控件" + str + i.ToString();//自己创建的控件
this.Controls.Add(lb);
//lb.AutoSize = true;
//lb.Location = new System.Drawing.Point(i, i * 20);
}
), 1);
} private void button1_Click(object sender, EventArgs e)
{
var t1 = new Thread(childThread);
t1.IsBackground = true;
t1.Start();
}
}
}