Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Relative Design Components on WinForm

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
14 Nov 2012CPOL2 min read 17.1K   339   12  
Simple code to make your components always fit exact size of Form, during and after resize.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TForm.display;


namespace RelativeLayoutTest {
    public partial class RelativeTest : Form {





        public RelativeTest() {
            // some minimal size is always neccessary
            this.MinimumSize = new System.Drawing.Size(500, 400);
            InitializeComponent();



            ///////////
            // left box::
            Position pos = RelativeLayout.Add(BoxLeft);

            pos.Left = 10;
            pos.Bottom = 44;
            pos.Vertical = -3;
            pos.Horizontal = -3;

            ///////////
            // right box::
            pos = RelativeLayout.Add(BoxRight);
            pos.Right = 10;
            pos.Bottom = 44;
            pos.Vertical = -3;
            pos.Horizontal = 3;

            ///////////
            // buttons

            pos = RelativeLayout.Add(leftButton);
            pos.Left = 20;
            pos.RelativeHorizontal = 0.3f;

            pos = RelativeLayout.Add(rightButton);
            pos.Right = 20;
            pos.RelativeHorizontal = 0.7f;

            pos = RelativeLayout.Add(centerButton);
            pos.Horizontal = 0;
            pos.Width = -0.4f;


            pos = RelativeLayout.Add(ContainerAlignButton);
            pos.Top = pos.Bottom = pos.Left = pos.Right = 15;

            pos = RelativeLayout.Add(RelativeButton);
            pos.RelativeVertical = 0.2f;
            pos.RelativeHorizontal = 0.2f;
            pos.Width = -0.2f;
            pos.Height = -0.2f;


            pos = RelativeLayout.Add(LeftRelWidButton);
            pos.Left = 10;
            pos.Bottom = 10;
            pos.Width =  -0.4f;

            ///////////
            // jumping button
            Button jumpButton = new Button();
            jumpButton.Text = "Click to change container <=";
            jumpButton.Click += new EventHandler(jumpButton_Click);
            jumpButton.Width = 160;
            

            pos = RelativeLayout.Add(jumpButton);
            pos.Horizontal = 0;
            pos.Vertical = -20;

            BoxLeft.Controls.Add(jumpButton);
            jumpButton.BringToFront();

            // 2 :

            jumpButton = new Button();
            jumpButton.Text = "Click to change container =>";
            jumpButton.Click += new EventHandler(jumpButton_Click);
            jumpButton.Width = 160;

            pos = RelativeLayout.Add(jumpButton);
            pos.Horizontal = 0;
            pos.Vertical = 20;

            BoxRight.Controls.Add(jumpButton);
            jumpButton.BringToFront();

            ///////////
            // tree::
            TreeTest.Nodes.Add("node 1");
            TreeTest.Nodes.Add("node 2");

            pos = RelativeLayout.Add(TreeTest);
            pos.Right = 20;
            pos.Horizontal = 70;
            pos.Top = 50;
            pos.Vertical = -10;

            //////////
            // list::
            ListTest.Items.Add("Item 1");
            ListTest.Items.Add("Item 2");
            ListTest.Items.Add("Item 3");

            pos = RelativeLayout.Add(ListTest);
            pos.Top = 50;
            pos.Vertical = -10;

            pos.Horizontal = -35;
            pos.Width = 200;

            ////////////////////
            // labels ::

            pos = RelativeLayout.Add(TopRightLabel);
            pos.Top = 25;
            pos.Right = 25;

            pos = RelativeLayout.Add(HorizonBottomLabel);
            pos.Bottom = 15;
            pos.Horizontal = 0;

            pos = RelativeLayout.Add(WidHeiLabel);
            pos.RelativeHorizontal = 3/4f;
            pos.RelativeVertical = 3/4f;

            pos = RelativeLayout.Add(BottomRightLabel);
            pos.Right = 15;
            pos.Bottom = 15;

            pos = RelativeLayout.Add(LeftVerticalLabel);
            pos.Left = 5;
            pos.Vertical = 0;

            pos = RelativeLayout.Add(RightLabel);
            pos.Right = 5;
            pos.Vertical = 0;

            pos = RelativeLayout.Add(TopHorizontalLabel);
            pos.Top = 25;
            pos.Horizontal = 0;

            pos = RelativeLayout.Add(VerHorLabel);
            pos.Vertical = 0;
            pos.Horizontal = 0;

            pos = RelativeLayout.Add(TopLeftLabel);
            pos.Left = 5;
            pos.Top = 25;

            pos = RelativeLayout.Add(BottomLeftLabel);
            pos.Left = 5;
            pos.Bottom = 15;

            ////////////////////
            // text box ::
            TextBoxTest.Text = "Some start text ...\n";
            pos = RelativeLayout.Add(TextBoxTest);
            pos.Top = 50;
            pos.Vertical = -10;
            pos.Left = 20;
            pos.Horizontal = -140;


            RelativeLayout.ReplaceContainer(this);
        }

        void jumpButton_Click(object sender, EventArgs e) {
            Control jump = (Button)sender;
            if (jump.Parent == BoxLeft) {
                BoxRight.Controls.Add(jump);
            } else {
                BoxLeft.Controls.Add(jump);
            };
            jump.BringToFront();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions