Can't get scrollbars to show up

G

Guest

I'm doing some custom drawing to a Panel object overriding the onPaint, and
the stuff I draw usually goes beyond the ClipRectangle of the panel's current
size- so I set autoscroll to true yet it never shows scrollbars. I also tried
putting my custom Panel inside of a regular Panel with Autoscroll true andf
it still does not work.

What can I do to make scrollbars appear? Is there a size setting I have to
change on my Panel so that it knows I drew beyond the ClipRectangle?
 
N

Nicholas Paldino [.NET/C# MVP]

MrNobody,

The documentation for the ScrollableControl class states:

To manually override which scroll bars are visible, set the VScroll and
HScroll properties. If either property is set to false, the corresponding
scroll bar is not visible, even if the AutoScroll property is set to true.

Have you set these manually? Since you are painting the control
yourself, it doesn't know if you have to scroll the contents or not.

What you might want to do is host a panel control in your control which
has a docking of fill. Then, when you have to redraw the panel, you resize
your inner pannel to the appropriate size. When you do that, the parent
control should show or hide the scrollbars appropriately.

Hope this helps.
 
G

Guest

Nicholas, Thank you for the reply!

I tried that VScroll property and it didn't seem to have an effect, either
with Autoscroll on or off.

I'm not quite sure what you mean by having an inner panel, when I add a
panel to my custom panel then it overwrites whatever I draw with blank space.

Here's a simplified version of my problem:

public class MyPanel : Panel {

public MyPanel () : base () {
this.VScroll = true;
this.AutoScroll = true;
}

protected override void OnPaint(PaintEventArgs e) {
base.OnPaint (e);

Pen p = new Pen(Brushes.Blue, 2);
e.Graphics.DrawLine(p, 50, 0, 50, 250);
}

}

And then on the main form do this:

MyPanel panel = new MyPanel();
panel.Size = new Size(100, 150);
panel.Location = new Point(50,50);
panel.BorderStyle = BorderStyle.FixedSingle;
panel.BackColor = Color.White;
this.Controls.Add(panel);

So the main panel gives it a size smaller than what it draws (the line
extends beyond the bottom clip boundary). Nothing I do seems to get
scrollbars to show up
 
N

Nicholas Paldino [.NET/C# MVP]

MrNobody,

Create a custom control which derives from Panel. Then, in the
constructor, create an inner pannel control which is a child of your custom
control. The inner pannel control will be placed at 0, 0 in the parent
control. Then, the inner pannel will be the panel that paints itself, and
sizes itself according to what you have to paint. When you resize the inner
panel, it will cause the scrollbars to appear or disappear according to the
size of the inner pannel.
 
G

Guest

I must be misunderstanding something because I cannot seem to get the
behavior you describe doing the steps you listed. Create a custom control
which does it's own drawing but in it's consutructor create another instance
of itself? Would you be able to show me what you mean via pseudo code or just
regular code?
 
N

Nicholas Paldino [.NET/C# MVP]

MrNobody,

You have a custom control derived from Panel. You add another Panel
(not your custom control) in the constructor, and paint in that. Something
like this:

public class MyPanel : Panel
{
// The internal panel that is drawn on.
private Panel drawingPanel;

public Panel()
{
// Set the auto scroll property to true.
this.AutoScroll = true;

// Create the panel.
drawingPanel = new Panel();

// Add it.
this.Controls.Add(drawingPanel);

// Set the location of the panel to the origin, so that it looks
like painting on the panel takes place on the
// control.
drawingPanel.Location = new Point(0, 0);

// Set the size of the inner panel here.
// You can also set the size anywhere else that you wish. When you
adjust the size of the
// drawing panel, this control should show the scrollbars if the
size of the inner panel exceeds the size of
// this control. You might also have to set the Dock property of
the drawingPanel to the top left corner.
drawingPanel.Size = ...
}

private void DrawingPanelPaintEventHandler(object sender,
PaintEventArgs)
{
// Handle the painting here.
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top