0

I am creating a grid of buttons using the following code:

Button[][] buttons;

In the method:

for (int r = 0; r < row; r++)
    {
       for ( int c = 0; c < col; c++)
           {
             buttons[r][c] = new Button();
           }
    }

How can I clear and reset buttons[][] if row or col changes, is there away to do it?

4

2 回答 2

3

就在这里。您可以调用该Array.Clear()函数。由于您的数组包含Button引用类型的对象,因此它将数组中的每个项目重置为null.

Array.Clear(buttons, 0, buttons.Length);

但是,我强烈建议为此使用通用容器之一,而不是原始数组。例如,aList<T>将是一个不错的选择。在你的情况下,T将是Button.

using System.Collections.Generic;  // required at the top of the file for List<T>

List<Button> buttons = new List<Button>();

要像二维数组一样使用它,您将需要一个嵌套列表(基本上,aList包含List包含Button对象的 a)。语法有点吓人,但不难理解它的含义:

List<List<Button>> buttons = new List<List<Button>>();
于 2013-03-25T22:22:02.337 回答
-1

你可以调用 Clear() 方法

http://msdn.microsoft.com/en-us/library/system.array.clear.aspx

于 2013-03-25T22:21:15.253 回答