Question How to create pages in winform dynamically and add buttons to it

sbhojani

New member
Joined
Oct 25, 2017
Messages
1
Programming Experience
3-5
Hello All,
I am trying to develop an application wherein i need to read data and create buttons dynamically in winform. When the number of buttons exceed a certain limit i need to create a button named next which should basically navigate to next page and create the remaining number of buttons. For example if the number of buttons to be created is 16 then 13 buttons should be displayed on page 1 and when the user clicks on next the remaining 3 should be displayed on next page. How should i do this through code. Also the size of the form should vary dynamically based on the number of buttons.

Thanks
 
It's not really clear what you mean by "pages". If it's WinForms, the first thing that comes to mind is TabPages in a TabControl. In that case, you can create a new TabPage object and add it to the TabPages collection of the TabControl.

I'd suggest that you add a TableLayoutPanel to the TabPage and give it the appropriate number of rows and columns so you can then simply Add a Button to its Controls collection to have them laid out in a table. When a page is full, you create another TabPage and another TableLayoutPanel, add the latter to the former and then the former to the TabControl. I suggest that you do what you can with that and then post back when you get stuck.

I'd probably suggest starting with at least one TabPage with a TableLayoutPanel in the designer. If you know the maximum number of pages and it's not too large then you might add them all in the designer and then simply remove the ones you don't need. Otherwise, the designer code for the first one will give you a good idea of the code you need to write to add another page.
 
I'd suggest that you add a TableLayoutPanel to the TabPage and give it the appropriate number of rows and columns so you can then simply Add a Button to its Controls collection to have them laid out in a table.
I would probably go for a FlowLayoutPanel in the TabPage.
 
Back
Top Bottom