Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Auto Arrange Dynamic Buttons

$
0
0

I was informed my question is a WinForms question and not a Visual C# question so here I go.

As you can see from my previous posts I have been getting some help on the forums in reference to creating dynamic buttons and everything works great so far. I have one problem though.  I have been trying to get the buttons to auto go to next line right below the button. With the code below I added a variable countWords and when it reaches a certain number or works will change the x coord and place a new line.  The problem with it is that I used multiplication so when it hits the third line it goes too far in the groupbox. Is there an easier away to arrange these buttons on the groupbox?

private void createButtons(int length)
        {
            Button[] buttons = new Button[length];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new Button();
                Point p = new Point(10, (i + 1) * 30);
                buttons[i].Location = p;
                buttons[i].Text = "Button" + i.ToString();
                buttons[i].Name = "Button" + i.ToString();
                this.Controls.Add(buttons[i]);
            }
        }

I would like to have the buttons go from left to right but when a word hits the end of the group box it places the new word exactly underneath the others going from left to right.  Example: 

string x = "I love the CSharp language";

From the code above the integer parameter is our x variable.  I currently have the buttons to where they will mimic that by doing this: 1 1111 111 111111 11111111 being that each 1 represents a button and has the pattern of the letters in the string. Lets say the 3rd word hits the end of the group box I would desire to have it auto place the new words under the others like so: 

1 1111 111

111111 11111111

The reason for this project is to create a word game that you have to guess.  The buttons text are blank until the person guess the word.  This is the first time I am creating buttons dynamically and trying to arrange them in groups. Think of the game wheel of fortune and the boxes are buttons.



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>