Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Windows.Forms: Panel/Button size problem

Started by xeddiex Feb 27, 2007 at 9:17 PM 1 replies 950+ views
Original Post
xeddiex
xeddiex
Language: C# Enviroment: Windows Forms Hi guys. Whenever I try to set the width of my Button to half the width of the container control - Panel - I get a shorter width than the half I wanted.

    class EmployeePanel : Panel
    {
        private Button clockIn;

        public EmployeePanel()
        {
            this.Borderstyle = Borderstyle.Fixed3D;
            clockIn = new Button();
            clockIn.Text = "Clock In";
            clockIn.Size = new Size(this.ClientRectangle.Width / 2, this.ClientRectangle.Height);
            clockIn.Dock = Dockstyle.Left;
            clockIn.Show();

            this.Controls.Add(clockIn);
        }
    }
/* class that creates an instance of the above control: */

    class Class1 : Form
    {
        private EmployeePanel employeePanel;

        public Class1()
        {
            employeePanel = new EmployeePanel();
            employeePanel.Size = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
            this.Controls.Add(employeePanel);
            employeePanel.Show();
        }
    }
Example: Free Image Hosting at www.ImageShack.us Does anyone know why this is happening? Thanks, xeddiex
one..
dalep
dalep
You are setting the width of the button in the panel's constructor, which is invoked before you assign the size to the panel. So the button width is half the default client width of the panel. Since its docked left, its width won't change unless you tell it to.

If you want the button always half the width of the panel, you should size it in the panel's Resize event or the overridden OnSize member function or something like that.
benryves
benryves
You could just put the button inside a TableLayoutPanel and let that handle the resizing for you. [smile]
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.