Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I m developing food application in that depends upon my database value's i have to get the dynamic button and also event handler..if any one knows just help me!!!
Posted

1 solution

You can create as many buttons as you need in Windows Forms.
Suppose that you already queried those values in your database and that you have them in a DataTable called dtButtonsToCreate with a single column, say 'fruits'.
To create as many buttons as needed do:
C#
for (int i = 0; i < dtButtonsToCreate.Count; i++)
{
      Button b = new Button();
      b.Click  += new EventHandler(OnButtonClick);
      this.Controls.Add(b);
//you will also need to add some code to accurately size and place the button. You can
//do that based on the amount of controls there are with dtButtonsToCreate.Count
} 


you need to have a method in advance for the event handler as seen above.
THIS NEEDS TO BE IN YOUR CODE IN ADVANCE (can't be created dynamically):
C#
private void OnButtonClick(object sender, EvenArgs e)
{
     //your code for the event.
}




BUT WAIT!
If you need different buttons, you must need them to do different things.
In this way every button would trigger the exact same action...

Let's solve that!
Create a new class called ExtendedButton and inherit from the button class:

C#
using System.Windows.Forms

namespace blah
{
    class ExtendedButton : System.Windows.Forms.Button //this inherits from button
    {

        //constructor
        public ExtendedButton()
        {  /*you dont need to do anything here*/  } 


        //add public property
        private string myval;
        public string _myval
        {
          get{ return myval; }
          set{ myval = value; }
        }
    }
}



Now do the same code you did at the beginning but with variation:
C#
for (int i = 0; i < dtButtonsToCreate.Count; i++)
{
      ExtendedButton b = new ExtendedButton(); //with ExtendedButton this time
      b._myval = dtButtonsToCreate.Rows[i][0].ToString();//this asigns the fruit name to the extended button
      b.Click  += new EventHandler(OnButtonClick);
      this.Controls.Add(b);
//you will also need to add some code to accurately size and place the button. You can
//do that based on the amount of controls there are with dtButtonsToCreate.Count
} 


There after, you can get the value of the button to perform anything you need in the event handler:
C#
private void OnButtonClick(object sender, EvenArgs e)
{
     string TheFruit = ((ExtendedButton)sender)._myval;
     //your code for the event.
}



Hope this helps!

Should you need this in ASP.Net, you can pretty much do the same but adhere to the examples in this link:
http://msdn.microsoft.com/en-us/library/ms743596.aspx[^]
 
Share this answer
 
v2
Comments
ganesh89_babu 28-Sep-13 2:49am    
Thank you Homero
ganesh89_babu 28-Sep-13 2:50am    
if you have some other idea also just inform me i m waiting !!!!
Homero Rivera 28-Sep-13 2:51am    
My pleasure! Did it solve your problem?
Sergey Alexandrovich Kryukov 28-Sep-13 3:12am    
Cool down, please. Why someone owe you "some other idea". What's wrong with this one?
—SA
ganesh89_babu 28-Sep-13 2:53am    
i m fresher na that's y now struggling but till now it doesnt came perfect output!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900