Windows Develop Bookmark and Share   
 index > Windows Forms General > how to resize button like visual studio design?
 

how to resize button like visual studio design?

hai all,

i want to make my button can be resizeable at runtime.how to make that? please give me a sample code or reference...!!
muhamad nizar iqbal  Thursday, September 24, 2009 9:55 AM
I changed some code. Hope this one works

    public class MyButton : System.Windows.Forms.Panel
    {
        public MyButton()
            : base()
        {
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(MyButton_MouseDown);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(MyButton_MouseUp);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(MyButton_MouseMove);
            this.Resize += new EventHandler(MyButton_Resize);
            this.Cursor = Cursors.SizeNWSE;
            this.Controls.Add(_Button);
            _Button.Cursor = Cursors.Hand;
            this.BackColor = Color.Red;
        }

        void MyButton_Resize(object sender, EventArgs e)
        {
            _Button.Location = new Point(2, 2);
            _Button.Size = new Size(this.Width - 4, this.Height - 4);
        }
        private Button _Button = new Button();
        private int x;
        private int y;
        public Button Button
        {
            get { return _Button; }
            set { _Button = value; }
        }

        private bool allowResize = false;
        private int mousex;
        private int mousey;
        void MyButton_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (allowResize)
            {
                if (this.Width / 2 > e.X)
                {
                    if (this.Height / 2 < e.Y)
                    {
                        this.Left = this.Left + (e.X - mousex);
                        this.Height = e.Y;
                        this.Width = this.Width - (e.X - mousex);
                    }
                    else
                    {
                        this.Left = this.Left + (e.X - mousex);
                        this.Top = this.Top + (e.Y - mousey);
                        this.Width = this.Width - (e.X - mousex);
                        this.Height = this.Height - (e.Y - mousey);
                    }
                }
                else
                {
                    if (this.Height / 2 < e.Y)
                    {
                        this.Width = e.X;
                        this.Height = e.Y;
                    }
                    else
                    {
                        this.Top = this.Top + (e.Y - mousey);
                        this.Width = e.X;
                        this.Height = this.Height - (e.Y - mousey);
                    }
                }
            }
        }

        void MyButton_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            allowResize = false;
        }

        void MyButton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            allowResize = true;
            mousex = e.X;
            mousey = e.Y;
        }
    }


Tamer Oz  Thursday, September 24, 2009 2:07 PM
Do you want end user to resize it from gui or you want to resize it by code?
Tamer Oz  Thursday, September 24, 2009 10:07 AM
i want end user to resize button from gui
muhamad nizar iqbal  Thursday, September 24, 2009 10:58 AM
You should write your own control.

Here is a sample I made 2-3 years ago.

The main idea is putting button into a panel that 2 pixel larger than button and listening panels mousedown mouseup and mousemove events. And resizing button with panel.

Hope it helps.
    public class MyButton : System.Windows.Forms.Panel
    {
        public MyButton()
            : base()
        {
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(MyButton_MouseDown);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(MyButton_MouseUp);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(MyButton_MouseMove);
            this.Resize += new EventHandler(MyButton_Resize);
            this.Cursor = Cursors.SizeNWSE;
            this.Controls.Add(_Button);
            _Button.Cursor = Cursors.Hand;
            this.BackColor = Color.Red;
        }

        void MyButton_Resize(object sender, EventArgs e)
        {
            _Button.Location = new Point(2, 2);
            _Button.Size = new Size(this.Width - 4, this.Height - 4);
        }
        private Button _Button = new Button();

        public Button Button
        {
            get { return _Button; }
            set { _Button = value; }
        }

        private bool allowResize = false;

        void MyButton_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (allowResize)
            {
                this.Width = e.X;
                this.Height = e.Y;
            }
        }

        void MyButton_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            allowResize = false;
        }

        void MyButton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            allowResize = true;
        }
    }
Tamer Oz  Thursday, September 24, 2009 11:27 AM
i can not find
this
.Cursor = Cursors.SizeNWSE;
Button.Cursor = Cursors.Hand;
muhamad nizar iqbal  Thursday, September 24, 2009 12:24 PM
now i can resize my button but when my button resize the button position change to top left. can you solve my problem?.
muhamad nizar iqbal  Thursday, September 24, 2009 12:35 PM
now i can resize my button but when my button resize the button position change to top left. can you solve my problem?.
i can resolve that problem.but another problem is when itry to resize button from the left, the button can not be resize. do you solve this problem?.
muhamad nizar iqbal  Thursday, September 24, 2009 1:01 PM
I changed some code. Hope this one works

    public class MyButton : System.Windows.Forms.Panel
    {
        public MyButton()
            : base()
        {
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(MyButton_MouseDown);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(MyButton_MouseUp);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(MyButton_MouseMove);
            this.Resize += new EventHandler(MyButton_Resize);
            this.Cursor = Cursors.SizeNWSE;
            this.Controls.Add(_Button);
            _Button.Cursor = Cursors.Hand;
            this.BackColor = Color.Red;
        }

        void MyButton_Resize(object sender, EventArgs e)
        {
            _Button.Location = new Point(2, 2);
            _Button.Size = new Size(this.Width - 4, this.Height - 4);
        }
        private Button _Button = new Button();
        private int x;
        private int y;
        public Button Button
        {
            get { return _Button; }
            set { _Button = value; }
        }

        private bool allowResize = false;
        private int mousex;
        private int mousey;
        void MyButton_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (allowResize)
            {
                if (this.Width / 2 > e.X)
                {
                    if (this.Height / 2 < e.Y)
                    {
                        this.Left = this.Left + (e.X - mousex);
                        this.Height = e.Y;
                        this.Width = this.Width - (e.X - mousex);
                    }
                    else
                    {
                        this.Left = this.Left + (e.X - mousex);
                        this.Top = this.Top + (e.Y - mousey);
                        this.Width = this.Width - (e.X - mousex);
                        this.Height = this.Height - (e.Y - mousey);
                    }
                }
                else
                {
                    if (this.Height / 2 < e.Y)
                    {
                        this.Width = e.X;
                        this.Height = e.Y;
                    }
                    else
                    {
                        this.Top = this.Top + (e.Y - mousey);
                        this.Width = e.X;
                        this.Height = this.Height - (e.Y - mousey);
                    }
                }
            }
        }

        void MyButton_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            allowResize = false;
        }

        void MyButton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            allowResize = true;
            mousex = e.X;
            mousey = e.Y;
        }
    }


Tamer Oz  Thursday, September 24, 2009 2:07 PM
thank you very much Tamer Oz MVP your answer is very help me. if i have another problem i will ask you again.
muhamad nizar iqbal  Thursday, September 24, 2009 3:19 PM
Hi,

If you are interested, I created a custom control that allows your user to move, size and align form controls at run-time.
The custom control DLL is free for commercial and non-commercial use and canbe found at : http://www.sharpcontrols.net/Controls/SharpControlSelect.aspx
Any comments on the control would be gratefully accepted.

Andy

www.sharpcontrols.net -> Free custom controls for .NET
andyfraser  Saturday, September 26, 2009 8:31 PM

You can use google to search for other answers

Custom Search

More Threads

• Digital Rights Management DRM for Win Forms
• What is the differnece?
• How to use controls in an executable?
• Logging into MDI Application
• MetaFile Problem
• autoresize methods not doing anything for datagridview
• bitmaps in forms
• control system sounds
• Dock
• How do i specify permissions against calling assembly