Set TopIndex to auto scroll 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.GroupBox GroupBox1;
    private System.Windows.Forms.Label Label4;
    private System.Windows.Forms.Label Label1;
    private System.Windows.Forms.PictureBox pic;
    private System.Windows.Forms.TextBox txt;
    private System.Windows.Forms.Button cmd;
    private System.Windows.Forms.Label Label2;
    private System.Windows.Forms.Label Label3;
    private System.Windows.Forms.ListBox eventLogList;

    public Form1() {
        InitializeComponent();
    }
    private void Log(String data)
    {
        eventLogList.Items.Add(data);
        int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
        eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
    }

    private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
    }

    private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        Log("Key Press: " + e.KeyChar.ToString());
    }

    private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
    }

    private void txt_TextChanged(object sender, System.EventArgs e)
    {
        Log("Changed: " + " Text is: " + txt.Text);
    }

    private void pic_MouseEnter(object sender, System.EventArgs e)
    {
        Log("Mouse Enter");
    }

    private void pic_MouseHover(object sender, System.EventArgs e)
    {
        Log("Mouse Hover");
    }

    private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
    }

    private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
    }

    private void pic_Click(object sender, System.EventArgs e)
    {
        Log("Click");
    }

    private void pic_DoubleClick(object sender, System.EventArgs e)
    {
        Log("Double Click");
    }

    private void pic_MouseLeave(object sender, System.EventArgs e)
    {
        Log("Mouse Leave");
    }

    private void InitializeComponent()
    {
        this.GroupBox1 = new System.Windows.Forms.GroupBox();
        this.Label4 = new System.Windows.Forms.Label();
        this.Label1 = new System.Windows.Forms.Label();
        this.pic = new System.Windows.Forms.PictureBox();
        this.txt = new System.Windows.Forms.TextBox();
        this.cmd = new System.Windows.Forms.Button();
        this.Label2 = new System.Windows.Forms.Label();
        this.Label3 = new System.Windows.Forms.Label();
        this.eventLogList = new System.Windows.Forms.ListBox();
        this.GroupBox1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).BeginInit();
        this.SuspendLayout();
        // 
        // GroupBox1
        // 
        this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.GroupBox1.Controls.Add(this.Label4);
        this.GroupBox1.Controls.Add(this.Label1);
        this.GroupBox1.Controls.Add(this.pic);
        this.GroupBox1.Controls.Add(this.txt);
        this.GroupBox1.Controls.Add(this.cmd);
        this.GroupBox1.Controls.Add(this.Label2);
        this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.GroupBox1.Location = new System.Drawing.Point(7, 0);
        this.GroupBox1.Name = "GroupBox1";
        this.GroupBox1.Size = new System.Drawing.Size(384, 148);
        this.GroupBox1.TabIndex = 12;
        this.GroupBox1.TabStop = false;
        // 
        // Label4
        // 
        this.Label4.Location = new System.Drawing.Point(92, 108);
        this.Label4.Name = "Label4";
        this.Label4.Size = new System.Drawing.Size(56, 16);
        this.Label4.TabIndex = 5;
        this.Label4.Text = "And here:";
        // 
        // Label1
        // 
        this.Label1.Location = new System.Drawing.Point(6, 24);
        this.Label1.Name = "Label1";
        this.Label1.Size = new System.Drawing.Size(144, 16);
        this.Label1.TabIndex = 2;
        this.Label1.Text = "Test keyboard events here:";
        // 
        // pic
        // 
        this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pic.Location = new System.Drawing.Point(156, 48);
        this.pic.Name = "pic";
        this.pic.Size = new System.Drawing.Size(192, 48);
        this.pic.TabIndex = 3;
        this.pic.TabStop = false;
        this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
        this.pic.Click += new System.EventHandler(this.pic_Click);
        this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
        this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
        this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
        this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
        // 
        // txt
        // 
        this.txt.Location = new System.Drawing.Point(156, 20);
        this.txt.Name = "txt";
        this.txt.Size = new System.Drawing.Size(192, 21);
        this.txt.TabIndex = 1;
        this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
        this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
        this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
        this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
        // 
        // cmd
        // 
        this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.cmd.Location = new System.Drawing.Point(156, 100);
        this.cmd.Name = "cmd";
        this.cmd.Size = new System.Drawing.Size(88, 28);
        this.cmd.TabIndex = 4;
        this.cmd.Text = "Button1";
        this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
        this.cmd.Click += new System.EventHandler(this.pic_Click);
        this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
        this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
        this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
        this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
        // 
        // Label2
        // 
        this.Label2.Location = new System.Drawing.Point(20, 52);
        this.Label2.Name = "Label2";
        this.Label2.Size = new System.Drawing.Size(128, 16);
        this.Label2.TabIndex = 2;
        this.Label2.Text = "Test mouse events here:";
        // 
        // Label3
        // 
        this.Label3.Location = new System.Drawing.Point(23, 100);
        this.Label3.Name = "Label3";
        this.Label3.Size = new System.Drawing.Size(64, 24);
        this.Label3.TabIndex = 11;
        this.Label3.Text = "Label3";
        // 
        // eventLogList
        // 
        this.eventLogList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.eventLogList.FormattingEnabled = true;
        this.eventLogList.IntegralHeight = false;
        this.eventLogList.Location = new System.Drawing.Point(7, 156);
        this.eventLogList.Name = "eventLogList";
        this.eventLogList.Size = new System.Drawing.Size(384, 212);
        this.eventLogList.TabIndex = 10;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(399, 374);
        this.Controls.Add(this.GroupBox1);
        this.Controls.Add(this.Label3);
        this.Controls.Add(this.eventLogList);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "Event Tracker";
        this.GroupBox1.ResumeLayout(false);
        this.GroupBox1.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
        this.ResumeLayout(false);

    }

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

}


           
          


