1
votes

I created a custom user control in a c# forms application to contain a groupbox, a checkbox, and a button.

In my main app, I'm able to add these controls to a flow layout panel and set their initial values.

Problem is, how do I access the button event and the checkbox after the item is already in the flow layout panel?

private void btnAdd_Click(object sender, EventArgs e)
{
    AttributeListItem.AttributeListItem at = new AttributeListItem.AttributeListItem();
    at.groupbox.Text = lbxLDAPFields.GetItemText(lbxLDAPFields.SelectedItem);
    flPanel.Controls.Add(at);
    // button name is btnEdit
}
2
Sorry, I was not clearer. I'm adding a bunch of these into a flow layout panel. I didn't notice the public feature on the components; I over rode the component itself in code. That is a lot easer; thanks for pointing that out. From my understanding, each of those custom controls will have a unique event? Do I have all of those events point to one handler in my calling page and check to see what button was clicked? I'm not really clear on how that is handled... Thanks!Zonus
Oh wait, it looks like Mark Hall's example shows this. Let me work with it a bit!... excited. Thanks!Zonus
I just made a change to my code, noticed that I forgot to enclose the finding of the control in an eventhandler.Mark Hall

2 Answers

1
votes

Use Events and public properties, Since it sounds like you are adding each item in the designer you can then hookup you eventhandlers and access your properties in your usercontrol assigning a name to it so you can locate it later. This is a very rough example see it will work for you.

UserControl

public partial class MyCustomUserControl : UserControl
{
    public event EventHandler<EventArgs> MyCustomClickEvent;
    public MyCustomUserControl()
    {
        InitializeComponent();
    }
    public bool CheckBoxValue
    {
        get { return checkBox1.Checked;}
        set { checkBox1.Checked = value; }
    }
    public string SetCaption
    {
        get { return groupBox1.Text;}
        set { groupBox1.Text = value;}
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MyCustomClickEvent(this, e);

    }
}

Form1

public partial class Form1 : Form
{
    int count =1;
    public Form1()
    {
        InitializeComponent();
    }

    private void mcc_MyCustomClickEvent(object sender, EventArgs e)
    {
        ((MyCustomUserControl)sender).CheckBoxValue = !((MyCustomUserControl)sender).CheckBoxValue;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MyCustomUserControl mcc = new MyCustomUserControl();
        mcc.MyCustomClickEvent+=mcc_MyCustomClickEvent;
        mcc.Name = "mmc" + count.ToString();
        mcc.SetCaption = "Your Text Here";
        flowLayoutPanel1.Controls.Add(mcc);
        count += 1;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        var temp = this.Controls.Find("mmc1", true);
        if (temp.Length != 0)
        {
            var uc = (MyCustomUserControl)temp[0];
            uc.SetCaption = "Found Me";
        }    
    }
}
0
votes

Dirty and easy solution: make UserControl controls public

enter image description here

Then you can do something like

userControl1.button1.PerformClick();

P.S.: btw, you are already accessing groupbox in your example, so it looks like you know about this. Do you create controls programmatically (in user control constructor perhaps)? Then you can make FlowLayoutPanel public and use its Control collection to find needed control or you can keep controls instances in the public field/property of your UserControl.