tags:

views:

276

answers:

2

What I am trying to achieve is a form that has a button on it that causes the Form to 'drop-down' and become larger, displaying more information. My current attempt is this:

private void btnExpand_Click(object sender, EventArgs e)
{
    if (btnExpand.Text == ">")
    {
        btnExpand.Text = "<";
        _expanded = true;
        this.MinimumSize = new Size(1, 300);
        this.MaximumSize = new Size(int.MaxValue, 300);
    }
    else
    {
        btnExpand.Text = ">";
        _expanded = false;
        this.MinimumSize = new Size(1, 104);
        this.MaximumSize = new Size(int.MaxValue, 104);
    }
}

Which works great! Except for one small detail... Note that the width values are supposed to be able to go from 1 to int.MaxValue? Well, in practice, they go from this.Width to int.MaxValue, ie. you can make the form larger, but never smaller again. I'm at a loss for why this would occur. Anyone have any ideas?

For the record: I've also tried a Form.Resize handler that set the Height of the form to the same value depending on whatever the boolean _expanded was set to, but I ended up with the same side effect.

PS: I'm using .NET 3.5 in Visual Studio 2008. Other solutions are welcome, but this was my thoughts on how it "should" be done and how I attempted to do it.

Edit: Seems the code works, as per the accepted answers response. If anyone else has troubles with this particular problem, check the AutoSize property of your form, it should be FALSE, not TRUE. (This is the default, but I'd switched it on as I was using the form and a label with autosize also on for displaying debugging info earlier)

+1  A: 

As per the docs, use 0 to denote no maximum or minimum size. Tho, I just tried it and it didn't like 0 at all. So I used int.MaxValue like you did and it worked. What version of the the framework you using?

Joel Lucsy
For some reason AutoSize was set to true on the form and that was interfereing with the MaximumSize and MinimumSize... Huh. Oh well. Thanks anyway :)
Matthew Scharley
Sure, no problem. =)
Joel Lucsy
As an extra sidenote, I believe it means to use ie. new Size(0, 0) to represent no min/max, but I don't think the code was designed to handle individual 0 values. A shame, but there you have it.
Matthew Scharley
A: 

Actually, having a look at the MinimumSize and MaximumSize (.NET 3.5) in reflector its pretty clear that the designed behaviour is not quite the same as the docs suggest. There is some minimum width constraints determined from a helper class and 0 has no special meaning (i.e. no limit.

Another note, I see in your code above that you are Expanding or Contracting based upon the text value of your Button, this is a bad idea, if someone comes along later and changes the text in the designer to say, "Expand" instead of < without looking at your code it will then have an unexpected side effect, presumably you have some code somewhere that changes the button text, it would be better to have a state variable somewhere and switch on that.

Tim Jarvis
The fact that there already IS a state variable only serves to highlight this. Thanks. But they'll still need to look at my code when they start clicking the button and it messes up their own changes to the buttons text.
Matthew Scharley