Click here to Skip to main content
Click here to Skip to main content

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

By , 7 May 2011
 
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)

About the Author

charles henington
United States United States
Member
I do not claim to be wrong! I just rarely ever write.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 2 It does not make much sense to uses ...memberPhilippe Mori14 May '11 - 10:15 
Reason for my vote of 2
It does not make much sense to uses another enum type.

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

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