Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

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

, 7 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
using System;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { private Panel panel1; private ComboBox comboBox1; public Form1() { InitializeComponent(); } ...
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)

Share

About the Author

charles henington

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? Pinmembercharles henington15-May-11 5:07 
GeneralReason for my vote of 2 It does not make much sense to uses ... PinmemberPhilippe Mori14-May-11 11:15 
GeneralReason for my vote of 5 Very elegant and practical solution!... PinmemberDrABELL10-May-11 11:33 
Reason for my vote of 5
Very elegant and practical solution! 5*

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 7 May 2011
Article Copyright 2011 by charles henington
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid