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);

ただし、生の配列ではなく、汎用コンテナーの 1 つを使用することを強くお勧めします。たとえば、aList<T>が適しています。あなたの場合、TになりますButton

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

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

2 次元配列のように使用するには、ネストされたリスト (基本的に、オブジェクトListを含む aListを含む a) が必要になります。Button構文は少し難解ですが、意味を理解するのはそれほど難しくありません。

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 に答える