Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

Simple method using Enum.Parse to change the dockstyle to control on form with combobox

4.00/5 (2 votes)
7 May 2011CPOL 9.8K  
using System;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { private Panel panel1; private ComboBox comboBox1; public Form1() { InitializeComponent(); } ...
C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Panel panel1;
        private ComboBox comboBox1;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.panel1.Location = new System.Drawing.Point(47, 65);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 100);
            this.panel1.TabIndex = 0;
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(126, 187);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 21);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(298, 264);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.ResumeLayout(false);

        }

        protected override void OnLoad(EventArgs e)
        {
            string[] DockStyles = Enum.GetNames(typeof(MyDock));
            comboBox1.Items.AddRange(DockStyles);
            base.OnLoad(e);
        }

        private enum MyDock
        {
            None,
            Top,
            Bottom,
            Left,
            Right,
            Fill
        }

        private DockStyle Style(string style)
        {
            return (DockStyle)Enum.Parse(typeof(MyDock), style);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            panel1.Dock = Style(comboBox1.SelectedItem.ToString());
            comboBox1.BringToFront();
        }
    }
}

License

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