locked
Access controls in a composite control RRS feed

  • Question

  • I am learning to build a composite control. I want to know how to access to the event handlers and the properties of the sub-controls. Do I need to rewrite them by myself? For instance I have a button and a Textbox in a composite control. How do I access to the click event of the button in my composite  control?
    Friday, November 13, 2020 7:55 PM

Answers

  • Yes, the properties of the button will be hidden from the users of your composite control unless you choose to republish any of them.

    You could republish the whole of the button through a property of type Button, and then it would be accessible as yourControl.TheButton.TheProperty. But I do not recommend this. It breaks the encapsulation, since it exposes the internal details of your control and therefore stops you from changing the internal structure of the control without affecting the users of the control.

    • Marked as answer by antidinosaur Sunday, November 15, 2020 3:15 AM
    Saturday, November 14, 2020 7:53 AM

All replies

  • The way that you normally do it is that you re-republish the event. In your control, you declare a public event. Then, you add a handler for the click of the button, and inside the handler you raise your public event.
    Friday, November 13, 2020 8:47 PM
  • Thank you very much! And:

    Does it mean that the original public properties of the button also become private and need to be republished? 

    Friday, November 13, 2020 10:09 PM
  • Yes, the properties of the button will be hidden from the users of your composite control unless you choose to republish any of them.

    You could republish the whole of the button through a property of type Button, and then it would be accessible as yourControl.TheButton.TheProperty. But I do not recommend this. It breaks the encapsulation, since it exposes the internal details of your control and therefore stops you from changing the internal structure of the control without affecting the users of the control.

    • Marked as answer by antidinosaur Sunday, November 15, 2020 3:15 AM
    Saturday, November 14, 2020 7:53 AM