954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to generate button at runtime and its text from database for windows application

Hello,
Can anyone help me out
i need a code to generate button at runtime having its text displayed from database using array concept.
in my store procedure i have id and description and i need to display the description on runtime generated button.

Thanks,
suhasini

suhasinishinde
Newbie Poster
6 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Hello, to generate Controls in runtime see the the threads, which were discussed not really much time ago here, on Daniweb:
C# Preforming Caluclations With Controls Created In Runtime
C# Cannot Read Dynamic Textbox

For using stored procedures:
The C# Station ADO.NET Tutorial: Using Stored Procedures

Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Use this code here

private void AddFields()
        {
            if (ButtonIndex != 11)
            {
                Button field = new Button();
                field.Name = "field" + ButtonIndex.ToString();
                ButtonIndex++;
                field.Size = new Size(200, 20);
                field.Location = new Point(10,10);
            }

You will need to add this where you declare your variables

public int ButtonIndex = 1;

If you are creating more then one at diffrent locations then you should use a panel and under properties use the flowdirection property, this will mean it will naturlly flow whatever direction you set it at instead of coding for each button location, you will need to use the following code and replace it with the one that specifies location.

panel1.Controls.Add(field);

Also if you are using more than one and you want to cap how many to generate add this under the if statement

else
            {
                MessageBox.Show("A Maximum of 10 Areas is Supported");
            }

Hope this fixes your problem.

wingers1290
Junior Poster in Training
89 posts since May 2009
Reputation Points: 12
Solved Threads: 0
 

hey wingers1290, you have to make a citation at least, i remember those codes from some posts that i post to help you, you could post the original instead of chaning the names, avoid plagiarism.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

sorry

wingers1290
Junior Poster in Training
89 posts since May 2009
Reputation Points: 12
Solved Threads: 0
 
Hello..... i tried this code.... bt not able to generate fields..... pls help....


:(

bk_bhupendra
Newbie Poster
22 posts since Oct 2009
Reputation Points: 8
Solved Threads: 1
 

Ok, firstly you need to retrieve the lsit of button ID's and Descriptions from the database, check out the link Antenka gave you or google for one of the many many tutorials on c# database access.

You said you want to use an array, i'm assuming that will be to store the details of the buttons. Once you have your array you can create the buttons using code similar to that given earlier:

string[] Descriptions = { "One", "Two", "Another Description", "Desc." };

            for( int i = 1; i<=Descriptions.Length;i++)
            {
                Button btn = new Button();
                btn.Name = "btn" + i.ToString(); //btn1, btn2, etc
                btn.Text = Descriptions[i - 1];
                panel1.Controls.Add(btn);
            }
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You