日期:2014-05-18  浏览次数:20844 次

动态添加控件
小弟winform开发中有个需求:有个很大的输入区域(目前用richtextbox),里面的内容是放一些模板的,模板里面有很多关键字(比如国家,区域,街道等),不同的用户在这些关键字部分的需求是不同,但是我们可以配置这些关键字.现在用户要求对这些关键字内容卡控,只能输入特定的内容(比如数字,101-308等)或者配置的内容(固定的省份).

我的设想是在一个大的控件里面根据关键字标签动态添加控件到外面的容器中,但是面临着一下两个问题:
1.第一次编辑完了之后如何继续保留这一部分关键字标签,能够接着继续编辑.
2.动态添加的控件如何能够定位,当外部容器布局改变的时候(比如说字体,缩放,拖拽,滚动等).

谢谢!



------解决方案--------------------
定位这个就要你自己控制了,然后通过anchor或dock去随着窗体而动
------解决方案--------------------
C# code

        #region *****************动态创建控件,
        RichTextBox richTextBox = null;
        private void richTextBox_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            richTextBox.Height = e.NewRectangle.Height + 10;
            richTextBox.Size = new System.Drawing.Size(912, richTextBox.Height);
        }

        private void createControls(string Message, string SendName, string SendTime, int replyId, int i, UnionInfo un)
        {
            Panel pnl = new Panel();
            pnl.BackColor = Color.White;
            // GroupBox pnl = new GroupBox();
         //    pnl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            richTextBox = new RichTextBox();
            richTextBox.BackColor = Color.White;
            richTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;

            richTextBox.Name = "richTextBox" + "_" + i;

            richTextBox.TabIndex = 7;
            richTextBox.Text = Message;
            richTextBox.ReadOnly = true;
            richTextBox.ScrollBars = RichTextBoxScrollBars.None;

            richTextBox.ContentsResized += new ContentsResizedEventHandler(richTextBox_ContentsResized);
            richTextBox.LinkClicked += new LinkClickedEventHandler(richTextBox_LinkClicked);
            richTextBox.Location = new System.Drawing.Point(20, 30);
            pnl.Controls.Add(richTextBox);


            Label lbl1 = new Label();
            lbl1.AutoSize = true;
            lbl1.Location = new System.Drawing.Point(430, 10);
            lbl1.Name = "lbl7" + "_" + i;
            lbl1.Size = new System.Drawing.Size(41, 12);
            lbl1.TabIndex = 6;
            lbl1.Text = "附件:";

            Label lbl2 = new Label();
            lbl2.AutoSize = true;
            lbl2.Location = new System.Drawing.Point(212, 10);
            lbl2.Name = "lbl8" + "_" + i;
            lbl2.Size = new System.Drawing.Size(41, 12);
            lbl2.TabIndex = 5;
            lbl2.Text = "发件时间:";

            Label lbl3 = new Label();
            lbl3.AutoSize = true;
            lbl3.Location = new System.Drawing.Point(20, 10);
            lbl3.Name = "lbl9" + "_" + i;
            lbl3.Size = new System.Drawing.Size(41, 12);
            lbl3.TabIndex = 4;
            lbl3.Text = "发送人:";

            TextBox textBox1 = new TextBox();
            textBox1.BackColor = Color.White;
            textBox1.Location = new System.Drawing.Point(75, 6);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(123, 21);
            textBox1.TabIndex = 4;
            textBox1.ReadOnly = true;
            textBox1.Text = SendName;
            // 
            // textBox2
            // 
            TextBox textBox2 = new TextBox();
            textBox2.BackColor = Color.White;
            textBox2.Location = new System.Drawing.Point(280, 6);
            textBox2.Name = "textBox2";
            textBox2.Size = new System.Drawing.Size(140, 21);
            textBox2.TabIndex = 5;
            textBox2.ReadOnly = true;
            textBox2.Text = SendTime;
         


            pnl.Controls.Add(richTextBox);
            pnl.Controls.Add(lbl1);
            pnl.Controls.Add(lbl2);
            pnl.Controls.Add(lbl3);
            pnl.Controls.Add(textBox1);
            p