views:

107

answers:

3

I want to create buttons or list of items on the basis of number of items in my database or from list of items in my array and for each item create a onclick function for either buttons or any list of items

+4  A: 

How about:

int y = 10;
foreach (string name in names)
{        
    Button button = new Button();
    button.Text = name;
    button.Position = new Point(10, y);
    y += 20;
    button.Click += HandleButtonClick;
    Controls.Add(button);
}

You might also store the buttons in an array or a list... there's nothing particularly special about GUI controls that stops you from creating them at execution time just like any other object.

If that doesn't help, please give more information about what you need to do that the above doesn't help you with.

Jon Skeet
I am happy to see it and quite sure its working but let me ask something more about HandleButtonClick .How will i declare function in global scope to pass this one the reference of it. Regard MAk
Mobin
You don't declare the method "in global scope" - the closest is to write a static method. The button which has been clicked will be passed as the "sender" argument in the method. (The signature should be `static void HandleButtonClick(object sender, EventArgs args)` - you could use an instance method if you want though.
Jon Skeet
Thanks a lot sir you had been a great help:D
Mobin
A: 

I have done it also by looking at Visual Studio code.

programmernovice
well the point of asking here is to get a solution not some stupid ideas surely i could do that but thanks for showing off smartness.My advice to you is "A fool looks wise until he's quite"
Mobin
Give some code or something helpful things !!!
anishmarokey
yes thats what i am talking may be a reference to help or something at least where someone can learn from don't just get yourself points for answering things duh
Mobin