winform devexpress代码动态添加控件
form1.cs
public partial class Form1 : Form
{
int nCount = 0; //记录点击次数
const int startX = 100; //起点位置
const int startY = 100;
const int width = 150; //单元格宽高
const int height = 200;
int x = 0; //每次点击添加 新的起点X坐标
public Form1()
{
InitializeComponent();
}
private void btnAddCol_Click(object sender, EventArgs e)
{
x = startX + nCount * width;
//----------表格边框---------------
Panel pan = new Panel();
pan.Size = new Size(width, 3);
pan.Location = new Point(x, startY);
pan.BackColor = Color.Black;
this.Controls.Add(pan);
Panel pan1 = new Panel();
pan1.Size = new Size(width, 3);
pan1.Location = new Point(x, startY + height);
pan1.BackColor = Color.Black;
this.Controls.Add(pan1);
Panel pan2 = new Panel();
pan2.Size = new Size(3, height);
pan2.Location = new Point(x, startY);
pan2.BackColor = Color.Black;
this.Controls.Add(pan2);
Panel pan3 = new Panel();
pan3.Size = new Size(3, height);
pan3.Location = new Point(x + width, startY);
pan3.BackColor = Color.Black;
this.Controls.Add(pan3);
Panel pan4 = new Panel();
pan4.Size = new Size(width, 3);
pan4.Location = new Point(x, startY + 50);
pan4.BackColor = Color.Black;
this.Controls.Add(pan4);
//----------表格边框END---------------
//文本编辑框
//TextBox text = new TextBox();
//text.Multiline = true;
//text.Location = new Point(x + 3, startY + 53);
//text.Size = new Size(width - 3, height - 53);
//text.BackColor = SystemColors.Control;
//this.Controls.Add(text);
//添加图片
//DevExpress.XtraEditors.PictureEdit pic = new DevExpress.XtraEditors.PictureEdit();
//pic.Location = new Point(x + 3, startY + 53);
//pic.Size = new Size(width - 3, height - 53);
//pic.Image = Properties.Resources.微信图片_20201123154541;
//this.Controls.Add(pic);
HeaderForm f2 = new HeaderForm();
f2.LabelText += new LabelTextHandler(label_text);
f2.ShowDialog();
nCount++;
}
public void label_text(string latxt, string chetxt1, string chetxt2,
string chetxt3, string chetxt4)
{
//列标题
Label la = new Label();
la.Location = new Point(x + 50, startY + 20);
la.Text = latxt;
this.Controls.Add(la);
//CheckBox组合框item文本
DevExpress.XtraEditors.CheckedListBoxControl check = new DevExpress.XtraEditors.CheckedListBoxControl();
check.Location = new Point(x + 20, startY + 70);
check.Items.Add(chetxt1);
check.Items.Add(chetxt2);
check.Items.Add(chetxt3);
check.Items.Add(chetxt4);
this.Controls.Add(check);
}
}
Header.cs
public delegate void LabelTextHandler(string latxt, string chetxt1, string chetxt2,
string chetxt3, string chetxt4);
public partial class HeaderForm : Form
{
public HeaderForm()
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
InitializeComponent();
}
public event LabelTextHandler LabelText;
private void btnHeaderOK_Click(object sender, EventArgs e)
{
LabelText(textEdit1.Text,textEdit2.Text,textEdit3.Text,textEdit4.Text,textEdit5.Text);
this.Close();
}
private void btnHeaderCancel_Click(object sender, EventArgs e)
{
textEdit1.Text = "";
}
}