I CALL 我又遇难了!Groupbox问题!

APOLLO_TS 2013-04-08 08:07:48
页面就是两个groupbox,两个label

动态添加checkbox,结果就显示其中一个!到底哪个地方出问题了啊?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void SetCheckBox(GroupBox gb,Label lb)
{
lb.Text = lb.Text + "----" + gb.Location.X.ToString()+"/"+gb.Location.Y.ToString();
for (int a = gb.Location.Y + 12; a < gb.Location.Y + gb.Size.Height - 22; a = a + 22)
{
for (int b = gb.Location.X + 12; b < gb.Location.X + gb.Size.Width - 114; b = b + 112)
{

CheckBox cb = new CheckBox() { Text = "GB_" + b.ToString() + "_" + a.ToString(), Name = "GB_" + b.ToString() + "_" + a.ToString() };
cb.Location = new System.Drawing.Point(b, a);
cb.Size = new System.Drawing.Size(108, 16);
gb.Controls.Add(cb);
lb.Text = lb.Text + "----" + cb.Name;
}
}

}

private void Form1_Load(object sender, EventArgs e)
{
SetCheckBox(this.groupBox1,this.label1);
SetCheckBox(this.groupBox2, this.label2);
}
}
}





第二个groupbox 怎么就没添加进去呢?坐标没重复,名称没重复。god!
...全文
296 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
APOLLO_TS 2013-04-08
  • 打赏
  • 举报
回复
mygod!我当成绝对定位用了!一句话就道出我的问题了!三Q ------------------- 目前一部分代码改成如下:

        /// <summary>
        /// 向GroupBox中添加CheckBox
        /// </summary>
        /// <param name="groupBox">指定的GroupBox</param>
        /// <param name="colOfUIItems">CheckBox属性的集合</param>
        /// <param name="evnForCheckBox">CheckBox要响应的点选事件</param>
        private void SetCheckBox(GroupBox groupBox, IEnumerable<UIItems> colOfUIItems,EventHandler evnForCheckBox)
        {
            //存储CheckBox对象
            Queue<CheckBox> que = new Queue<CheckBox>();
            var t = from d in colOfUIItems
                    select new CheckBox
                    {
                        Name = d.ItemName,
                        Text = d.Text,
                    };
            //CheckBox添加事件
            foreach (var item in t)
            {
                item.CheckedChanged +=evnForCheckBox;
                que.Enqueue(item);
            }
            //指定CheckBox在GroupBox中的坐标,坐标为相对坐标
            for (int y =20; y <groupBox.Size.Height - 22; y = y + 22)
            {
                for (int x =12; x <groupBox.Size.Width - 16; x = x + 112)
                {
                    if (que.Count > 0)
                    {
                        CheckBox cb = que.Dequeue();
                        cb.Location = new System.Drawing.Point(x, y);
                        cb.Size = new System.Drawing.Size(108, 16);
                        groupBox.Controls.Add(cb);
                    }
                }
            }
        }

宝_爸 2013-04-08
  • 打赏
  • 举报
回复
cb.Location 应该是相对坐标,给第二个group赋值的时候cb.Location = new System.Drawing.Point(b, a);中的a太大了。
宝_爸 2013-04-08
  • 打赏
  • 举报
回复
位置不对了。。我只修改了一句话: CheckBox cb = new CheckBox() { Text = "GB_" + b.ToString() + "_" + a.ToString(), Name = "GB_" + b.ToString() + "_" + a.ToString() }; cb.Location = new System.Drawing.Point(b, a - gb.Location.Y); cb.Size = new System.Drawing.Size(108, 16); gb.Controls.Add(cb); lb.Text = lb.Text + "----" + cb.Name;
蝶恋花雨 2013-04-08
  • 打赏
  • 举报
回复
for (int b = gb.Location.X + 12; b < gb.Location.X + gb.Size.Width - 114; b = b + 112) 这里产生的b的坐标重复了。所以生成的是一个。
  • 打赏
  • 举报
回复
你给我说下你form的大小吧

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