0

Hello everyone I need some help with the positioning of an array of buttons.I want to make this function so it scans the name of the previous button and it names the next one +1,afterwards I want to position these buttons on the screen having a certain space between them and them being positioned in the center of the screen.I have tried many times to modify my method but I don't know how to get this method to work.

This is how my method looks like. UPDATED PS.Reference not set to an instance of an object Q.Q

   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;
   }
6
  • This is depending how he wants to arrange them. Commented Oct 23, 2013 at 18:44
  • I found that in most cases when there is an idea of having button1, button2, etc., usually is good enough to have one button and one comboBox. Select what you want to do in comboBox and press the button. Commented Oct 23, 2013 at 18:46
  • I need them for tic tac toe I can't make a combobox for this. Commented Oct 23, 2013 at 18:48
  • For tic tac toe - TableLayoutPannel is perfect solution. Commented Oct 23, 2013 at 18:53
  • 2
    We don't even need any control for tic-tac-toe, just draw everything yourself, drawing is what you should make familiar when programming with winforms. Commented Oct 23, 2013 at 18:54

3 Answers 3

1

Play with this example...

Button Grid

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Idle_Mind.That's really sweet code!Thank you for the awesome response!
0

You cannot change a foreach variable reference (ie b). If you want to initialize an array you should use for loop:

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

}

Also, mybtns will be full of nulls since Button is a reference type which means it's default value is a null.

4 Comments

Thanks.How do you position them on the XY axis?Each distant from another?
I have found a solution to the naming problem but I still need to understand how to create a point for each single button and also parent it to the main window,each distance from another a certain amount of space.
You can increment position of the previous button. Position first button, remember location. Then for the next one, take previous location (Point) and increment, for example, keep same X but increase Y'. newY = oldY + oldButton.Height + 3`. Repeat. Or use ready-controls - layout panels.
Can you please take a look at the new method i have added after the edit.
0

you want something like:

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

I'm not sure why you're using a byte over an int, but it works either way.

Essentially, when you create the array, you're not creating the objects within the array. And you cannot modify the thing you are looping over within a foreach loop, so you need a for loop.

1 Comment

Yeah just understood that my if statement is pretty useless in the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.