Form with list, button

image_pdfimage_print


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Modeless
{
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  public class MainFormDemo : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    AddItemForm frmAdd;

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

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      frmAdd = new AddItemForm (this);

    }

    /// <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.listBox1 = new System.Windows.Forms.ListBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.button3 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(42, 16);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(208, 199);
      this.listBox1.TabIndex = 0;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(17, 240);
      this.button1.Name = "button1";
      this.button1.TabIndex = 1;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(109, 240);
      this.button2.Name = "button2";
      this.button2.TabIndex = 2;
      this.button2.Text = "Delete Item";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // button3
      // 
      this.button3.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this.button3.Location = new System.Drawing.Point(201, 240);
      this.button3.Name = "button3";
      this.button3.TabIndex = 3;
      this.button3.Text = "Close";
      this.button3.Click += new System.EventHandler(this.button3_Click);
      // 
      // MainForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.button3;
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button3,
                                      this.button2,
                                      this.button1,
                                      this.listBox1});
      this.Name = "MainForm";
      this.Text = "MainForm";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new MainFormDemo());
    }


    private void button1_Click(object sender, System.EventArgs e)
    {
      if (frmAdd.IsDisposed == true)
        frmAdd = new AddItemForm (this);
      frmAdd.Show ();
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      if (listBox1.SelectedIndex < 0)
        return;
      object obj = listBox1.Items&#91;listBox1.SelectedIndex&#93;;
      listBox1.Items.Remove (obj);
    }

    private void button3_Click(object sender, System.EventArgs e)
    {
      Application.Exit ();
    }

    public string AddItemToList (string strAdd)
    {
      if (strAdd == "")
        return ("");
      if (listBox1.FindString (strAdd, -1) < 0)
      {
        listBox1.Items.Add (strAdd);
        return ("");
      }
      MessageBox.Show (""" + strAdd + "" is already in the list box", "Duplicate");
      return (strAdd);
    }
  }
  /// <summary>
  /// Summary description for AddItemForm.
  /// </summary>
  public class AddItemForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    MainFormDemo parent;
    public AddItemForm(MainFormDemo parent)
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      this.parent = parent;
    }

    /// <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.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(28, 8);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(224, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(40, 48);
      this.button1.Name = "button1";
      this.button1.TabIndex = 2;
      this.button1.Text = "Add";
      this.button1.Click += new System.EventHandler(this.button1_Click_1);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(184, 48);
      this.button2.Name = "button2";
      this.button2.TabIndex = 3;
      this.button2.Text = "Close";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // AddItemForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(280, 77);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button2,
                                      this.button1,
                                      this.textBox1});
      this.Name = "AddItemForm";
      this.Text = "AddItem";
      this.ResumeLayout(false);

    }
    #endregion

    private void button1_Click_1(object sender, System.EventArgs e)
    {
      textBox1.Text = parent.AddItemToList (textBox1.Text);
//      textBox1.Text = "";
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      this.Dispose ();
      this.Close ();
    }
  }

}



           
          


Object List 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;

namespace DataBinding101
{
    /// <summary>
    /// Summary description for ObjectListBinding.
    /// </summary>
    public class ObjectListBinding : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ListBox lstCity;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ObjectListBinding()
        {
            //
            // 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.lstCity = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // lstCity
            // 
            this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstCity.IntegralHeight = false;
            this.lstCity.Location = new System.Drawing.Point(4, 4);
            this.lstCity.Name = "lstCity";
            this.lstCity.Size = new System.Drawing.Size(252, 216);
            this.lstCity.TabIndex = 2;
            this.lstCity.DoubleClick += new System.EventHandler(this.lstCity_DoubleClick);
            // 
            // ObjectListBinding
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(268, 230);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lstCity});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ObjectListBinding";
            this.Text = "ObjectListBinding";
            this.Load += new System.EventHandler(this.ObjectListBinding_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void ObjectListBinding_Load(object sender, System.EventArgs e)
        {
            City[] cityChoices = {new City("Seattle", "U.S.A."), 
                                     new City("New York", "U.S.A."), new City("Tokyo", "Japan"), 
                                     new City("Montreal", "Canada")};

            lstCity.DataSource = cityChoices;
            lstCity.DisplayMember = "Name";

        }

        private void lstCity_DoubleClick(object sender, System.EventArgs e)
        {
                MessageBox.Show(((City)lstCity.SelectedItem).Country);
        }

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

    public class City
    {
        private string name;
        private string country;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
            }
        }

        public City(string name, string country)
        {
            this.Name = name;
            this.Country = country;
        }
    }

}

           
          


Data ListBox Binding 3

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 DataListBinding.
    /// </summary>
    public class DataListBinding : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ListBox lstName;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DataListBinding()
        {
            //
            // 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.lstName = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // lstName
            // 
            this.lstName.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstName.IntegralHeight = false;
            this.lstName.Location = new System.Drawing.Point(8, 8);
            this.lstName.Name = "lstName";
            this.lstName.Size = new System.Drawing.Size(232, 184);
            this.lstName.TabIndex = 1;
            // 
            // DataListBinding
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(248, 202);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lstName});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "DataListBinding";
            this.Text = "DataListBinding";
            this.Load += new System.EventHandler(this.DataListBinding_Load);
            this.ResumeLayout(false);

        }
        #endregion

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

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

        }

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


           
          


ADO.NETBinding.zip( 76 k)

Single Item Data 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;

namespace DataBinding101
{
    /// <summary>
    /// Summary description for SingleItemDataBinding.
    /// </summary>
    public class SingleItemDataBinding : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ListBox lstCity;
        internal System.Windows.Forms.TextBox txtCity;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public SingleItemDataBinding()
        {
            //
            // 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.lstCity = new System.Windows.Forms.ListBox();
            this.txtCity = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // lstCity
            // 
            this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstCity.IntegralHeight = false;
            this.lstCity.Location = new System.Drawing.Point(12, 40);
            this.lstCity.Name = "lstCity";
            this.lstCity.Size = new System.Drawing.Size(224, 148);
            this.lstCity.TabIndex = 3;
            // 
            // txtCity
            // 
            this.txtCity.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.txtCity.Location = new System.Drawing.Point(12, 12);
            this.txtCity.Name = "txtCity";
            this.txtCity.Size = new System.Drawing.Size(224, 21);
            this.txtCity.TabIndex = 2;
            this.txtCity.Text = "";
            // 
            // SingleItemDataBinding
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(248, 202);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lstCity,
                                                                          this.txtCity});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "SingleItemDataBinding";
            this.Text = "SingleItemDataBinding";
            this.Load += new System.EventHandler(this.SingleItemDataBinding_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void SingleItemDataBinding_Load(object sender, System.EventArgs e)
        {
             string[] cityChoices = {"Seattle", "New York", "Tokyo", "Montreal"};
            lstCity.DataSource = cityChoices;
            txtCity.DataBindings.Add("Text", cityChoices, "");
        }

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



           
          


ListBox: Binding Unusual Properties

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.Drawing.Text;

namespace DataBinding101
{
    /// <summary>
    /// Summary description for BindingUnusualProperties.
    /// </summary>
    public class BindingUnusualProperties : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.ListBox lstFonts;
        internal System.Windows.Forms.Label lblSampleText;
        internal System.Windows.Forms.ListBox lstColors;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public BindingUnusualProperties()
        {
            //
            // 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.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.lstFonts = new System.Windows.Forms.ListBox();
            this.lblSampleText = new System.Windows.Forms.Label();
            this.lstColors = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(210, 13);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(140, 12);
            this.Label2.TabIndex = 9;
            this.Label2.Text = "Choose a Font:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(14, 13);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(140, 12);
            this.Label1.TabIndex = 8;
            this.Label1.Text = "Choose a Color:";
            // 
            // lstFonts
            // 
            this.lstFonts.Location = new System.Drawing.Point(210, 29);
            this.lstFonts.Name = "lstFonts";
            this.lstFonts.Size = new System.Drawing.Size(180, 134);
            this.lstFonts.TabIndex = 7;
            // 
            // lblSampleText
            // 
            this.lblSampleText.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lblSampleText.Location = new System.Drawing.Point(18, 185);
            this.lblSampleText.Name = "lblSampleText";
            this.lblSampleText.Size = new System.Drawing.Size(372, 96);
            this.lblSampleText.TabIndex = 6;
            this.lblSampleText.Text = "Click an item in one of the lists above to change the font or color of this text." +
                " Once the initial conditions are set up (i.e., the binding), this operation happ" +
                "ens automatically.";
            // 
            // lstColors
            // 
            this.lstColors.Location = new System.Drawing.Point(14, 29);
            this.lstColors.Name = "lstColors";
            this.lstColors.Size = new System.Drawing.Size(176, 134);
            this.lstColors.TabIndex = 5;
            // 
            // BindingUnusualProperties
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(404, 294);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Label2,
                                                                          this.Label1,
                                                                          this.lstFonts,
                                                                          this.lblSampleText,
                                                                          this.lstColors});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "BindingUnusualProperties";
            this.Text = "Binding Unusual Properties";
            this.Load += new System.EventHandler(this.BindingUnusualProperties_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void BindingUnusualProperties_Load(object sender, System.EventArgs e)
        {
            // These are our final data sources: two ArrayList objects.
            ArrayList fontObjList = new ArrayList();
            ArrayList colorObjList = new ArrayList();

            // The InstalledFonts collection allows us to enumerate installed fonts.
            // Each FontFamily needs to be converted to a genuine Font object
            // before it is suitable for data binding to the Control.Font property.
            InstalledFontCollection InstalledFonts = new InstalledFontCollection();
            foreach (FontFamily family in InstalledFonts.Families)
            {
                try
                {
                    fontObjList.Add(new Font(family, 12));
                }
                catch
                {
                    // We end up here if the font could not be created
                    // with the default style.
                }
            }

            // In order to retrieve the list of colors, we need to first retrieve
            // the strings for the KnownColor enumeration, and then convert each one
            // into a suitable color object.
            string[] colorNames;
            colorNames = System.Enum.GetNames(typeof(KnownColor));
            TypeConverter cnvrt = TypeDescriptor.GetConverter(typeof(KnownColor));

            foreach (string colorName in colorNames)
            {
                colorObjList.Add(Color.FromKnownColor((KnownColor)cnvrt.ConvertFromString(colorName)));
            }

            // We can now bind both our list controls.
            lstColors.DataSource = colorObjList;
            lstColors.DisplayMember = "Name";
            lstFonts.DataSource = fontObjList;
            lstFonts.DisplayMember = "Name";

            // The label is bound to both data sources.
            lblSampleText.DataBindings.Add("ForeColor", colorObjList, "");
            lblSampleText.DataBindings.Add("Font", fontObjList, "");

        }

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


           
          


Object ListBox Data Binding 2

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;

namespace DataBinding101
{
    /// <summary>
    /// Summary description for ObjectListBinding2.
    /// </summary>
    public class ObjectListBinding2 : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ListBox lstCity;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ObjectListBinding2()
        {
            //
            // 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.lstCity = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // lstCity
            // 
            this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstCity.IntegralHeight = false;
            this.lstCity.Location = new System.Drawing.Point(4, 9);
            this.lstCity.Name = "lstCity";
            this.lstCity.Size = new System.Drawing.Size(248, 216);
            this.lstCity.TabIndex = 3;
            // 
            // ObjectListBinding2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(260, 234);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lstCity});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ObjectListBinding2";
            this.Text = "ObjectListBinding2";
            this.Load += new System.EventHandler(this.ObjectListBinding2_Load);
            this.ResumeLayout(false);

        }
        #endregion

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

        private void ObjectListBinding2_Load(object sender, System.EventArgs e)
        {
            City2[] cityChoices = {new City2("Seattle", "U.S.A."), 
                                     new City2("New York", "U.S.A."), new City2("Tokyo", "Japan"), 
                                     new City2("Montreal", "Canada")};

            lstCity.DataSource = cityChoices;

        }
    
    }

    public class City2
    {
        private string name;
        private string country;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
            }
        }

        public City2(string name, string country)
        {
            this.Name = name;
            this.Country = country;
        }

        public override string ToString()
        {
            return Name + ", " + Country;
        }

    }

}