Solved

Adding Buttons to Winforms Dynamically

Posted on 2007-11-19
3
1,067 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now