C#自己创建了一个控件,想要动态的加载到PANEL中,怎么实现?求达人赐教 15
4个回答
展开全部
你可以自己拖一个button控件自己看:
首先是实例化:
this.button1 = new System.Windows.Forms.Button();
然后是设置属性:
this.button1.Location = new System.Drawing.Point(24, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
然后加事件句柄(注意扩号里的button1_Click就是事件响应的方法名):
this.button1.Click += new System.EventHandler(this.button1_Click);
所以下面要有一个button1_Click方法:
private void button1_Click(object sender, EventArgs e)
{......
}
除了button1_Click方法要提前写好,其他的代码都要写在动态触发的事件里。
控件也是一个类。
你先简陋的试试,理解后后再想怎末把代码写漂亮。
首先是实例化:
this.button1 = new System.Windows.Forms.Button();
然后是设置属性:
this.button1.Location = new System.Drawing.Point(24, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
然后加事件句柄(注意扩号里的button1_Click就是事件响应的方法名):
this.button1.Click += new System.EventHandler(this.button1_Click);
所以下面要有一个button1_Click方法:
private void button1_Click(object sender, EventArgs e)
{......
}
除了button1_Click方法要提前写好,其他的代码都要写在动态触发的事件里。
控件也是一个类。
你先简陋的试试,理解后后再想怎末把代码写漂亮。
展开全部
算你 走运,我昨晚刚给人写来个这样的.注意看看注释,应该你就会了.
private void btnClick(object aa,EventArgs ee){
if (aa is Button)
{
MessageBox.Show(((Button)aa).Text);
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
//1st create a panel to contain other controls
System.Windows.Forms.Panel bigPanel = new Panel();
bigPanel.Parent = this;//if we don't set this property then we can't see the panel.we comment this sentence for test it;
bigPanel.Size = new Size(this.Width,this.Height - btnCreate.Height -5);//panel's size
bigPanel.Location = new Point(0,btnCreate.Height + 5);//panel's location
//in order to see the panel ,here we set the bkcolor
bigPanel.BackColor = Color.AliceBlue;
//2nd create a label on the panel
Label myLabel = new Label();
myLabel.Parent = bigPanel;
myLabel.Text = @"test Label!" + (char)13 + "Have Fun";//ascii 13 is change line char;
//other properties r default value;so we can know its location is (0,0)
//and size depends on text property;
//3rd create a Password TextBox on panel
TextBox myTxt = new TextBox();
myTxt.Width = 120;//and height is default value ,if we want know its value we can do this
myLabel.Text += (char)13 + myTxt.Height.ToString();
myTxt.Location = new Point((bigPanel.Width - myTxt.Width)/2,(bigPanel.Height - myTxt.Height)/2);//on the center of panel
myTxt.PasswordChar = '#';
//4th create 2 btn on each other;
Button btn1 = new Button();
Button btn2 = new Button();
btn1.Parent = bigPanel;
btn1.Size = new Size(100,100);
btn1.Left = 50;
btn1.Top = 100;
btn1.Text = @"bottom button";
btn1.TextAlign = ContentAlignment.BottomRight; //text is at the bottom_right of btn1;
btn2.Parent = btn1;
//default text;so we don't set text property;
//but we want click the btn2,so we should add a event;
btn2.Click +=new EventHandler(btnClick);
private void btnClick(object aa,EventArgs ee){
if (aa is Button)
{
MessageBox.Show(((Button)aa).Text);
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
//1st create a panel to contain other controls
System.Windows.Forms.Panel bigPanel = new Panel();
bigPanel.Parent = this;//if we don't set this property then we can't see the panel.we comment this sentence for test it;
bigPanel.Size = new Size(this.Width,this.Height - btnCreate.Height -5);//panel's size
bigPanel.Location = new Point(0,btnCreate.Height + 5);//panel's location
//in order to see the panel ,here we set the bkcolor
bigPanel.BackColor = Color.AliceBlue;
//2nd create a label on the panel
Label myLabel = new Label();
myLabel.Parent = bigPanel;
myLabel.Text = @"test Label!" + (char)13 + "Have Fun";//ascii 13 is change line char;
//other properties r default value;so we can know its location is (0,0)
//and size depends on text property;
//3rd create a Password TextBox on panel
TextBox myTxt = new TextBox();
myTxt.Width = 120;//and height is default value ,if we want know its value we can do this
myLabel.Text += (char)13 + myTxt.Height.ToString();
myTxt.Location = new Point((bigPanel.Width - myTxt.Width)/2,(bigPanel.Height - myTxt.Height)/2);//on the center of panel
myTxt.PasswordChar = '#';
//4th create 2 btn on each other;
Button btn1 = new Button();
Button btn2 = new Button();
btn1.Parent = bigPanel;
btn1.Size = new Size(100,100);
btn1.Left = 50;
btn1.Top = 100;
btn1.Text = @"bottom button";
btn1.TextAlign = ContentAlignment.BottomRight; //text is at the bottom_right of btn1;
btn2.Parent = btn1;
//default text;so we don't set text property;
//but we want click the btn2,so we should add a event;
btn2.Click +=new EventHandler(btnClick);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要结合数据库 才能实现的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-01-16
展开全部
楼主看这这一句就行了
Panel1.Controls.Add(#######);
Panel1.Controls.Add(#######);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询