?
Solved

Adding Buttons to Winforms Dynamically

Posted on 2007-11-19
3
Medium Priority
?
1,073 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
[X]
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
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 600 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 900 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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