容器动态包含自定义控件的问题(在线等)

hejialin666 2010-01-26 04:33:20
前提条件:三个不同的自定义控件,每个控件都对应一个分组的信息字段,这个字段用来判断是否将本自定义控件外面包一个GroupBox,如果两个自定义控件的分组信息相同,则包在同一个GroupBox中。

我想的方法:for循环获取每个自定义控件的分组信息字段,和前一个的不相同(第一个肯定和前一个不相同,因为前一个是空)则new一个GroupBox对象groupBoxA,用groupBoxA.Controls.Add(controlName);包含。

问题:写的一点程序如下,问题多多呀:
for (int i = 0; i < 3; i++)
{
string strGroupName = 获取到第i个控件的分组信息字段;

if (strGroupName != strGroupNameTemp)
{
//GroupBox GroupControl = new GroupBox();

}
}
1.动态生成GroupBox对象,对象名怎么写?GroupBox GroupControl = new GroupBox();这一句肯定报错,说什么GroupControl已经定义,不能再定义什么的~~
2.最主要的:这个怎么实现?写到这里突然没有思路了!!这个思路是不是有问题?
谢谢高手,分不够再给!
...全文
122 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hejialin666 2010-01-28
  • 打赏
  • 举报
回复
问题解决,谢谢大家!
JOCLI 2010-01-27
  • 打赏
  • 举报
回复
补充下,我是贴了三个button取代你的自定义控件,用button的name来取代你所讲的gropbox属性 (每个控件都对应一个分组的信息字段)
JOCLI 2010-01-27
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, EventArgs e)
{
string btnNames = string.Empty;
foreach (Control ctrl in this.Controls)
{
btnNames = btnNames.Length > 0 ? btnNames + ";" + ctrl.Name : ctrl.Name;
}
string[] names = btnNames.Split(';');
string newNames = string.Empty;
for (int i = 0; i < names.Length; i++)
{
if(!newNames.Contains(names[i].ToString()))
{
newNames=newNames.Length>0?newNames+";"+names[i].ToString():names[i].ToString();
}
}
names = newNames.Split(';');
int locationX = 0,locationY=0;
for (int i = 0; i < names.Length; i++)
{
GroupBox gb = new GroupBox();
gb.Name = "gb" + names[i].ToString();
gb.Location = new Point(locationX, locationY);
locationX = locationX+gb.Size.Width;
locationY = locationY+gb.Size.Height;
this.Controls.Add(gb);
foreach (Control ctrl in this.Controls)
{
if (ctrl.Name == names[i].ToString())
{
ctrl.Location = new Point(0, 0);
gb.Controls.Add(ctrl);
}
}
}
}
测试没有问题,根据你的需要再作小修改,这个简单易懂
记得给分啊 :)
hejialin666 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dugupiaoyun 的回复:]

调用的时候先把groupbox建立好,然后随时new你的控件,指定它的属性Frame也就是groupbox是谁就行了啊。
        private void Form1_Load(object sender, EventArgs e)
        {
            MyCtl ctl = new MyCtl();
            ctl.Frame = this.groupBox1;
        }
[/Quote]

调用的时候先把groupbox建立好?一次建立多少个?我的这个groupbox的个数是不定的,可能一个,可能两个,可能三个。
hejialin666 2010-01-27
  • 打赏
  • 举报
回复
继续在线等,希望高手能给个思路!
dugupiaoyun 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 hejialin666 的回复:]
调用的时候先把groupbox建立好?一次建立多少个?我的这个groupbox的个数是不定的,可能一个,可能两个,可能三个。
[/Quote]

你的groupbox的个数是不定的,但是你的分组的信息的数量是确定的啊,既然知道要分多少个组,就意味着有多少个groupbox啊
hejialin666 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 a569673493 的回复:]
            //放group_box的map,注意dictionary的特性(key是唯一的哦)
            Dictionary <string, GroupBox> map = new Dictionary <string, GroupBox>();

            //假设这是你的自定义控件数组
            Control[] controls = new Control[3];
            foreach(Control c in controls)
            {
                GroupBox g=null;
                //c.Tag.ToString()这是你的控件的分组字段信息的string表现形式
                map.TryGetValue(c.Tag.ToString(), out g);
                if(g==null)
                {
                    g = new GroupBox();
                    map[c.Tag.ToString()]=g;
                }

                g.Controls.Add(c);
            }
我的意见
[/Quote]
谢谢!我先拿到我的程序里试一下哦!
a569673493 2010-01-26
  • 打赏
  • 举报
回复
这里利用dictionary 中key是唯一的特性 你测试一下吧
a569673493 2010-01-26
  • 打赏
  • 举报
回复
//放group_box的map,注意dictionary的特性(key是唯一的哦)
Dictionary<string, GroupBox> map = new Dictionary<string, GroupBox>();

//假设这是你的自定义控件数组
Control[] controls = new Control[3];
foreach(Control c in controls)
{
GroupBox g=null;
//c.Tag.ToString()这是你的控件的分组字段信息的string表现形式
map.TryGetValue(c.Tag.ToString(), out g);
if(g==null)
{
g = new GroupBox();
map[c.Tag.ToString()]=g;
}

g.Controls.Add(c);
}
我的意见
dugupiaoyun 2010-01-26
  • 打赏
  • 举报
