Click here to Skip to main content
16,006,341 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem

I want to increase my form size
so i created a Checkbox to decide Increase/Decrease the size

here is the code

private void ExtandedColorBox(object sender, EventArgs e)
{
      CheckBox CheckBox1 = new CheckBox();      //--> CheckBox1=name of the form
      CheckBox1.Checked = true;

     if (CheckBox1.Checked == true)
     {
         this.MaximumSize = new Size(300, 303); //--> this=Form
         this.Size = new Size(300, 303);
     }

     if (CheckBox1.Checked == false)
     {
         this.MaximumSize = new Size(300, 280);
         this.Size = new Size(300, 280);
     }
}


when i check then the form getting increased but when i check again it's does'nt getting decreased


[edit]Code block added - OriginalGriff[/edit]
because the status of the Checkbox never getting changed to False

Please help :-)
Posted
Updated 9-Jul-11 3:51am
v3
Comments
Sergey Alexandrovich Kryukov 10-Jul-11 2:36am    
Tag it: Forms.
--SA

1 solution

That's because you keep on creating a new checkbox - which does nothing useful for you.
Every time into the method, you create a new checkbox, and give set it's "Checked" property to True. So when you test it to see what you should be doing with teh form size, you always get "Checked" equals True, so you always set the size to 300 by 303.

Instead, delete the
CheckBox CheckBox1 = new CheckBox();
CheckBox1.Checked = true;
lines, and replace CheckBox1 with the name of the actual CheckBox on your form (probably checkBox1, but you may have changed it):
private void ExtandedColorBox(object sender, EventArgs e)
{
     if (checkBox1.Checked)
     {
         this.MaximumSize = new Size(300, 303); //--> this=Form
         this.Size = new Size(300, 303);
     }
     else
     {
         this.MaximumSize = new Size(300, 280);
         this.Size = new Size(300, 280);
     }
}
In addition, you don't need to check for false explicitly - if it isn't true then it is false: I have altered your code to be neater!

BTW: you don't need to keep typing this. either - it is implied in all non-static class methods:
private void ExtandedColorBox(object sender, EventArgs e)
{
     if (checkBox1.Checked)
     {
         MaximumSize = new Size(300, 303);
         Size = new Size(300, 303);
     }
     else
     {
         MaximumSize = new Size(300, 280);
         Size = new Size(300, 280);
     }
}
 
Share this answer
 
Comments
Eddie1987 9-Jul-11 10:11am    
Thank u so much !!!
:-)
OriginalGriff 9-Jul-11 10:20am    
You're welcome!
fjdiewornncalwe 9-Jul-11 10:35am    
Absolutely.
Sergey Alexandrovich Kryukov 10-Jul-11 2:42am    
Correct, a 5. I would not fight with excessive "this." -- it helps Intellisense to help you, but hard-coded size is disaster, as well as the whole approach to switch between two fixed sizes. Also, naming sucks -- should be semantic names without all those "1s".
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900