Click here to Skip to main content
15,884,739 members
Articles / Programming Languages / XML
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.
2.33/5 (3 votes)
14 May 2011CPOL 11.8K   1  
C#
using System;
using System.Windows.Forms;
using System.Xml;

namespace Anchor_Form
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.ComboBox comboBox1;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

       private string xml = "
XML
<DockStyles><Dock Data=\"None\"/><Dock Data=\"Top\"/><Dock Data=\"Bottom\"/><Dock Data=\"Left\"/><Dock Data=\"Right\"/><Dock Data=\"Fill\"/></DockStyles>
C#
";
C#
        private XmlDocument doc = new XmlDocument();

        public Form1()
        {
            InitializeComponent();
            doc.LoadXml(xml);
            
            foreach (XmlNode node in doc.SelectNodes("/DockStyles/Dock"))
            {
                comboBox1.Items.Add(node.Attributes["Data"].InnerText);
            }
             comboBox1.SelectedIndex = 0;
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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(43, 60);
            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(61, 178);
            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.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private DockStyle Style(string value)
        {
            return (DockStyle)Enum.Parse(typeof(DockStyle), value);
        }
        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

 
-- There are no messages in this forum --