Tuesday, March 1, 2011

Interacting with User Controls through Events

I've browsed through the questions on the site, but haven't found a decent answer yet.

My problem is that my main form class is cluttered due to all the events and methods.

I decided to break the form into user controls. Unfortunately I am having trouble accessing information from other User controls.

For example I have a user control that prints some information. But some of that information is in another User Control. How would I retrieve that data?

One solution I read was to pass a delegate to the Print User control. The delegate would contain a method from the Information User Control that returned the data from the control. So whenever the print event is called I would call that function.

The other solution I had was to attach an event to the information User Control. Whenever the information in the User Control changed, I would update my print variables.

From stackoverflow
  • Why do you need to use events for this at all? If control X needs information from control Y, then pass control Y into control X's constructor.

  • I asked the question above as an unregistered user, so I can't edit my question.

    The information from control Y changes a lot, which is why I thought I would need to use events.

    If I pass control Y in the constructor of control X, I thought that the information from control Y would be static.

    Would I pass it as a reference? Is this ok to do? I'm a novice programmer, so I'm still trying to wrap my mind the ideas of encapsulation, object oriented design patterns, etc.

    Gohn67 : I was thinking about my problem a bit more. Are you suggesting that I bind my User Control to the other User control?

0 comments:

Post a Comment