求帮助winform动态生成控件的问题

.Net技术 码拜 8年前 (2016-02-24) 1174次浏览
本人想在窗体加载后通过获取到的List添加几个checkbox,第一个checkbox是写死的,之后的位置与这个写死的checkbox相关。但是动态生成的这几个一直不显示,但是设断点看到数据时抓到了的,checkbox对象也正确的生成了

List<APP> M = new List<APP>();
 List<CheckBox> cbNew = new List<CheckBox>();
private void Cb()
        {
            M = GetAPPList();       
            cbFirst.Text =M[0].APPNAME ;//cbfirst是第一个写checkbox,写死的
            cbFirst.Tag = M0].APPID;
            for (int i = 1; i < M.Count; i++)
            {
                CheckBox cbAdd = new CheckBox();
                cbAdd.Text = sysMenu[i].APPNAME.ToString();
                cbAdd.Tag = sysMenu[i].APPID;
                cbAdd.Width = 84;
                cbAdd.Height = 16;
                cbAdd.Visible = true;
                int left = 90 + 84 * i;
                cbAdd.Location = new Point(left, 51);
                cbNew.Add(cbAdd);
            }
            for (int j = 1; j < cbNew.Count; j++)
            {
                this.Controls.Add(cbNew[j]);
            }
        }

求帮助为什么会不显示。

解决方案

15

你知道这个this代表什么?

15

全动态添加控件

        private void 设置选项()
        {
            string[] 复选文 = { "声音提示", "载入游戏", "智能列示", "智能行示" };
            CheckBox[] 复选组 = Array.ConvertAll(复选文, 控件 => new CheckBox());
            int 序号 = 0;
            foreach (CheckBox 控件 in 复选组)
            { 
                控件.Name = 控件.Text = 复选文[序号++];
                控件.Checked = true;
                控件.AutoSize = true;
                //控件.Parent = this;
                this.Controls.Add(控件);
            }
            Button 刷钮 = (Button)this.Controls["刷新"];
            CheckBox 声音 = (CheckBox)this.Controls[复选文[0]],
                     载入 = (CheckBox)this.Controls[复选文[1]],
                     智列示 = (CheckBox)this.Controls[复选文[2]],
                     智行示 = (CheckBox)this.Controls[复选文[3]];
            声音.Location = new Point(刷钮.Location.X + 刷钮.Width + 4, 刷钮.Location.Y);
            载入.Location = new Point(声音.Location.X, 声音.Location.Y + 声音.Height);
            智列示.Location = new Point(声音.Location.X + 声音.Width + 4, 声音.Location.Y);
            智行示.Location = new Point(智列示.Location.X, 智列示.Location.Y + 智列示.Height);
        }

http://blog.csdn.net/xianfajushi/article/details/38984049

140

你先在窗口上拖一个容器上去,在容器上动态生成。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求帮助winform动态生成控件的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)