471,086 Members | 1,059 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

Scrollbar mystery

I have a mystery where my scrollbar behaves differently from when I use it's
slider/arrow keys compared to when I manipulate it's value with a
onMouseWheel event...

When I manipulate my scrollbar's controls (the slider or the arrow key) to
go to the bottom, Once it reaches the bottom and cannot go any further I
somehow lose 9 pixels of height, so when it refreshes my image the bottom 9
pixels are clipped off.

But if I use my MouseWheel event and wheel down to the bottom, it lines up
perfectly. The MouseWheel event just takes the Delta and modified the
scorllbar.Value property directly, not allowing it to go max or < min.

So why does this happen?

The only properties I change are the scrollbar's Maximum and Minimum values,
where Minimum is 0 and Maximum is my image's height subtracted by the height
of the client size rectangle. Everything else left at default.

Nov 2 '06 #1
3 5733
I created a small sample app which demonstrates the behavior I am talking
about, since it's really hard for me to explain in text.

create a WindowsApplication and add my control like this:

ScrollControl s = new ScrollControl();
s.Location = new Point(10, 10);
s.Size = new Size(250, 250);
this.Controls.Add(s);

here is the control itself:

public partial class ScrollControl : UserControl
{
Bitmap bmp;

public ScrollControl()
{
InitializeComponent();

bmp = new Bitmap(400, 400);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Black, 0, -vScrollBar1.Value, 400, 400);
Pen p = new Pen(Brushes.Red, 10);
g.DrawRectangle(p, 0, -vScrollBar1.Value, 400, 400);
g.Dispose();

vScrollBar1.Scroll += new ScrollEventHandler(vScrollBar1_Scroll);
}

protected override void OnPaint(PaintEventArgs e)
{
Point loc = new Point(0, -vScrollBar1.Value);
e.Graphics.DrawImageUnscaled(bmp, new Rectangle(loc, bmp.Size));
}

public override void Refresh()
{
this.OnPaint(new PaintEventArgs(this.CreateGraphics(), new
Rectangle(new Point(0, 0), this.ClientSize)));
}

protected override void OnSizeChanged(EventArgs e)
{
vScrollBar1.SetBounds(ClientRectangle.Right - vScrollBar1.Width,
0, vScrollBar1.Width, ClientRectangle.Height);
vScrollBar1.Maximum = bmp.Height - ClientSize.Height;
}

void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
Refresh();
}

protected override void OnMouseWheel(MouseEventArgs e)
{
int move = e.Delta / 10 * -1;
if (move 0)
{
if (vScrollBar1.Value <= vScrollBar1.Maximum &&
vScrollBar1.Value + move >= vScrollBar1.Maximum)
vScrollBar1.Value = vScrollBar1.Maximum;
else
vScrollBar1.Value += move;
}
else
{
if (vScrollBar1.Value >= vScrollBar1.Minimum &&
vScrollBar1.Value + move <= vScrollBar1.Minimum)
vScrollBar1.Value = vScrollBar1.Minimum;
else
vScrollBar1.Value += move;
}
Refresh();
}
}
Nov 2 '06 #2
int move = e.Delta * SystemInformation.MouseWheelScrollLines / 120;

"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:9D**********************************@microsof t.com...
>I created a small sample app which demonstrates the behavior I am talking
about, since it's really hard for me to explain in text.
protected override void OnMouseWheel(MouseEventArgs e)
{
int move = e.Delta / 10 * -1;
if (move 0)
{
if (vScrollBar1.Value <= vScrollBar1.Maximum &&
vScrollBar1.Value + move >= vScrollBar1.Maximum)
vScrollBar1.Value = vScrollBar1.Maximum;
else
vScrollBar1.Value += move;
}
else
{
if (vScrollBar1.Value >= vScrollBar1.Minimum &&
vScrollBar1.Value + move <= vScrollBar1.Minimum)
vScrollBar1.Value = vScrollBar1.Minimum;
else
vScrollBar1.Value += move;
}
Refresh();
}
}


Nov 3 '06 #3


"Alex Fu" wrote:
int move = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
Ah, that makes the mouse wheel scrolling alot smoother, thanks!

Just that, it didnt solve the problem with the normal scroll bar control...

notice how when you you the mouse wheel to go to the very bottom of the
scroll area, you will be able to see the bottom red border?

now try doing the same thing using the scroll bar control, either the down
arrow or the thumb tab. It wont let you each the actual bottom so you can
never see that bottom red border...

why is that?
Nov 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Marion | last post: by
4 posts views Thread by Price Brattin | last post: by
9 posts views Thread by kjs | last post: by
reply views Thread by h-h | last post: by
2 posts views Thread by kurt sune | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.