Dynamically adding controls to User Control Windows Application Visual Studio 2008 3.5 c#

I am trying to add new controls (link labels) dynamically to a User Control's panel. I am thinking of creating a public method on the User Control class which would aid in creating these new link labels. The trick I am having a hard time figuring out is how to gain access to the Click Events after these link label controls are created. Can anyone point me in a direction in which this can be accomplished?
ImInOrlandoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SwissKnifeCommented:
Hello ImInOrlando

If you add a button to form and double click on it, you have a Click handler function, e.g.

     private void button1_Click(object sender, EventArgs e)     {
        // your code
     }

Now you can look in the YourForm.designer.cs file. Open the
       #region Windows Form Designer generated code

some where you will find the line
            this.button1.Click += new System.EventHandler(this.button1_Click);

This line adds the handler function to the control. You can do it the same way in your code.

If you do not need the handler anymore, you can remove it with
            this.button1.Click -= new System.EventHandler(this.button1_Click);

Good Luck
SwissKnife

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ImInOrlandoAuthor Commented:
Thanks SwissKnife

The problem I am having is when a control is added to a User Control dynamically, then how can you wire up a separate delegate for each of those 'new' controls? I am thinking you would need an EventHandler List or something but I can't find any examples anywhere.

The project basics:
I have a User Control in which I want to drop on various WinForms
I place the User Control on one WinForm and plan to add link labels to a panel
    on the User Control dynamically
Then wire the delegates (Click Event) for those new link label controls so I can do different
   code inside each link label Click Event.
0
SwissKnifeCommented:
Hello ImInOrlando

Each control handles the event handler list you are looking for by it self.  Look at the += operator and the -= operators in my example. You can even add the same delegate twice and it is called twice for each click.

As long you have a reference to a control you can add a handler.
My succestion: add the handler to the control and then add the control to the userControl, and it will work as you expect.
0
ImInOrlandoAuthor Commented:
Thanks for your assistance SwissKnife!

This is what I ended up doing to solve this issue. I am placing the code here for others to benefit!

I needed a solution for dynamically adding link labels to a User Control's panel container and wiring up the Click Event to create a different application path (Show a different form in Win Form App) for each link.


My User Control Code:
public partial class MyUserControl : UserControl
{
        //Event and Delegate declaration
        public event EventHandler LinkLabelOne_Click;
             
        public MyUserControl()
        {
            InitializeComponent();
        }            
 
        public void AddItem(int nPanel, string lnkText)
        {
            //From the form where the User Control is dropped
            //this gives a quick and easy start to create identical 
            //link labels for a given panel container
            LinkLabel lnkNew = new LinkLabel();
            lnkNew.Location = new Point(30, 40); //change y per each
            lnkNew.Size = new Size(117, 13);
            lnkNew.Text = lnkText;
            lnkNew.Name = "linkLabel3"; //need dynamic name generator
            lnkNew.Enabled = true;
            lnkNew.Visible = true;
            lnkNew.ActiveLinkColor = Color.DarkOrange;
            lnkNew.LinkColor = Color.Green;
            lnkNew.VisitedLinkColor = Color.DarkGreen;
            lnkNew.Click += new EventHandler(linkLabel_Click);
            splitContainer2.Panel1.Controls.Add(lnkNew);            
        }
 
        private void linkLabel_Click(object sender, EventArgs e)
        {
            if (LinkLabelOne_Click != null)
            {
                LinkLabelOne_Click(sender, e);
            }
        }
}
 
In InitializeComponent() in Form where User Control is dropped:
Add:
MyUserControl1.LinkLabelOne_Click += new EventHandler(LinkLabel1_Click);
 
Click Event to determine which Link Label has been clicked:
private void LinkLabel1_Click(object sender, EventArgs e)
{
    //Through Delegate from UserControl
    //we have Local Click Event Control to insure link
    //navigates where we want            
    LinkLabel lnkClicked = (LinkLabel)sender;
    string lnkName = lnkClicked.Name;
    if (lnkName == "linkLabel1") 
    {
        MessageBox.Show("Yes! Local Link1 Click Event Called!");
    }
    else if (lnkName == "linkLabel2")
    {
        MessageBox.Show("Yes! Local Link2 Click Event Called!");
    }
    else if (lnkName == "linkLabel3")
    {
        MessageBox.Show("Yes! Local Link3 Click Event Called!");
    }
}

Open in new window

0
SwissKnifeCommented:
I have not posted the final solution, but I think I have added important input to the solution.

As I have suggested, ImInOrlando adds an EventHandler to the control and then adds the control to the user control.
ImInOrlando has added only the EventHandler(..) call, that must be used to add delegates from a different class.

I think ImInOrland can refund only 250 points and has to accept at least assistance for his problem. He even thanks for assitance in a comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.