要对七、八个Textbox动态添加可以选择的组合框,点击文本框中先显示组合框Combox并且在组合框中异步加载数据,选择Combox数据,并自动将数据添加Textbox.text中,如果按照常规写法,可能需要更多的代码; - void Form1_Load(object sender, EventArgs e)
- {
- //对三个文本框添加事件
- textBox1.Click += new EventHandler(textBox1_Click);
- textBox2.Click += new EventHandler(textBox1_Click);
- textBox3.Click += new EventHandler(textBox1_Click);
- }
- void textBox1_Click(object sender, EventArgs e)
- {
- //TODO:定义一个动态组合框
- ComboBox cmb = new ComboBox();
- TextBox txtBox = sender as TextBox;
-
- //用组合框来覆盖文本框
- cmb.Location = txtBox.Location;
- cmb.Size = txtBox.Size;
- this.Controls.Add(cmb);
- cmb.Visible = true;
- //置前
- cmb.BringToFront();
- //对组合框异步加载
- ThreadPool.QueueUserWorkItem(
- state =>
- this.BeginInvoke(new Action(() =>
- { cmb.Items.AddRange(new object[] {"one", "two", "three"}); })));
- //添加事件,使用匿名方法
- cmb.SelectedIndexChanged += delegate
- {
- txtBox.Text = cmb.Text;
- //置后
- cmb.SendToBack();
- cmb = null;
- };
- }
复制代码
|
|