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

2.00/5 (2 votes)
9 May 2011CPOL 9.3K  
ComboBox items do not have to be strings.The combobox calls ToString on each object in Items so simply add the enum values to the combobox.using System;using System.Drawing;using System.Windows.Forms;namespace Anchor_Form{ public class Form1 : Form { public...
ComboBox items do not have to be strings.

The combobox calls ToString on each object in Items so simply add the enum values to the combobox.


using System;
using System.Drawing;
using System.Windows.Forms;


namespace Anchor_Form
{
    public class Form1 : Form
    {
		public Form1()
		{
			InitializeComponent();
		    
			foreach (DockStyle style in Enum.GetValues(typeof(DockStyle)))
				comboBox1.Items.Add(style);

			comboBox1.SelectedIndex = 0;
		}

		private void InitializeComponent()
		{
			panel1		= new Panel();
			comboBox1	= new ComboBox();
			SuspendLayout();
			// 
			// panel1
			// 
			panel1.BackColor = SystemColors.ControlDarkDark;
			panel1.Location = new Point(43, 60);
			panel1.Name = "panel1";
			panel1.Size = new Size(200, 100);
			panel1.TabIndex = 0;
			// 
			// comboBox1
			// 
			comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
			comboBox1.Location = new Point(61, 178);
			comboBox1.Name = "comboBox1";
			comboBox1.Size = new Size(121, 21);
			comboBox1.TabIndex = 1;
			comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
			// 
			// Form1
			// 
			AutoScaleDimensions = new SizeF(6F, 13F);
			AutoScaleMode = AutoScaleMode.Font;
			ClientSize = new Size(284, 264);
			Controls.Add(comboBox1);
			Controls.Add(panel1);
			Name = "Form1";
			Text = "Form1";
			ResumeLayout(false);
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			panel1.Dock = (DockStyle)comboBox1.SelectedItem;

			comboBox1.BringToFront();
		}

		Panel		panel1;
		ComboBox	comboBox1;
	}
}

License

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