UserControl and scroll bars

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
I'm trying to implement scroll bars in a user control. I'm doing all the painting on the control myself. I'm wondering if there is some trick that allows me to get the scroll bars for free. Or do I need to add VScrollbar and HScrollbar to my control and code it up myself. I haven't done this before, so I just want to avoid doing it the hard way. Thanks.
 
The AutoScroll property will automatically create scroll bars if a control exists outside the visible area of the container when set to True. If you have no controls outside the visible area of the UserControl then this can't help you. A cheat could be to place a 1x1 control with a transparent background colour at the bottom right corner of what you consider the UserControl's area. If this is outside the visible area then AutoScroll will do the work for you. That is a bit dodgy mind you.
 
Ok, so I'm going to go ahead and code up the scroll bars, and not use the auto scroll features.

Here's what I have so far:

ScrollableControl class that is a user-control. It contains three panel classes. Two panels hold scroll bars using Dock.Fill, and the third panel is called ClientPanel, which contains another user control called CustomTextControl. ScrollableControl currently only handles the resize event (resizes the scroll bars and the client area), and the Scroll event to capture any scrolling. Most of the real work is handled in the main CustomTextControl. Now, here's my question. When CustomTextControl adds/removes lines, that should cause ScrollableControl to update the scroll bars Maximum property. Should CustomTextControl raise a custom event to pass this message, i.e. something like LinesChanged or something? And, when the ScrollableControl class handles a Scroll event, the CustomTextControl needs to be notified that the visible region has changed (i.e. VScrollBar1.Value has chnaged). Should this be also raised as a custom event? Or should it just be passed down through the class hierarchy?

In general I guess my question is, when a child has some piece of info, and needs to notify the parent, what is the correct mechanism? And when a parent container receives some event, how should it notify a child?

Thanks...
 
The best way to handle this sort of thing is to assume that the parent knows evetything about the children and the children know nothing about the parent or each other. That means that if the parent wants affect a child it should access the child's members directly, i.e. either get or set a property or else call a method. If a child wants to affect the parent then it should do so indirectly, i.e. the child raises an event that the parent handles and then it's up to the parent to do what's required. That may include affecting another child. That way each child only has to worry about looking after its own little piece of the world and the aprent takes care of all coordination of children. Everything is kept as clean as possible that way.
 
Back
Top