Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement scrolling for a Custom Control?

How do I implement scrolling for my Custom Control? My control is fully custom drawn and its height is variable, and a part of the control contains a menu so if there are many items in the control, I'll need to be able to put scroll bars there. I've not really been able to find any clues on how to do this. I did see something about ScrollableControl, but I'm still not sure if that's what I need.

Also, how will my control know when it needs to show the scroll bars? Because my control is fully custom drawn so there's no real "controls" in there it's just a bunch of pixels that are drawn onto it so it's not like I can just set AutoScroll to true and I can't do that anyway because it's not the main part of the control that needs scrolling, it's a particular location on the control that will need to have the scrollbars.

like image 399
jay_t55 Avatar asked Apr 15 '26 05:04

jay_t55


2 Answers

If your custom control inherits from the Panel control, you just set the size of the content yourself in the custom control by this setting:

this.AutoScrollMinSize = New Size(yourWidth, yourHeight);

If your control's ClientSize.Height is greater than yourHeight, you won't get any scrollbars. If it's less, then you get a scrollbar.

In your paint method, add this to the beginning:

protected override void OnPaint(PaintEventArgs e) {
  e.Graphics.TranslateTransform(this.AutoScrollPosition.X,
                                this.AutoScrollPosition.Y);

Now everything you paint gets automatically transformed to the scrolling coordinates.

like image 110
LarsTech Avatar answered Apr 16 '26 20:04

LarsTech


You have two options.

The good new is that it is possible and both are not really hard.

  • The bad new is that for one option you will have to adapt all your drawing code:

First make your Control, I use a Panel, to have Autoscroll=true;

Then you need to add one dummy control, I use another Panel, maybe like this, far enough to the right and bottom to force the ScrollBars to show:

public Form1()
{
    InitializeComponent();

    Panel panelDummy = new Panel();
    panelDummy.Size = new Size(1,1);
    panelDummy.Location = new Point(yourMaxX,yourMaxY);
    panel1.Controls.Add(panelDummy);
}

And then you need to adapt your drawing code. Here is how:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    int xx = panel1.HorizontalScroll.Value;
    int yy = panel1.VerticalScroll.Value;

    e.Graphics.FillRectangle(Brushes.Wheat, new Rectangle(11 - xx, 22 - yy, 22, 311));
    e.Graphics.FillRectangle(Brushes.RosyBrown, new Rectangle(11 - xx, 280 - yy, 22, 3));
}

private void panel1_Scroll(object sender, ScrollEventArgs e)
{
    panel1.Invalidate();
}

I have added an Invalidate to the Scroll event to avoid messed up painting results.

  • The other option is simpler: Make your control large enough to hold all your drawn controls and the put it inside a Panel with AutoScroll=true; this will delegate the whole scrolling business to the containing Panel.
like image 26
TaW Avatar answered Apr 16 '26 18:04

TaW