Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C# Window Resizing

  1. Mar 5, 2014 #1


    User Avatar
    Gold Member

    I have wrote a program to resize buttons and such on my windows form. It doesn't seem to be doing much of anything. When I run the program it just shows me the form and it doesn't react to my resizing of it.
    Code (Text):

    namespace WindowsFormsApplication1
        public partial class Form1 : Form
            private bool playerTurn = false;

            private Image x = Properties.Resources.Image1;
            private Image y = Properties.Resources.Image2;

            private void Resize()
                int defaultWidth = (this.ClientRectangle.Width - radioButton1.Width) / 3;

                    button1.Width = button2.Width =
                    button3.Width = button4.Width =
                    button5.Width = button6.Width =
                    button7.Width = button8.Width =
                    button9.Width = defaultWidth;

                int defaultHeight = (this.ClientRectangle.Height - menuStrip1.Height) / 3;

                    button1.Height = button2.Height =
                    button3.Height = button4.Height =
                    button5.Height = button6.Height =
                    button7.Height = button8.Height =
                    button9.Height = defaultHeight;

                int secondColumn = button1.Width;
                int thirdColumn = 2 * button1.Width;

                    button1.Left = button4.Left = button7.Left = 0;
                    button2.Left = button5.Left = button8.Left = secondColumn;
                    button3.Left = button6.Left = button9.Left = thirdColumn;

                int firstRow = menuStrip1.Height;
                int secondRow = menuStrip1.Height + button1.Height;
                int thirdRow = menuStrip1.Height + 2 * button1.Height;

                    button1.Top = button2.Top = button3.Top = firstRow;
                    button4.Top = button5.Top = button6.Top = secondRow;
                    button7.Top = button8.Top = button9.Top = thirdRow;

                radioButton1.Left = button3.Left + button3.Width + 5;
                radioButton2.Left = radioButton1.Left;
                radioButton1.Top = button3.Top;
                radioButton2.Top = radioButton1.Top + radioButton1.Height + 10;

                    button10.Left = button11.Left = radioButton1.Left;
                    button10.Top = radioButton2.Top + radioButton2.Height + 10;
                    button11.Top = button10.Top + button10.Height + 10;

            public Form1()

            private void Form1_Load(object sender, EventArgs e)
  2. jcsd
  3. Mar 5, 2014 #2


    User Avatar
    Gold Member

    If you are using Visual Studio, explicit resizing should not be necessary. In the GUI Designer of Visual Studio, there is a property on GUI widgets such as buttons, called Anchor. If you want the button to get larger as the window does, anchor it to all four sides of the window (or panel) which contains it.
  4. Mar 5, 2014 #3


    User Avatar
    Science Advisor
    Homework Helper

    Aside from harborsparrow's solution, don't you need to pick up the event that is generated when you resize the window, and call your Resize() function?

    I..e you need to write a function Form1_Resize(object sender, EventArgs e) that will be called when the window is resized.

    I'm not a C# expert, but it I think your code calls your Resize() function when you create the window, but it will never call it again after that.
  5. Mar 6, 2014 #4


    Staff: Mentor

    I agree with AlephZero. You need to handle the event that is fired when the button is resized. By "handle the event" I mean you need to write an event handler, similar to what AlephZero suggested.

    The Button class has a slew of event defined in it. One of them is the SizeChanged event (http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.sizechanged(v=vs.100).aspx).

    The prototype of the event delegate is
    public delegate void SizeChangedEventHandler(
    Object sender,
    SizeChangedEventArgs e

    The SizeChangeEventArgs parameter contains information about whether the width or height has changed, along with information about the old and new sizes.
  6. Mar 6, 2014 #5


    User Avatar
    Gold Member

    If you're going to grind this out and do it the hard way, the preferred way would be to override the OnResize method of the Window or Panel widget that contains the buttons. In there, after resetting the Button sizes, you have to force the Buttons to redraw themselves...here's some further instructions: http://www.csharp-examples.net/redraw-control-on-resize/
  7. Mar 6, 2014 #6


    User Avatar
    Science Advisor
    Homework Helper

    That's the hard part about learning an event-driven GUI. Somehow, you have to figure out which 999 of every 1000 trees in the forest are not important to you.

    The reference documentation doesn't usually help, because (by definition) it should include descriptions of everything. Finding a good tutorial is often the best way, but the problem is, you can't tell if a tutorial is good unless you already know the subject :cry:
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook