Editable Binding

image_pdfimage_print



   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ADO.NET_Binding
{
    /// <summary>
    /// Summary description for EditableBinding.
    /// </summary>
    public class EditableBinding : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Label Label4;
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Label Label3;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.TextBox txtModelName;
        internal System.Windows.Forms.TextBox txtDescription;
        internal System.Windows.Forms.TextBox txtUnitCost;
        internal System.Windows.Forms.TextBox txtModelNumber;
        internal System.Windows.Forms.ComboBox cboModelName;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public EditableBinding()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #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.Label4 = new System.Windows.Forms.Label();
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label3 = new System.Windows.Forms.Label();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.txtModelName = new System.Windows.Forms.TextBox();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.txtUnitCost = new System.Windows.Forms.TextBox();
            this.txtModelNumber = new System.Windows.Forms.TextBox();
            this.cboModelName = new System.Windows.Forms.ComboBox();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Label4
            // 
            this.Label4.Location = new System.Drawing.Point(16, 17);
            this.Label4.Name = "Label4";
            this.Label4.Size = new System.Drawing.Size(88, 16);
            this.Label4.TabIndex = 20;
            this.Label4.Text = "Select a Record:";
            // 
            // GroupBox1
            // 
            this.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.Label3,
                                                                                    this.Label2,
                                                                                    this.Label1,
                                                                                    this.txtModelName,
                                                                                    this.txtDescription,
                                                                                    this.txtUnitCost,
                                                                                    this.txtModelNumber});
            this.GroupBox1.Location = new System.Drawing.Point(16, 53);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(400, 252);
            this.GroupBox1.TabIndex = 19;
            this.GroupBox1.TabStop = false;
            // 
            // Label3
            // 
            this.Label3.Location = new System.Drawing.Point(220, 56);
            this.Label3.Name = "Label3";
            this.Label3.Size = new System.Drawing.Size(36, 16);
            this.Label3.TabIndex = 18;
            this.Label3.Text = "Cost:";
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(16, 56);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(52, 16);
            this.Label2.TabIndex = 17;
            this.Label2.Text = "Model:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(16, 28);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(52, 16);
            this.Label1.TabIndex = 16;
            this.Label1.Text = "Name:";
            // 
            // txtModelName
            // 
            this.txtModelName.Location = new System.Drawing.Point(68, 24);
            this.txtModelName.Name = "txtModelName";
            this.txtModelName.Size = new System.Drawing.Size(316, 21);
            this.txtModelName.TabIndex = 15;
            this.txtModelName.Text = "";
            // 
            // txtDescription
            // 
            this.txtDescription.Location = new System.Drawing.Point(12, 92);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(372, 116);
            this.txtDescription.TabIndex = 14;
            this.txtDescription.Text = "";
            // 
            // txtUnitCost
            // 
            this.txtUnitCost.Location = new System.Drawing.Point(256, 52);
            this.txtUnitCost.Name = "txtUnitCost";
            this.txtUnitCost.Size = new System.Drawing.Size(128, 21);
            this.txtUnitCost.TabIndex = 13;
            this.txtUnitCost.Text = "";
            // 
            // txtModelNumber
            // 
            this.txtModelNumber.Location = new System.Drawing.Point(68, 52);
            this.txtModelNumber.Name = "txtModelNumber";
            this.txtModelNumber.Size = new System.Drawing.Size(136, 21);
            this.txtModelNumber.TabIndex = 12;
            this.txtModelNumber.Text = "";
            // 
            // cboModelName
            // 
            this.cboModelName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboModelName.Location = new System.Drawing.Point(108, 13);
            this.cboModelName.Name = "cboModelName";
            this.cboModelName.Size = new System.Drawing.Size(308, 21);
            this.cboModelName.TabIndex = 18;
            // 
            // EditableBinding
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(432, 318);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Label4,
                                                                          this.GroupBox1,
                                                                          this.cboModelName});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "EditableBinding";
            this.Text = "EditableBinding";
            this.Load += new System.EventHandler(this.EditableBinding_Load);
            this.GroupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void EditableBinding_Load(object sender, System.EventArgs e)
        {
            DataSet dsStore = new DataSet();
            dsStore.ReadXmlSchema(Application.StartupPath + "store.xsd");
            dsStore.ReadXml(Application.StartupPath + "store.xml");

            cboModelName.DataSource = dsStore.Tables["Products"];
            cboModelName.DisplayMember = "ModelName";

            txtModelName.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelName");
            txtModelNumber.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelNumber");
            txtUnitCost.DataBindings.Add("Text", dsStore.Tables["Products"], "UnitCost");
            txtDescription.DataBindings.Add("Text", dsStore.Tables["Products"], "Description");

        }
        [STAThread]
        static void Main() 
        {
            Application.Run(new EditableBinding());
        }
    }
}


           
          


