1

I have a new application like manager for cafe and I create an User Control for my application. My User Control's name is TableButton. It include Picture Box and Label and have three properties( ID, Name, Status).

When I add it in my form (with loop) and click it, it send to event data I do not want ( object sender is Picture Box or label depending on whether I click on a Picture Box or label. and I can't get data(ID, Name, Status). I want when I click anywhere in my TableButton then object sender is TableButton and I can get data from it.

So what's the solution for that? My English is not good so maybe my article is wrong and difficult to understand. I hope you will skip this inconvenience. Thank you so much.

2
  • Make all controls you want to access internal (oe public) ! By default they all are private so you can't access them from outside. Not sure what you mean by those properties, though.. Commented Jul 14, 2019 at 8:47
  • 1
    You should be able to access those properties my navigating to the senders parent container. Commented Jul 14, 2019 at 9:01

1 Answer 1

0

I want when I click anywhere in my tablebutton then object sender is tablebutton and i can get data from it.

If you subscribe a handler to child control's Click event and fire user control's Click in the handler you will get the affect you want. You can do it:

public partial class TableButton : UserControl
{
    class TableButtonControlCollection : ControlCollection
    {
        TableButton owner;

        public TableButtonControlCollection(TableButton owner) : base(owner)
        {
            this.owner = owner;
        }

        public override void Add(Control value)
        {
            base.Add(value);
            value.Click += Value_Click;
        }

        private void Value_Click(object sender, EventArgs e)
        {
            owner.OnClick(EventArgs.Empty);
        }
    }

    protected override ControlCollection CreateControlsInstance()
    {
        return new TableButtonControlCollection(this);
    }
}

Add this snippet into you TableButton class

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.