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

namespace CustomTypeConverter
{
    public partial class frm96CustomTypeConverter : Form
    {
        public frm96CustomTypeConverter()
        {
            InitializeComponent();
        }

        // Make some people.
        private void frm96CustomTypeConverter_Load(object sender, EventArgs e)
        {
            Person[] people =
            {
                new Person() {
                    FirstName="Ann", LastName="Archer",
                    Address=new StreetAddress(){Street="101 Ash Ave", City="Ann Arbor", State="MI", Zip="12345"},
                    Email="Ann@anywhere.com", Phone="703-287-3798"},
                new Person() {
                    FirstName="Ben", LastName="Best",
                    Address=new StreetAddress() {Street="231 Beach Blvd", City="Boulder", State="CO", Zip="24361"},
                    Email="Ben@bestplace.com", Phone="209-783-2918"},
                new Person() {
                    FirstName="Cindy", LastName="Carter",
                    Address=new StreetAddress() {Street="3783 Cherry Ct", City="Cedar Rapids", State="IA", Zip="36268"},
                    Email="CindyCarter@TheCarters.com", Phone="404-329-0182"},
            };

            // Display them in a ListBox.
            peopleListBox.DataSource = people;
        }

        // Display the selected Person in the PropertyGrid.
        private void peopleListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            peoplePropertyGrid.SelectedObject = peopleListBox.SelectedItem;
        }

        private void pgdPeople_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            switch (e.ChangedItem.Label)
            {
                case "Address":
                case "Street":
                case "City":
                case "State":
                case "Zip":
                    // Refresh the PropertyGrid.
                    peoplePropertyGrid.Refresh();
                    break;
                case "FirstName":
                case "LastName":
                    // Refresh the ListBox.
                    peopleListBox.DisplayMember = "FirstName";
                    peopleListBox.DisplayMember = null;
                    break;
            }
        }


        /// <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);
        }

        #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.peoplePropertyGrid = new System.Windows.Forms.PropertyGrid();
            this.peopleListBox = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // pgdPeople
            // 
            this.peoplePropertyGrid.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.peoplePropertyGrid.LineColor = System.Drawing.SystemColors.ControlDark;
            this.peoplePropertyGrid.Location = new System.Drawing.Point(123, 12);
            this.peoplePropertyGrid.Name = "pgdPeople";
            this.peoplePropertyGrid.Size = new System.Drawing.Size(234, 266);
            this.peoplePropertyGrid.TabIndex = 7;
            this.peoplePropertyGrid.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler(this.pgdPeople_PropertyValueChanged);
            // 
            // peopleListBox
            // 
            this.peopleListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left)));
            this.peopleListBox.FormattingEnabled = true;
            this.peopleListBox.IntegralHeight = false;
            this.peopleListBox.Location = new System.Drawing.Point(12, 12);
            this.peopleListBox.Name = "peopleListBox";
            this.peopleListBox.Size = new System.Drawing.Size(105, 266);
            this.peopleListBox.TabIndex = 6;
            this.peopleListBox.SelectedIndexChanged += new System.EventHandler(this.peopleListBox_SelectedIndexChanged);
            // 
            // frm96CustomTypeConverter
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(369, 290);
            this.Controls.Add(this.peoplePropertyGrid);
            this.Controls.Add(this.peopleListBox);
            this.Name = "frm96CustomTypeConverter";
            this.Text = "CustomTypeConverter";
            this.Load += new System.EventHandler(this.frm96CustomTypeConverter_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PropertyGrid peoplePropertyGrid;
        private System.Windows.Forms.ListBox peopleListBox;
    }
}