Most popular

วันอาทิตย์ที่ 24 กรกฎาคม พ.ศ. 2554

เริ่มต้นสร้าง Custom Control ใน .Net C# ตอนที่ 2

จากตอนที่แล้ว (http://systemdevman.blogspot.com/2011/07/customize-control-net-c-1.html) ผู้เขียนได้แสดงการสร้าง Custom Control ที่ทำการ Inheritance (สืบทอด) มาจาก Control ต้นแบบคือ Button (ปุ่มกด)
ในตอนที่ 2 นี้ผู้เขียนจะแสดงการสืบทอดจาก Control DataGridView ทำให้เป็น Grid สำเร็จรูปเพื่อแสดงชุดข้อมูลที่ต้องการ เพื่อให้ท่านผู้อ่านได้เข้าใจในแนวคิดการสร้าง Custom Control เพื่อประยุคใช้งานมากขึ้น

เพิ่มไฟล์ Custom Control เข้ามาใน Project

ในที่นี้ผู้เขียนตั้งชื่อไฟล์เป็น myGridView.cs
ในไฟล์จะได้คลาสเริ่มต้นที่สืบทอดมาจาก Control เหมือนเดิม
ผู้เขียนจะเปลี่ยนให้สืบทอดมาจาก DataGridView แทน
โดยในตัวอย่างนี้จะทำให้ DataGridView มีการเลือก Column Set ที่กำหนดไว้ให้ใช้งานอยู่แล้ว ประกอบด้วย กลุ่มข้อมูลสินค้า , กลุ่มข้อมูลพนักงาน และกลุ่มข้อมูลจังหวัด

คำสั่งทั้งหมดมีดังนี้
    //สร้างประเภทของข้อมูล

    public enum ColumnSet { Employee, Product, Province };
    //คลาส myGridView ที่สืบทอดมาจาก Control DataGridView
    public partial class myGridView : DataGridView
    {
        //ประกาศตัวแปร selectColSet ให้เก็บค่าชนิด ColumnSet ที่สร้างไว้
        private ColumnSet selectColSet;


        //เพิ่มคำอธิบาย Property
        [Description("เลือกกลุ่มข้อมูล")]
        //ใช้การ Encapsulation สร้าง Property SelectColSet เพื่อให้เข้าถึงค่าของ selectColSet จากภายนอก
        public ColumnSet SelectColSet
        {
            get { return selectColSet; }
            set { selectColSet = value; }
        }


        //method กำหนดกลุ่มของ Column สำหรับข้อมูลจังหวัด
        private void provinceColSet()
        {
            this.Columns.Add("PV_CODE", "รหัสจังหวัด");
            this.Columns.Add("PV_THNAME", "ชื่อจังหวัดภาษาไทย");
            this.Columns.Add("PV_ENNAME", "ชื่อจังหวัดภาษาอังกฤษ");
            this.Columns.Add("PV_ZONE", "ภาค");
            this.Columns.Add("PV_POSTCODE", "รหัสไปรษณีย์");
        }
    
        //method กำหนดกลุ่มของ Column สำหรับข้อมูลสินค้า
        private void productColSet()
        {
            this.Columns.Add("PD_CODE", "รหัสสินค้า");
            this.Columns.Add("PD_NAME", "ชื่อสินค้า");
            this.Columns.Add("PD_UNIT", "หน่วย");
            this.Columns.Add("PD_PRICEUNIT", "ราคาต่อหน่วย");
        }


        //method กำหนดกลุ่มของ Column สำหรับข้อมูลพนักงาน
        private void employeColSet()
        {
            this.Columns.Add("FIRSTNAME", "ชื่อจริง");
            this.Columns.Add("LASTNAME", "นามสกุล");
            this.Columns.Add("AGE", "อายุ");
            this.Columns.Add("SEX", "เพศ");
            this.Columns.Add("POSITION", "ตำแหน่ง");
        }


        //Constructor
        public myGridView()
        {
            InitializeComponent();
        }


        //method ในการวาด Control
        protected override void OnPaint(PaintEventArgs pe)
        {
            //อ้างอิงใช้คุณสมบัติ OnPaint ของ DataGridView คลาสแม่
            base.OnPaint(pe);


            //ตรวจสอบว่ามี Column อยู่แล้วหรือไม่ เพื่อให้ทำการวาดครั้งเดียว เนื่องจาก OnPaint ทำงานแบบลูป
            if (this.ColumnCount < 1)
            {
                //หากมีการเลือกค่ากลุ่ม Column ใดก็ให้วาดโครงสร้างกลุ่ม Column นั้นๆ
                switch (this.selectColSet)
                {
                    case ColumnSet.Employee: this.employeColSet(); break;
                    case ColumnSet.Product: this.productColSet(); break;
                    case ColumnSet.Province: this.provinceColSet(); break;
                }
            }
        }
    }


เสร็จแล้วให้ทำการ Compile และ Build Project จะเห็น myGridView แสดงใน ToolBox

Control myGridView ใน ToolBox
นำ Control myGridView มาวางบนฟอร์มจะปรากฎ Column ตามที่กำหนดใน Property
ในหน้าต่าง Properties ของ myGridView ที่ Property SelectColSet พบชื่อระบุกลุ่ม Column ให้เลือกใช้งานได้อย่างสะดวก
พร้อมแสดงคำอธิบายด้านล่างด้วย

1 ความคิดเห็น: