Link to home
Start Free TrialLog in
Avatar of ImInOrlando
ImInOrlandoFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of SwissKnife
SwissKnife
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ImInOrlando

ASKER

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.
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.
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

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.