65.9K
CodeProject 正在变化。 阅读更多。
Home

动态对话框

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.42/5 (10投票s)

2004 年 8 月 12 日

CPOL

2分钟阅读

viewsIcon

82552

downloadIcon

1553

一篇关于动态对话框和 ListView 控件的文章。

Sample Image

引言

欢迎阅读我在 CodeProject 上的第一篇文章!我编写的程序允许用户输入字段名称、选择数据类型并设置字段长度。完成这些操作后,他们应该能够基于输入内容创建对话框。我之前在 MFC 中做过类似的事情,但还没有在 .NET 中做过。这里提供的代码只是一个示例,用于展示完成此任务所需的过程。

使用代码

由于这更像是一个如何实现的示例,因此类的结构并非完全可重用。但希望您能在您的项目中找到这些概念有用。最初,我想将数据添加到 ListView 中,以便您可以查看实际将要使用的数据。这在 VB 中不仅很容易,而且网络上也有大量的示例展示了如何操作。我遇到的问题是,使用 C# 的示例较少。但是,经过反复试验,我最终使其正常工作。第一步是,在将数据输入 ListView 后,我们需要获取该数据并将其添加到用于动态对话框的类中。这是通过使用 foreach 语句完成的。

        private void btnTest_Click(object sender, System.EventArgs e)
        {
            TestDyn tl;
            int lop = 0;
            int numentries; 
            //
            // If we have entries then allow the test
            //
            numentries = listView1.Items.Count;
            if ( numentries > 0 ) 
            {
                tl = new TestDyn();
                // set the number of entries
                tl.SetNumEntries(listView1.Items.Count);
                // for each entry set its name in the test dialog. 
                foreach (ListViewItem item in listView1.Items) 
                {
                    tl.SetlabelName(lop, item.SubItems[0].Text);
                    lop++;
                }
                tl.ShowDialog();
            }
        }

实际处理动态对话框的类可以使用向导创建。不要使用向导创建任何控件,因为我们将在代码中创建它们。需要创建的第一组代码变量是建立页面上所需的控件的最大数量以及实际数量的计数。

        // for now assume the maximum allowable entries at 3.
        // this can be changed to allow as much as you want. 
        const int maxentries = 3; 
        int    numentries;  // lets keep the count of what we have

        // the actual declarations of the controls that we will create. 
        private System.Windows.Forms.Label [] label1;
        private System.Windows.Forms.TextBox [] textBox1;

在动态对话框类构造函数中,在调用 InitializeComponent 之前,添加一个调用以初始化新的控件。InitMyComponent 创建了请求数量的控件的控件数组和数组元素。

        protected void InitMyComponent()
        {
            int lopcnt; 

            // create the array of controls
            this.label1 = new Label[maxentries];
            this.textBox1 = new TextBox[maxentries];

            // now create the entries for each array element 
            for ( lopcnt = 0; lopcnt < maxentries; lopcnt++) 
            {    
                this.label1[lopcnt]  = new System.Windows.Forms.Label();
                this.textBox1[lopcnt]  = new System.Windows.Forms.TextBox();
            }
        }

现在是实际设置每个对象位置的代码。您不仅需要找到初始位置,而且需要在控件集的每次迭代中进行递增。如果您想进一步完善它,可以花更多时间在初始位置和递增上。由于我真的把它作为一个示例,所以我没有花太多时间进行完善。

        protected override void OnLoad(EventArgs e)
        {
            int    lop;
            int    x1;
            int    y1;
            int    x2;
            int    y2;
            int    width1;
            int    height1;
            int taborder = 0; 
            int    width2;
            int    height2;

            // TODO:  Add TestLic.OnLoad implementation
            base.OnLoad (e);

            // set the initial setting of the controls. 
            // for the label 
            // although the initial placement
            // of the controls are a bit arbitrary you 
            // would want to be more precise for a better look and feel. 
            x1 = 16;
            y1 = 24;
            width1 = 88;
            height1 = 16;

            // and the textbox
            x2 = 128;
            y2 = 16;
            width2 = 120;
            height2 = 20;
            this.SuspendLayout();
            // for each entry that was requested. 
            for ( lop = 0; lop < numentries; lop++) 
            {
                // 
                // label n
                // 
                label1[lop].Location = new System.Drawing.Point(x1, y1);
                label1[lop].Name = "label" + Convert.ToString(lop);
                label1[lop].Size = new System.Drawing.Size(width1, height1);
                label1[lop].TabIndex = taborder;
                taborder++;

                // 
                // textBox n
                // 
                textBox1[lop].Location = new System.Drawing.Point(x2, y2);
                textBox1[lop].Name = "textBox" + Convert.ToString(lop);
                textBox1[lop].Size = new System.Drawing.Size(width2, height2);
                textBox1[lop].TabIndex = taborder;
                taborder++;

                this.Controls.Add(textBox1[lop]);
                this.Controls.Add(label1[lop]);

                y1 += 50;
                y2 += 50;
            }
            this.ResumeLayout(false);
        }

关注点

接下来可以执行的任务是添加不同类型的控件。在这里,我只允许 TextBoxLabel。另一个任务是在对话框上添加一个 Panel,并将控件放在上面。这样,当控件数量超过可见空间时,您可以添加滚动条。

© . All rights reserved.