C# C# Window Resizing: Program Troubleshooting

  • Thread starter Thread starter TheDemx27
  • Start date Start date
  • Tags Tags
    Window
Click For Summary
The discussion centers on issues with a Windows Forms application where button resizing is not functioning as expected. The primary concern is that the Resize() method, which adjusts button dimensions and positions based on the form's size, is only called during initialization and not when the form is resized. It is suggested that the developer should handle the Form's resize event by creating an event handler, such as Form1_Resize, to invoke the Resize() method whenever the window is resized. Additionally, using the Anchor property in Visual Studio's GUI Designer is recommended for automatic resizing of buttons. The conversation highlights the importance of understanding event-driven programming in GUI applications, emphasizing that proper event handling is crucial for dynamic UI behavior.
TheDemx27
Gold Member
Messages
169
Reaction score
13
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:
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()
        {
            InitializeComponent();
            Resize();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }
}
 
Technology news on Phys.org
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.
 
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.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.resize(v=vs.110).aspx
 
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
)
(http://msdn.microsoft.com/en-us/library/system.windows.sizechangedeventhandler(v=vs.100).aspx)

The SizeChangeEventArgs parameter contains information about whether the width or height has changed, along with information about the old and new sizes.
 
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/
 
Mark44 said:
The Button class has a slew of event defined in it.

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:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
13K
  • · Replies 9 ·
Replies
9
Views
3K