ADO.NETBinding.zip( 76 k)

Add new item to ListBox (text from TextBox)

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
      private System.Windows.Forms.ListBox displayListBox;
      private System.Windows.Forms.TextBox inputTextBox;
      private System.Windows.Forms.Button addButton;
      private System.Windows.Forms.Button removeButton;
      private System.Windows.Forms.Button clearButton;
      
      public Form1() {
        InitializeComponent();
    }

      private void addButton_Click( object sender, EventArgs e )
      {
        displayListBox.Items.Add( inputTextBox.Text );
        inputTextBox.Clear();
      } 
      private void removeButton_Click( object sender, EventArgs e )
      {
        if ( displayListBox.SelectedIndex != -1 )
          displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
      }

      private void clearButton_Click( object sender, EventArgs e )
      {
         displayListBox.Items.Clear();
      }

      private void InitializeComponent()
      {
         this.displayListBox = new System.Windows.Forms.ListBox();
         this.inputTextBox = new System.Windows.Forms.TextBox();
         this.addButton = new System.Windows.Forms.Button();
         this.removeButton = new System.Windows.Forms.Button();
         this.clearButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // displayListBox
         // 
         this.displayListBox.FormattingEnabled = true;
         this.displayListBox.Location = new System.Drawing.Point(13, 12);
         this.displayListBox.Name = "displayListBox";
         this.displayListBox.Size = new System.Drawing.Size(119, 238);
         this.displayListBox.TabIndex = 0;
         // 
         // inputTextBox
         // 
         this.inputTextBox.Location = new System.Drawing.Point(149, 12);
         this.inputTextBox.Name = "inputTextBox";
         this.inputTextBox.Size = new System.Drawing.Size(100, 20);
         this.inputTextBox.TabIndex = 1;
         // 
         // addButton
         // 
         this.addButton.Location = new System.Drawing.Point(149, 56);
         this.addButton.Name = "addButton";
         this.addButton.Size = new System.Drawing.Size(100, 36);
         this.addButton.TabIndex = 2;
         this.addButton.Text = "Add";
         this.addButton.Click += new System.EventHandler(this.addButton_Click);
         // 
         // removeButton
         // 
         this.removeButton.Location = new System.Drawing.Point(149, 109);
         this.removeButton.Name = "removeButton";
         this.removeButton.Size = new System.Drawing.Size(100, 36);
         this.removeButton.TabIndex = 3;
         this.removeButton.Text = "Remove";
         this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
         // 
         // clearButton
         // 
         this.clearButton.Location = new System.Drawing.Point(149, 165);
         this.clearButton.Name = "clearButton";
         this.clearButton.Size = new System.Drawing.Size(100, 36);
         this.clearButton.TabIndex = 4;
         this.clearButton.Text = "Clear";
         this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
         // ListBoxTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(263, 268);
         this.Controls.Add(this.clearButton);
         this.Controls.Add(this.removeButton);
         this.Controls.Add(this.addButton);
         this.Controls.Add(this.inputTextBox);
         this.Controls.Add(this.displayListBox);
         this.Name = "ListBoxTestForm";
         this.Text = "ListBoxTest";
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      [STAThread]
      static void Main()
      {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
    }

}


           
          


Remove item if one is selected from ListBox

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
      private System.Windows.Forms.ListBox displayListBox;
      private System.Windows.Forms.TextBox inputTextBox;
      private System.Windows.Forms.Button addButton;
      private System.Windows.Forms.Button removeButton;
      private System.Windows.Forms.Button clearButton;
      
      public Form1() {
        InitializeComponent();
    }

      private void addButton_Click( object sender, EventArgs e )
      {
        displayListBox.Items.Add( inputTextBox.Text );
        inputTextBox.Clear();
      } 
      private void removeButton_Click( object sender, EventArgs e )
      {
        if ( displayListBox.SelectedIndex != -1 )
          displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
      }

      private void clearButton_Click( object sender, EventArgs e )
      {
         displayListBox.Items.Clear();
      }

      private void InitializeComponent()
      {
         this.displayListBox = new System.Windows.Forms.ListBox();
         this.inputTextBox = new System.Windows.Forms.TextBox();
         this.addButton = new System.Windows.Forms.Button();
         this.removeButton = new System.Windows.Forms.Button();
         this.clearButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // displayListBox
         // 
         this.displayListBox.FormattingEnabled = true;
         this.displayListBox.Location = new System.Drawing.Point(13, 12);
         this.displayListBox.Name = "displayListBox";
         this.displayListBox.Size = new System.Drawing.Size(119, 238);
         this.displayListBox.TabIndex = 0;
         // 
         // inputTextBox
         // 
         this.inputTextBox.Location = new System.Drawing.Point(149, 12);
         this.inputTextBox.Name = "inputTextBox";
         this.inputTextBox.Size = new System.Drawing.Size(100, 20);
         this.inputTextBox.TabIndex = 1;
         // 
         // addButton
         // 
         this.addButton.Location = new System.Drawing.Point(149, 56);
         this.addButton.Name = "addButton";
         this.addButton.Size = new System.Drawing.Size(100, 36);
         this.addButton.TabIndex = 2;
         this.addButton.Text = "Add";
         this.addButton.Click += new System.EventHandler(this.addButton_Click);
         // 
         // removeButton
         // 
         this.removeButton.Location = new System.Drawing.Point(149, 109);
         this.removeButton.Name = "removeButton";
         this.removeButton.Size = new System.Drawing.Size(100, 36);
         this.removeButton.TabIndex = 3;
         this.removeButton.Text = "Remove";
         this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
         // 
         // clearButton
         // 
         this.clearButton.Location = new System.Drawing.Point(149, 165);
         this.clearButton.Name = "clearButton";
         this.clearButton.Size = new System.Drawing.Size(100, 36);
         this.clearButton.TabIndex = 4;
         this.clearButton.Text = "Clear";
         this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
         // ListBoxTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(263, 268);
         this.Controls.Add(this.clearButton);
         this.Controls.Add(this.removeButton);
         this.Controls.Add(this.addButton);
         this.Controls.Add(this.inputTextBox);
         this.Controls.Add(this.displayListBox);
         this.Name = "ListBoxTestForm";
         this.Text = "ListBoxTest";
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      [STAThread]
      static void Main()
      {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
    }

}


           
          


Clear all items in a ListBox

