有一个服务器/客户端程序,在客户端连接到服务器时启动一个线程,现在我想在启动这个线程时创建一个button,点击这个button后,建立一个dataGridView,在dataGridView中显示数据库的信息。
现在线程里创建一个button搞不定,应该是用委托、时间吧,但是搞不好,另外,连接几十台客户机,button的命名也是一个问题,点击每个button的处理也要单独编写吗?
现在线程里创建一个button搞不定,应该是用委托、时间吧,但是搞不好,另外,连接几十台客户机,button的命名也是一个问题,点击每个button的处理也要单独编写吗?
public delegate void delTest(); private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(AddButton));
thread.IsBackground = true;
thread.Start();
} private void AddButton()
{
if (InvokeRequired)
{
delTest del = delegate()
{
Button bt = new Button();
bt.Text = "Test";
bt.Name = "test";
bt.Location = new Point(50, 50);
bt.Click += new EventHandler(bt_Click);
this.Controls.Add(bt);
};
this.Invoke(del);
}
} void bt_Click(object sender, EventArgs e)
{
DataGridView dgv = new DataGridView();
//........
this.Controls.Add(dgv);
}
{
Button bt = new Button();
bt.Text = "Test";
bt.Name = "test";
bt.Location = new Point(50, 50);
bt.Click += new EventHandler(bt_Click);
this.Controls.Add(bt);
}).Start();
我今天刚听说Invoke 机制,
下面的代码示例显示包含一个委托的控件。委托封装一个将项添加到列表框中的方法,该方法在拥有窗体的基础句柄的线程上使用指定的参数执行。用户单击按钮时,Invoke 运行此委托。using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading; public class MyFormControl : Form
{
public delegate void AddListItem(String myString);
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example ";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod(String myString)
{
myListBox.Items.Add(myString);
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
String myString; public void Run()
{
for (int i = 1; i <= 5; i++)
{
myString = "Step number " + i.ToString() + " executed";
Thread.Sleep(400);
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle with
// the specified list of arguments.
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});
}
}
}这只是一种写法,还有别的写法,这里就不说了。
我开始想把下面的代码加进去,总是不行
private void InitializeMyButton()
{
Button button1 = new Button(); button1.DialogResult = DialogResult.OK; Controls.Add(button1);
}