• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4736
  • Last Modified:

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?
0
ImInOrlando
Asked:
ImInOrlando
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now