Solved

Adding Buttons to Winforms Dynamically

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

22 Experts available now in Live!

Get 1:1 Help Now