繁体   English   中英

创建C#Winform简单动态控件

[英]Creating C# winform simple dynamic controls

        private void CreateNewControl()
        {
            List<Control> list = new List<Control>();
            TableLayoutPanel layout = new TableLayoutPanel();
            layout.Dock = DockStyle.Fill;
            this.Controls.Add(layout);
            layout.ColumnCount = 3;
            layout.GrowStyle = TableLayoutPanelGrowStyle.AddRows;

            for (int i = 0; i < 9; i++)
            {

                if (wantedType == DevExpress.XtraEditors.CheckEdit)
                {

                    CheckBox chk = new CheckBox();
                    chk.Tag = i;

                    layout.Controls.Add(chk);
                    layout.AutoScroll = true;

                }


                if (wantedType ==  LabelControl)
                {
                    Label chk = new Label();

                    chk.Tag = i;

                    layout.Controls.Add(chk);
                    layout.AutoScroll = true;

                }

//我想设置布局的列宽,以便在显示标签时它们不会聚集在一起,并且看起来与显示复选框时完全一样。我该怎么做?

总的来说,我要做的是:

  • 在“原型”项目中使用IDE,以在所需位置创建带有控件的表单
  • 看看由IDE(在MyFormName创建的源代码.Designer.cs文件)看到的是由IDE来创造这些控件生成的内容源代码
  • 在我的真实项目中创建自己的表单,并使用基于我从使用IDE创建的原型中学到的内容进行手工编码的代码
// Loop through all the controls you want to add.
// Add a integer field that measures the highest width of each control like

int _iMaxWidth = 0;

for (int i=0; i < TotalControls.Count; ++i)
{
   if ( control[i].Width > _iMaxWidth)
      _iMaxWidth = control[i].Width
}

// Then you'll know what the width size of the column should be.
Col.Width = iMaxWidth + 2; // +2 to make things a little nicer.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM