Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Adding Buttons to Winforms Dynamically

Posted on 2007-11-19
3
1,070 Views
Last Modified: 2013-12-17
I have to diaplay buttons on the form dynamically depending upon the RecordCount obtained
from database.
I am thinking of creating ButtonCollection Class or Array Class. Then add to panel and to the form
Is it good approach. Or there is better way to handle this in .NET 2.0 using generics.

Can anyone suggest or show a simple example.
0
Comment
Question by:AdibRahman77
3 Comments
 
LVL 10

Expert Comment

by:dkloeck
ID: 20313497
I thisn the best would be create something like this:

create a panel and create the buttons inside (with a panel its easier to clear the buttons you had before)

//clear the panel
panel1.Controls.Clear();
for (int i = 0; i < recordCount; i++)
            {
                //create the button
                Button tempButton = new Button();
               //set some properties
                tempButton.Location = new Point(10, 10 + i * 20);
                tempButton.Size = new Size(20, 20);
               //add to the panel's controls
                panel1.Controls.Add(tempButton);                
            }
0
 
LVL 16

Assisted Solution

by:TSmooth
TSmooth earned 200 total points
ID: 20313530
The only reason I could see to create your own class would be if you wanted to add some special helper functions to assist with the positioning or formatting of the buttons. Otherwise, you could just use a generic collection of buttons such as List<Button> and then when necessary, loop through those to position them on the panel.

You could alternatively use a DataGridView and have each row contain a button.
0
 
LVL 14

Accepted Solution

by:
ptakja earned 300 total points
ID: 20314150
I suggest that you use either a Panel approach as TSmooth suggested, or just use the Controls collection on the form. Each form has a Controls collection. You access it the same way as a panel. Here's TSmooth's suggestion translated to VB.NET:

//clear the panel
Call panel1.Controls.Clear()
For ii As Integer = 0 to RecordCount
    'create the button
    Dim btn As New Button
    'set some properties
    btn.Location = New Point(10, 10 + ii * 20)
    btn.Size = New Size(20, 20)
    'add to the panel's controls
    panel1.Controls.Add(btn)
Next

To add the button to the form collection change panel1.Controls.Add(btn) TO Me.Controls.Add(btn).
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question