Sign in to follow this  
MindWipe

[.net] Form Scrollbar problems

Recommended Posts

Heya chaps! So I'm making this MDI editor. It's child forms should have scrollbars that are enabled always when needed. For the moment I try to load just an image in the forms and if the image's too big the scrollbars get enabled. The image gets loaded correctly. So to get these scrollbars showing I do:
    private void RegionFile_Load(object sender, EventArgs e)
        {
            this.VerticalScroll.Visible = true;
            this.HorizontalScroll.Visible = true;
            SetupScrollbars();
        }

And SetupScrollbars() looks like:
  {
            int xOffset = (int)(regionImage.Width - ClientSize.Width;
            int yOffset = (int)(regionImage.Height - ClientSize.Height;

            if (xOffset > 0)
            {
                this.HorizontalScroll.Enabled = true;
                this.HorizontalScroll.Maximum = xOffset;
                this.HorizontalScroll.Minimum = 0;
                this.HorizontalScroll.SmallChange = 1;
                this.HorizontalScroll.LargeChange = xOffset / 4;
            }
            else
                this.HorizontalScroll.Enabled = false;

            if (yOffset > 0)
            {
                this.VerticalScroll.Enabled = true;
                this.VerticalScroll.Maximum = yOffset;
                this.VerticalScroll.Minimum = 0;
                this.VerticalScroll.SmallChange = 1;
                this.VerticalScroll.LargeChange = yOffset / 4;
            }
            else
                this.VerticalScroll.Enabled = false;
        }

The scrollbars show correct, but the problem is I can't get their values to change. The Scroll event gets called and the scrollbars' values seem to increase at that point, but then get resetted back to zero :S What's going on? And what should I do? Thanks, MindWipe

Share this post


Link to post
Share on other sites
What do you mean they "seem to increase"? How do you know they get reset back to 0? Do you have code in the Scroll event?

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
What do you mean they "seem to increase"? How do you know they get reset back to 0? Do you have code in the Scroll event?


In the Scroll event the scrollbar get the value set to 1 (the ScrollArgument.newValue == 1) but directly afterwards the scrollbar is set to 0 again. For the moment I got no code in the function but I checked the values a while back.

I tried to set the value to something in SetupScrollbars() the value got changed, but no matter what I pressed on the scrollbar it always got set back to 0.

/MindWipe

Share this post


Link to post
Share on other sites
Ok, so try this:

Create a new application. Add following code:

Quote:

private void Form1_Load(object sender, EventArgs e)
{
this.VerticalScroll.Enabled = true;
this.VerticalScroll.Maximum = 20;
this.VerticalScroll.Minimum = 0;
this.VerticalScroll.SmallChange = 1;
this.VerticalScroll.LargeChange = 5;

this.HorizontalScroll.Enabled = true;
this.HorizontalScroll.Maximum = 20;
this.HorizontalScroll.Minimum = 0;
this.HorizontalScroll.SmallChange = 1;
this.HorizontalScroll.LargeChange = 5;

this.VerticalScroll.Visible = true;
this.HorizontalScroll.Visible = true;
}


And try it. See how you can't change the scrollbars.

/MindWipe

Share this post


Link to post
Share on other sites
You must have grabbed the wrong version I uploaded just before I fixed it. Try it again.

The reason yours doesn't work is that there's no controls outside the client area. There's nothing to scroll to. You also need to set the AutoScroll to true. Making the scrollbars visible when there's nothing to scroll to is not necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
You must have grabbed the wrong version I uploaded just before I fixed it. Try it again.

The reason yours doesn't work is that there's no controls outside the client area. There's nothing to scroll to. You also need to set the AutoScroll to true. Making the scrollbars visible when there's nothing to scroll to is not necessary.


I still get the same version when I download it. Ok, noticed how to use the AutoScroll, and if it only works when there's a control outside the client area that makes sense it didn't work.

Cheers!

/MindWipe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this