Solved

Adding Buttons to Winforms Dynamically

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

8 Experts available now in Live!

Get 1:1 Help Now