C#自己创建了一个控件,想要动态的加载到PANEL中,怎么实现?求达人赐教 15

 我来答
bluelight458794
2009-01-16 · TA获得超过918个赞
知道小有建树答主
回答量:349
采纳率:50%
帮助的人:219万
展开全部
你可以自己拖一个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方法要提前写好,其他的代码都要写在动态触发的事件里。
控件也是一个类。
你先简陋的试试,理解后后再想怎末把代码写漂亮。
TheFiend
2009-01-16 · TA获得超过625个赞
知道大有可为答主
回答量:1346
采纳率:0%
帮助的人:1201万
展开全部
算你 走运,我昨晚刚给人写来个这样的.注意看看注释,应该你就会了.
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-17 · TA获得超过534个赞
知道小有建树答主
回答量:936
采纳率:100%
帮助的人:0
展开全部
要结合数据库 才能实现的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2009-01-16
展开全部
楼主看这这一句就行了
Panel1.Controls.Add(#######);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式