image_pdfimage_print


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
      private System.Windows.Forms.ListBox displayListBox;
      private System.Windows.Forms.TextBox inputTextBox;
      private System.Windows.Forms.Button addButton;
      private System.Windows.Forms.Button removeButton;
      private System.Windows.Forms.Button clearButton;
      
      public Form1() {
        InitializeComponent();
    }

      private void addButton_Click( object sender, EventArgs e )
      {
        displayListBox.Items.Add( inputTextBox.Text );
        inputTextBox.Clear();
      } 
      private void removeButton_Click( object sender, EventArgs e )
      {
        if ( displayListBox.SelectedIndex != -1 )
          displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
      }

      private void clearButton_Click( object sender, EventArgs e )
      {
         displayListBox.Items.Clear();
      }

      private void InitializeComponent()
      {
         this.displayListBox = new System.Windows.Forms.ListBox();
         this.inputTextBox = new System.Windows.Forms.TextBox();
         this.addButton = new System.Windows.Forms.Button();
         this.removeButton = new System.Windows.Forms.Button();
         this.clearButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // displayListBox
         // 
         this.displayListBox.FormattingEnabled = true;
         this.displayListBox.Location = new System.Drawing.Point(13, 12);
         this.displayListBox.Name = "displayListBox";
         this.displayListBox.Size = new System.Drawing.Size(119, 238);
         this.displayListBox.TabIndex = 0;
         // 
         // inputTextBox
         // 
         this.inputTextBox.Location = new System.Drawing.Point(149, 12);
         this.inputTextBox.Name = "inputTextBox";
         this.inputTextBox.Size = new System.Drawing.Size(100, 20);
         this.inputTextBox.TabIndex = 1;
         // 
         // addButton
         // 
         this.addButton.Location = new System.Drawing.Point(149, 56);
         this.addButton.Name = "addButton";
         this.addButton.Size = new System.Drawing.Size(100, 36);
         this.addButton.TabIndex = 2;
         this.addButton.Text = "Add";
         this.addButton.Click += new System.EventHandler(this.addButton_Click);
         // 
         // removeButton
         // 
         this.removeButton.Location = new System.Drawing.Point(149, 109);
         this.removeButton.Name = "removeButton";
         this.removeButton.Size = new System.Drawing.Size(100, 36);
         this.removeButton.TabIndex = 3;
         this.removeButton.Text = "Remove";
         this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
         // 
         // clearButton
         // 
         this.clearButton.Location = new System.Drawing.Point(149, 165);
         this.clearButton.Name = "clearButton";
         this.clearButton.Size = new System.Drawing.Size(100, 36);
         this.clearButton.TabIndex = 4;
         this.clearButton.Text = "Clear";
         this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
         // ListBoxTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(263, 268);
         this.Controls.Add(this.clearButton);
         this.Controls.Add(this.removeButton);
         this.Controls.Add(this.addButton);
         this.Controls.Add(this.inputTextBox);
         this.Controls.Add(this.displayListBox);
         this.Name = "ListBoxTestForm";
         this.Text = "ListBoxTest";
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      [STAThread]
      static void Main()
      {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
    }

}



           
          


ListBox selected Item changed event

image_pdfimage_print


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
  private System.Windows.Forms.ListBox lstCustomers;
  public Form1() {
        InitializeComponent();
    lstCustomers.Items.Add(new Customer("A", "B", DateTime.Now.AddDays(-10)));
    lstCustomers.Items.Add(new Customer("C", "D", DateTime.Now.AddDays(-100)));
    lstCustomers.Items.Add(new Customer("F", "G", DateTime.Now.AddDays(-500)));

  }
  private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
  {
    Customer cust = (Customer)lstCustomers.SelectedItem;
    MessageBox.Show("Birth Date: " + cust.BirthDate.ToShortDateString());
  }
  private void InitializeComponent()
  {
    this.lstCustomers = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // lstCustomers
    // 
    this.lstCustomers.FormattingEnabled = true;
    this.lstCustomers.Location = new System.Drawing.Point(12, 12);
    this.lstCustomers.Name = "lstCustomers";
    this.lstCustomers.Size = new System.Drawing.Size(120, 95);
    this.lstCustomers.TabIndex = 0;
    this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(308, 230);
    this.Controls.Add(this.lstCustomers);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

  }


  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}
public class Customer
{
  public string FirstName;
  public string LastName;
  public DateTime BirthDate;

  public Customer() { }

  public Customer(string firstName, string lastName, DateTime birthDate)
  {
    FirstName = firstName;
    LastName = lastName;
    BirthDate = birthDate;
  }

  public override string ToString()
  {
    return FirstName + " " + LastName;
  }
}


           
          


Add Object to ListBox

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
  private System.Windows.Forms.ListBox lstCustomers;
  public Form1() {
        InitializeComponent();
    lstCustomers.Items.Add(new Customer("A", "B", DateTime.Now.AddDays(-10)));
    lstCustomers.Items.Add(new Customer("C", "D", DateTime.Now.AddDays(-100)));
    lstCustomers.Items.Add(new Customer("F", "G", DateTime.Now.AddDays(-500)));

  }
  private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
  {
    Customer cust = (Customer)lstCustomers.SelectedItem;
    MessageBox.Show("Birth Date: " + cust.BirthDate.ToShortDateString());
  }
  private void InitializeComponent()
  {
    this.lstCustomers = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // lstCustomers
    // 
    this.lstCustomers.FormattingEnabled = true;
    this.lstCustomers.Location = new System.Drawing.Point(12, 12);
    this.lstCustomers.Name = "lstCustomers";
    this.lstCustomers.Size = new System.Drawing.Size(120, 95);
    this.lstCustomers.TabIndex = 0;
    this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(308, 230);
    this.Controls.Add(this.lstCustomers);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

  }


  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}
