Windows Phone 7 Belling‘s课堂(十三) 动态添加多个控件

此节课共分三节介绍:
1:先看一个学生的例子:有点问题。
如何动态添加多个控件?
我要的效果是这样
这是是程序原有的控件。
textbox  textbox  textbox  lable
textbox  textbox  textbox  lable
textbox  textbox  textbox  lable

当控件不可以满足时,我要添加一行控件

textbox  textbox  textbox  lable 换行
textbox  textbox  textbox  lable 换行
textbox  textbox  textbox  lable 换行
textbox  textbox  textbox  lable 换行
textbox  textbox  textbox  lable 换行


这样的效果怎么弄?

这是我做的代码
for (int i = 0; i < 3; i++)
            {

                TextBox text = new TextBox();


                text.Name = string.Format("text{0}", i + 1);


                text.Left = i * 120;


                panel1.Controls.Add(text);



            }

好啦,上边是一个学生所提到的问题。
 
2:接下来是重点了,很高明的手法,控件集成。
     不过用的form窗体程序,原理是一样的。
     (1)先看图:
     

 
    (2)看一下前台UI代码
       
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace generateControl
10 {
11     public partial class Form1 : Form
12     {
13         public Form1()
14         {
15             InitializeComponent();
16             x = textBox1.Location.X;
17             y = textBox1.Location.Y;
18         }
19 
20         static int x, y;
21         private void button1_Click(object sender, EventArgs e)
22         {
23             GenerateControl();
24         }
25 
26         private void GenerateControl()
27         //完全可以将三个textbox合在一起,作为GenerateControl的整个控件。。。
28         {
29             y = y + 26;
30             TextBox txt1 = new TextBox();
31             txt1.Location = new System.Drawing.Point(x, y);
32             this.Controls.Add(txt1);
33 
34             TextBox txt2 = new TextBox();
35             txt2.Location = new System.Drawing.Point(x+107, y);
36             this.Controls.Add(txt2);
37 
38             TextBox txt3 = new TextBox();
39             txt3.Location = new System.Drawing.Point(x+214, y);
40             this.Controls.Add(txt3);
41 
42             Label lbl1 = new Label();
43             lbl1.Text = "label1";
44             lbl1.Location = new System.Drawing.Point(x+321, y);
45             this.Controls.Add(lbl1);
46         }
47     }
48 }

 

 
 
接下来,是一个事例程序,大家可以下载一下。
 附件
  generateControl.rar  (30.35 KB)

上传时间:2011-4-18 18:53
下载次数:41

写了个DEMO希望对LZ有帮助
 
你把panel1的AutoScroll属性设为True再看看 
==============================
不行

转载于:https://www.cnblogs.com/Belling/archive/2012/12/17/2822007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值