Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / XML
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
7 May 2011CPOL 9K   1   3
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)


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralYour not very bright are you? Pin
charles henington15-May-11 4:07
charles henington15-May-11 4:07 
GeneralReason for my vote of 2 It does not make much sense to uses ... Pin
Philippe Mori14-May-11 10:15
Philippe Mori14-May-11 10:15 
GeneralReason for my vote of 5 Very elegant and practical solution!... Pin
DrABELL10-May-11 10:33
DrABELL10-May-11 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.