public class Customer
{
  public string FirstName;
  public string LastName;
  public DateTime BirthDate;

  public Customer() { }

  public Customer(string firstName, string lastName, DateTime birthDate)
  {
    FirstName = firstName;
    LastName = lastName;
    BirthDate = birthDate;
  }

  public override string ToString()
  {
    return FirstName + " " + LastName;
  }
}

           
          


List Box click event

image_pdfimage_print


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
  private System.Windows.Forms.Label lblSaturation;
  private System.Windows.Forms.Label lblHue;
  private System.Windows.Forms.Label lblBrightness;
  private System.Windows.Forms.Label Label1;
  private System.Windows.Forms.ListBox lstColors;

  public Form1()
  {
    InitializeComponent();
    string[] colorNames  = System.Enum.GetNames(typeof(KnownColor));
    lstColors.Items.AddRange(colorNames);
  }

  private void lstColors_SelectedIndexChanged(object sender, EventArgs e)
  {
    KnownColor selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), lstColors.Text);

    this.BackColor = System.Drawing.Color.FromKnownColor(selectedColor);

    lblBrightness.Text = "Brightness = " + this.BackColor.GetBrightness().ToString();
    lblHue.Text = "Hue = " + this.BackColor.GetHue().ToString();
    lblSaturation.Text = "Saturation = " + this.BackColor.GetSaturation().ToString();
  }

  private void InitializeComponent()
  {
    this.lblSaturation = new System.Windows.Forms.Label();
    this.lblHue = new System.Windows.Forms.Label();
    this.lblBrightness = new System.Windows.Forms.Label();
    this.Label1 = new System.Windows.Forms.Label();
    this.lstColors = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // lblSaturation
    // 
    this.lblSaturation.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.lblSaturation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lblSaturation.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.lblSaturation.Location = new System.Drawing.Point(268, 57);
    this.lblSaturation.Name = "lblSaturation";
    this.lblSaturation.Size = new System.Drawing.Size(136, 20);
    this.lblSaturation.TabIndex = 4;
    this.lblSaturation.Text = " Saturation";
    // 
    // lblHue
    // 
    this.lblHue.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.lblHue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lblHue.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.lblHue.Location = new System.Drawing.Point(268, 33);
    this.lblHue.Name = "lblHue";
    this.lblHue.Size = new System.Drawing.Size(136, 20);
    this.lblHue.TabIndex = 3;
    this.lblHue.Text = " Hue";
    // 
    // lblBrightness
    // 
    this.lblBrightness.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.lblBrightness.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lblBrightness.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.lblBrightness.Location = new System.Drawing.Point(268, 9);
    this.lblBrightness.Name = "lblBrightness";
    this.lblBrightness.Size = new System.Drawing.Size(136, 20);
    this.lblBrightness.TabIndex = 2;
    this.lblBrightness.Text = " Brightness";
    // 
    // Label1
    // 
    this.Label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Label1.Location = new System.Drawing.Point(12, 9);
    this.Label1.Name = "Label1";
    this.Label1.Size = new System.Drawing.Size(200, 20);
    this.Label1.TabIndex = 0;
    this.Label1.Text = " Choose a Background Color:";
    // 
    // lstColors
    // 
    this.lstColors.FormattingEnabled = true;
    this.lstColors.Location = new System.Drawing.Point(12, 37);
    this.lstColors.Name = "lstColors";
    this.lstColors.Size = new System.Drawing.Size(200, 238);
    this.lstColors.TabIndex = 1;
    this.lstColors.SelectedIndexChanged += new System.EventHandler(this.lstColors_SelectedIndexChanged);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(430, 284);
    this.Controls.Add(this.lblSaturation);
    this.Controls.Add(this.lblHue);
    this.Controls.Add(this.lblBrightness);
    this.Controls.Add(this.Label1);
    this.Controls.Add(this.lstColors);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Color Changer";
    this.ResumeLayout(false);

  }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}