0

I am trying to create a system that has X amount of buttons, like a POS system. Number of buttons will be different based on the number of items user input. My question is if I should create it dynamically like the following:

    private void createButton(int numOfBtn)
    {
        for (int i = 0; i < numOfBtn; i++)
        {

            Button btn = new Button();
            btn.Name = "button";       
            btn.Text = "button";
            btn.ForeColor = Color.White;
            btn.BackColor = Color.Green;
            btn.Font = new Font("Serif", 24, FontStyle.Bold);
            btn.Width = 170;
            btn.Height = 80;
            btn.TextAlign = ContentAlignment.MiddleCenter;
            btn.Margin = new Padding(5);              
        }
    }

Should create buttons one by one on the designer, dynamically like the above code or if there is a better way of doing so?

Also, currently I am having the windows set to Maximum size when it launches, so when I am creating the buttons, how can I tell if the number of button has max out the space(flowlayout).

Edit: Is it better to create all the buttons from the designer first and assign value of it after? but this way there will always have a maximum amount of buttons, let says if I create 20 buttons from the designer, at max I can only assign twenty items.. what would the better way of doing such task?

cutecutebj
  • 179
  • 1
  • 15
  • If the number of Buttons is variable, you don't have much of a choice, you need to create them dynamically. I'ld suggest to build a specialized class that contains all Buttons references and provides methods and events for those Buttons. Including a specialized Builder constructor or internal method. The Button class itself could be a Custom Control (derived from Button) with specific features. – Jimi Dec 17 '18 at 09:02
  • 5
    Please ignore the first two comments. For a dynamic collection of Buttons, use an ItemsControl with a Button element in its ItemTemplate. Start reading here: [Data Templating Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview) – Clemens Dec 17 '18 at 09:08
  • If this is like a numeric keypad kind of thing make the itemspanel a wrappanel of uniformgrid. You'd want some way to calculate the rows and columns which suit the different layouts best. If instead it's an infinite list of products then use a listbox rather than itemscontrol - it has sliders. – Andy Dec 17 '18 at 09:31
  • @dymanoid Someone (not the OP) added a WPF tag. That Button definition is for a WinForms Button. MVVM is an uncommon pattern there and, if the question is WinForms-related, I really doubt the OP would think to use it (if the OP can implement a MVVM pattern in WinForms, I hardly believe he has a question about organizing collections of Controls). – Jimi Dec 17 '18 at 10:17
  • 1
    [Create dynamic buttons in a grid layout - Create a magic square UI](https://stackoverflow.com/q/33968993/3110834) – Reza Aghaei Dec 17 '18 at 11:16
  • @benji Do you want to do this in WPF form or Windows Form? – Nikunj Kakadiya Dec 17 '18 at 12:31
  • @NikunjKakadiya this is Windows form – cutecutebj Dec 18 '18 at 01:58
  • @Andy thank you for the reply, its not gonna be the numeric keypad, its gonna be a list of items, lets say on a bigger screen size it might be able to fit in 50 buttons, but if its a square screen it might only be able to contain let says 20 buttons. So how should I check how many buttons I can contain on that specific screen size, and the rest of the buttons should be create on the next page – cutecutebj Dec 18 '18 at 02:00
  • I think you'd have to calculate everything out. Work out how big the part of the window is that'll contain your buttons. Decide on some criteria for minimum size to be readable and do some integer division. I think there's a flowlayoutpanel you could maybe use. Long time since I did winforms though. – Andy Dec 18 '18 at 09:39

0 Answers0