0

みなさん、こんにちは。ボタンの配列の配置について助けが必要です。この関数を作成して、前のボタンの名前をスキャンし、次のボタンに +1 という名前を付けたいと思います。その後、これらのボタンを画面に配置したいと思います。それらとそれらの間の特定のスペースが画面の中央に配置されています。メソッドを何度も変更しようとしましたが、このメソッドを機能させる方法がわかりません。

これは私の方法がどのように見えるかです。UPDATED PS.Reference がオブジェクト QQ のインスタンスに設定されていません

   public Button[] ButtonCreator(byte numOfBtnsNeeded,Form1 form)
   {
       Button[] mybtns = new Button[numOfBtnsNeeded];
       foreach (Button  b in mybtns)
       {
               for (int i = 0; i < mybtns.Length; i++)
               {
                   mybtns[i].Name = i.ToString();
                   mybtns[i].Parent = form;
                   mybtns[i].Height = 50;
                   mybtns[i].Width = 50;
                   for (int k = i + 1; k < mybtns.Length; k++)
                   {
                       mybtns[i].Location = new Point(190, 80);
                       mybtns[k].Location = Point.Add(new Point(mybtns[i].Location.X + 10,mybtns[i].Location.Y + 10),new Size(mybtns[i].Size.Width,mybtns[i].Size.Height));
                   }
               }
       }
       foreach (Button b in mybtns)
       {
           b.Show();
       }
       return mybtns;
   }
4

3 に答える 3

1

この例で遊んでください...

ボタングリッド

public partial class Form1 : Form
{

    private List<List<Button>> grid = new List<List<Button>>();

    public Form1()
    {
        InitializeComponent();
        byte numRows = 5;
        byte numCols = 5;
        for (byte i = 0; i < numRows; i++)
        {
            grid.Add(ButtonRowCreator(numCols, 25, (i+1) * 50));
        }
    }

    public List<Button> ButtonRowCreator(byte numOfBtnsNeeded, int x, int y)
    {
        List<Button> btns = new List<Button>();
        for (int i = 0; i < numOfBtnsNeeded; i++)
        {
            Button btn = new Button();
            btn.Size = new Size(50, 50);
            btn.Location = new Point(x + (i * btn.Width), y);
            btns.Add(btn);
            this.Controls.Add(btn);
            btn.Click += new EventHandler(btn_Click);
        }
        return btns;
    }

    void btn_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        btn.Text = "X";

        int curRow = -1, curCol = -1;
        for(int i = 0; i < grid.Count; i++)
        {
            int index = grid[i].IndexOf(btn);
            if (index != -1)
            {
                curRow = i;
                curCol = index;
                Console.WriteLine("curRow = " + curRow.ToString() + ", curCol = " + curCol.ToString());
            }
        }

        // ... now you can use "curRow", "curCol" and "grid" to do something ...

        // reset all BackColors:
        foreach (List<Button> row in grid)
        {
            foreach (Button col in row)
            {
                col.BackColor = Button.DefaultBackColor;
            }
        }

        // the below should give you some examples for the 
        // syntax necessary to access buttons in the grid

        // highlight current row:
        foreach (Button col in grid[curRow])
        {
            col.BackColor = Color.Yellow;
        }

        // highlight current col:
        for (int i = 0; i < grid.Count; i++)
        {
            grid[i][curCol].BackColor = Color.Yellow;
        }
    }

}
于 2013-10-23T19:56:02.920 に答える
0

次のようなものが必要です:

public Button[] ButtonCreator(byte numOfBtnsNeeded)
{
    Button[] mybtns = new Button[numOfBtnsNeeded];

    for (int i = 0; i < mybtns.Length; i++)
    {
        mybtns[i] = new Button();
        mybtns[i].Name = (i + 1).ToString();
    }

    return mybtns;
}

byteを使用している理由はわかりませんintが、どちらの方法でも機能します。

基本的に、配列を作成するときは、配列内にオブジェクトを作成していません。また、foreach ループ内でループしているものを変更することはできないため、for ループが必要です。

于 2013-10-23T18:39:43.010 に答える
0

foreach変数参照 (つまり)は変更できませんb。配列を初期化する場合は、forループを使用する必要があります。

for(int i = 0; i < numOfBtnsNeeded; i++)
{
    var button = mybtns[i] = new Button();
    //Here you can modify the reference of button.

}

また、はデフォルト値が null であることを意味するため、nullでmybtnsいっぱいになります。Buttonreference type

于 2013-10-23T18:35:48.703 に答える