回复
用我的这个思路的话,这句就得动态的改下
this.Location = new Point(10, 20);
改为
this.Location = new Point(10, 20 + (_Frame.Controls.Count-1) * (this.Height + 10));
hejialin666 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dugupiaoyun 的回复:]
干嘛这么麻烦哦。
明明很简单的东西.
在你的控件里加个属性:
C# codepublicclass MyCtl : UserControl
{private GroupBox _Frame=null;public GroupBox Frame {get {return _Frame; }set {
_Frame= value;this.Parent= _Frame;
_Frame.Controls.Add(this);this.Location=new Point(10,20);
}
}
}

调用的时候先把groupbox建立好,然后随时new你的控件,指定它的属性Frame也就是groupbox是谁就行了啊。
        private void Form1_Load(object sender, EventArgs e)
        {
            MyCtl ctl = new MyCtl();
            ctl.Frame = this.groupBox1;
        }
[/Quote]
好的,新思路!我理解一下,先试一试。
dugupiaoyun 2010-01-26
  • 打赏
  • 举报
回复
干嘛这么麻烦哦。
明明很简单的东西.
在你的控件里加个属性:

public class MyCtl : UserControl
{
private GroupBox _Frame = null;

public GroupBox Frame {
get { return _Frame; }
set {
_Frame = value;
this.Parent = _Frame;
_Frame.Controls.Add(this);
this.Location = new Point(10, 20);
}
}
}


调用的时候先把groupbox建立好,然后随时new你的控件,指定它的属性Frame也就是groupbox是谁就行了啊。
private void Form1_Load(object sender, EventArgs e)
{
MyCtl ctl = new MyCtl();
ctl.Frame = this.groupBox1;
}
hejialin666 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chenglidexiaoxue 的回复:]
for循环获取每个自定义控件的分组信息字段,和前一个的不相同(第一个肯定和前一个不相同,因为前一个是空)则new一个GroupBox对象groupBoxA

----只和前一个比能行么,除非事先对每个自定义控件的分组信息字段排下序
[/Quote]
是,我也刚刚想到这个问题,谢谢你!
不过目前这个问题很好解决,难就难在怎么动态生成对象,和保存前面对象让后面的用!
chenglidexiaoxue 2010-01-26
  • 打赏
  • 举报
回复
for循环获取每个自定义控件的分组信息字段,和前一个的不相同(第一个肯定和前一个不相同,因为前一个是空)则new一个GroupBox对象groupBoxA

----只和前一个比能行么,除非事先对每个自定义控件的分组信息字段排下序
hejialin666 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hellocode 的回复:]
GroupBox[] gcs = new GroupBox[3];
for (int i = 0; i < 3; i++)
{
              string strGroupName = 获取到第i个控件的分组信息字段;

                if (strGroupName != strGroupNameTemp)
                {
                    //GroupBox GroupControl = new GroupBox();
                      gcs[i] = new GroupBox();//这样就可以了吧!
                }
}

[/Quote]
这个代码的意思是每一个自定义控件都New一个GroupBox对象,可是如果两个控件需要用一个GroupBox,那前一个new的GroupBox怎么保存到后一个控件时再用呢?
hellocode 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hellocode 的回复:]
GroupBox[] gcs = new GroupBox[3];
for (int i = 0; i < 3; i++)
{
              string strGroupName = 获取到第i个控件的分组信息字段;

                if (strGroupName != strGroupNameTemp)
                {
                    //GroupBox GroupControl = new GroupBox();
                      gcs[i] = new GroupBox();//这样就可以了吧!
                }
}

[/Quote]
定义数组时要注意长度~
如果长度不定,也可以考虑用Array.
hejialin666 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hellocode 的回复:]
GroupBox[] gcs = new GroupBox[3];
for (int i = 0; i < 3; i++)
{
              string strGroupName = 获取到第i个控件的分组信息字段;

                if (strGroupName != strGroupNameTemp)
                {
                    //GroupBox GroupControl = new GroupBox();
                      gcs[i] = new GroupBox();//这样就可以了吧!
                }
}

[/Quote]
谢谢,是个法,我再看看!
hellocode 2010-01-26
  • 打赏
  • 举报
回复
GroupBox[] gcs = new GroupBox[3];
for (int i = 0; i < 3; i++)
{
string strGroupName = 获取到第i个控件的分组信息字段;

if (strGroupName != strGroupNameTemp)
{
//GroupBox GroupControl = new GroupBox();
gcs[i] = new GroupBox();//这样就可以了吧!
}
}
hejialin666 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lianshaohua 的回复:]
思路没有什么太大问题,感觉你还要好好的看看书,看看控件与控件的操作,尤其是容器和一般的control
[/Quote]

谢谢!不过对我的帮助不大!
ztenv 2010-01-26
  • 打赏
  • 举报
回复
思路没有什么太大问题,感觉你还要好好的看看书,看看控件与控件的操作,尤其是容器和一般的control

110,500

社区成员

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

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

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