Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,
please help me out.how to add events in a dynamically created hyperlink control in c#.net

thanks,
rumki
Posted

C#
HyperLink hyperLink = new HyperLink();
hyperLink.Click += OnHyperLinkClick;
 
Share this answer
 
Comments
Yusuf 21-Feb-11 15:57pm    
nice and sweet. Good answer
Assuming the control is Button and its parent is Panel:

C#
Panel parent = new Panel();
//...

Button myButton = new Button();
myButton.Text = "My &Button";
myButton.Width = //...
myButton.Left = //...
myButton.Top = //...

parent.Controls.Add(myButton);

//now, the event; you don't need a separate method for it, use anonymous:
MyButton.Click += delegate(object sender, KeyEventArgs eventArgs) {
    Button buttonSender = (Button)sender; //can be sure
    //call whatever you want, using buttonSender and/or evenArgs or not
}

//easier way if you have C# v.3 or later, use lambda form:
MyButton.Click += (sender, eventArgs) => {
    Button buttonSender = (Button)sender; //can be sure
    //call whatever you want, using buttonSender and/or evenArgs or not
}
//if this form, you don't need argument types, they are inferred from the even type.


For motivation and discussion, see also how to call keydown event on particular button click[^].

—SA
 
Share this answer
 

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