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

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.

Comments
Good job

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.

Hello.....
i tried this code....
bt not able to generate fields.....
pls help....

:(

Comments
This is old thread. Start a new thread.

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);
            }
This article has been dead for over six months. Start a new discussion instead.