解决 尝试将现有按钮添加到带有arraylist的流式布局面板中

101

成员
已加入
2020年12月14日
留言内容
20
编程经验
Beginner
我正在尝试使用arraylists在flowlayout面板上添加一个按钮。
我的阵列列表名称是media
Media.Add(flowlayout1.Control.Add(mainbutton)这是行不通的,但是如果我编写了可以正常工作但没有将其放置在flowlayout面板中的media.Add(mainbutton),我会在form1_load方法中编写
 
Solution
Are you creating these Buttons in code or in the designer? If it's in the designer - it sounds like it is because you say "existing" buttons - then why aren't you adding them to the FlowLayoutPanel in the designer in the first place?

If you are creating them in code and you want to add them all to the FlowLayoutPanel in one go then you need to call AddRange and, for that, you need an array. If you know how many controls you need then create an array of that size, populate it and then add the lot, e.g.
C#:
var buttons = new Button[buttonCount];

for (var i = 0; i < buttonCount; i++)
{
    var btn = new Button();
    
    // Configure btn here.

    buttons[i] = btn;
}...

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,563
地点
悉尼,澳大利亚
编程经验
10+
Are you creating these Buttons in code or in the designer? If it's in the designer - it sounds like it is because you say "existing" buttons - then why aren't you adding them to the FlowLayoutPanel in the designer in the first place?

If you are creating them in code and you want to add them all to the FlowLayoutPanel in one go then you need to call AddRange and, for that, you need an array. If you know how many controls you need then create an array of that size, populate it and then add the lot, e.g.
C#:
var buttons = new Button[buttonCount];

for (var i = 0; i < buttonCount; i++)
{
    var btn = new Button();
    
    // Configure btn here.

    buttons[i] = btn;
}

flowLayoutPanel1.Controls.AddRange(buttons);
If you don't know how many there will be ahead of time then us a List<Button> and convert that to an array when you're done, e.g.
C#:
var buttons = new List<Button>();

while (condition)
{
    var btn = new Button();
    
    // Configure btn here.

    buttons.Add(btn);
}

flowLayoutPanel1.Controls.AddRange(buttons.ToArray());
 
解决方案

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,563
地点
悉尼,澳大利亚
编程经验
10+
这里有很多要解决的问题。首先,请不要发布未格式化的代码,因为它太难阅读了。编辑器工具栏提供了用于格式化内联代码段的大型代码块的按钮,因此没有理由不使用它们。

其次,您发布的未格式化代码不完整。请尽力并花一些时间发布所有且仅相关的代码。您发布的内容有两个开头括号,只有一个结尾,因此显然不是所有相关代码。我们可能可以找出缺少的内容,但重点是我们不必猜测或做出可能错误的假设。

As for the code, you should not be using an ArrayList for anything anyway. They have been obsolete since 2005. If you want to create a dynamic array structure, use a List<T>. In this particular case, I have no idea what any type of list would actually be for. If you want to add a Button to a FlowLayoutPanel then do so.
C#:
flowlayout1.Controls.Add(mainbutton);
That's all you need. What's the ArrayList supposed to be achieving? That's where a FULL and CLEAR explanation of the problem comes in, which includes what you're trying to achieve, how you're trying to achieve it and what happens when you try.
 

101

成员
已加入
2020年12月14日
留言内容
20
编程经验
Beginner
抱歉,下次我将添加更多详细信息,我正在制作一个使用按钮作为视频图标的视频浏览器,并且有200多个按钮,因此我试图将所有现有按钮放入数组列表并将其添加到我的表格
 

101

成员
已加入
2020年12月14日
留言内容
20
编程经验
Beginner
Are you creating these Buttons in code or in the designer? If it's in the designer - it sounds like it is because you say "existing" buttons - then why aren't you adding them to the FlowLayoutPanel in the designer in the first place?

If you are creating them in code and you want to add them all to the FlowLayoutPanel in one go then you need to call AddRange and, for that, you need an array. If you know how many controls you need then create an array of that size, populate it and then add the lot, e.g.
C#:
var buttons = new Button[buttonCount];

for (var i = 0; i < buttonCount; i++)
{
    var btn = new Button();
   
    // Configure btn here.

    buttons[i] = btn;
}

flowLayoutPanel1.Controls.AddRange(buttons);
If you don't know how many there will be ahead of time then us a List<Button> and convert that to an array when you're done, e.g.
C#:
var buttons = new List<Button>();

while (condition)
{
    var btn = new Button();
   
    // Configure btn here.

    buttons.Add(btn);
}

flowLayoutPanel1.Controls.AddRange(buttons.ToArray());
这正是我所要的,只是需要一种将按钮放入列表形式的方法
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,563
地点
悉尼,澳大利亚
编程经验
10+
为了将来参考,对问题的完整而清晰的解释可能是这样的:
I am creating a known/unknown number of Buttons in the Load event handler of my form and I would like to add all those Buttons to a FlowLayoutPanel. I currently have the following code:

在此处提供代码。

当我使用此代码时,在编译/运行时会发生以下错误/行为:

在此处的代码中提供错误消息或意外行为和位置。
您需要解释您要实现的目标以及如何实现(不仅是后者),以及尝试时会发生什么。
 
最佳 底部