110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newBT().Tag=newCB();
}
//动态生成ComboBox
private Control newCB()
{
ComboBox cb = new ComboBox();
cb.Location = new Point(new Size(50, 50));
cb.Text="aaaaa";
this.Controls.Add(cb);
return cb;
}
//动态生成Button
private Control newBT()
{
Button bt = new Button();
bt.Location = new Point(new Size(200, 50));
bt.Text = "ck";
this.Controls.Add(bt);
bt.Click += new System.EventHandler(this.bt_Click);
return bt;
}
private void bt_Click(object sender, EventArgs e)
{
Button bt=sender as Button;
ComboBox cb =bt.Tag as ComboBox;
}
}
}
//将sender转换成button。
Button btn = sender as Button;
if(btn != null)
{
Text btn_Text = btn.Text;
//对于ComboBox相对复杂一点,也许你需要this.Controls中找。
foreach(Control ctr in this.Controls)
{
if(ctr is ComboBox)
{
ComboxBox cbx = ctr as ComboBox;
MessageBox.Show(cbx.Text);
}
}