C# Window Resizing: Program Troubleshooting

  • Context: C# 
  • Thread starter Thread starter TheDemx27
  • Start date Start date
  • Tags Tags
    Window
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C# program designed to resize buttons on a Windows form. Participants explore various methods for ensuring that the button sizes adjust dynamically when the form is resized, addressing both theoretical and practical aspects of event handling in GUI applications.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant notes that explicit resizing may not be necessary if using Visual Studio's GUI Designer, suggesting the use of the Anchor property to allow buttons to resize automatically.
  • Another participant emphasizes the need to handle the resize event by implementing a Form1_Resize method to call the Resize() function whenever the window is resized.
  • A different participant agrees with the previous point, mentioning the importance of handling the SizeChanged event for buttons to ensure they react to size changes.
  • One suggestion is to override the OnResize method of the containing window or panel to manage button resizing more effectively, including forcing the buttons to redraw themselves.
  • A participant comments on the complexity of learning event-driven GUI programming, highlighting the challenge of distinguishing between important and unimportant events in the documentation.

Areas of Agreement / Disagreement

Participants express multiple competing views on how best to implement resizing functionality, with no consensus reached on a single approach. Some advocate for using the Anchor property, while others suggest handling resize events directly.

Contextual Notes

Participants mention various methods and event types without resolving which is the most effective or appropriate for the specific use case. There is also an acknowledgment of the challenges in navigating event-driven programming documentation.

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:
 

Similar threads

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