Building control derived from Windows Forms control in Visual Studio 2005: Part II

In the first part of this article we have defined our task  and have prepared a basis for creation and testing the ComboBox_C control derived from an Windows Forms ComboBox control. In this part we are going to build and test the control.

Let's pass at once to a code which we should add to our classes.

After adding the needed code to  ComboBox_C.cs the page  will look as follows: 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

 

namespace ComboBox_C

{

    public partial class ComboBox_C : ComboBox//UserControl

    {

        public ComboBox_C()

        {

            InitializeComponent();

        }

 

        #region "forClass"

        bool b_AddRow = false;

        object o_AddValue = "-9";//default "value"

        string s_AddDisplay = "----";//default "display"

        #endregion

 

        #region "properties"

 

        public object C_AddValue

        {

            set

            {

                o_AddValue = value;

            }

            get

            {

                return o_AddValue;

            }

        }

        //-------------------------------------------------

        public string C_AddDisplay

        {

            set

            {

                s_AddDisplay = value;

            }

            get

            {

                return s_AddDisplay;

            }

        }

        //-------------------------------------------------

        //the C_AddRow property that allows to add

        //one item to the top of the items (received

        //from the DataSource property of the control)

        public bool C_AddRow

        {

            set

            {

                b_AddRow = value;

                if (b_AddRow)

                {

                    try

                    {

                        this.DataSource =

                            addEmptyRow((DataTable)this.DataSource,

                                o_AddValue, s_AddDisplay);

                    }

                    catch (Exception ex)

                    {

                        MessageBox.Show(

                            "Control:ComboBox_C//Property: C_AddRow " +

                            "//" + ex.Message,this.Name);

                    }

                }

 

            }

        }

 

        #endregion

 

        private DataTable addEmptyRow(DataTable dtIn, object addValue,

            string addDisplay)

        {

            DataTable dt = new DataTable();

            DataColumn dCol_0 = null;

            DataColumn dCol_1 = null;

            DataRow dRow = null;

            string sColumnName_0 = dtIn.Columns[0].Caption;

            string sColumnName_1 = dtIn.Columns[1].Caption;

            int iCount = 0;

            bool bDoInt = true;

 

            dCol_0 = new DataColumn();

            dCol_1 = new DataColumn();

            dCol_0.ColumnName = sColumnName_0;

            if (addValue.GetType() == Type.GetType("System.Int32"))

            {

                dCol_0.DataType = Type.GetType("System.Int32");

            }

            else

            {

                dCol_0.DataType = Type.GetType("System.String");

            }

 

            dt.Columns.Add(dCol_0);

 

            dCol_1 = new DataColumn();

            dCol_1.ColumnName = sColumnName_1;

            dCol_1.DataType = Type.GetType("System.String");

            dt.Columns.Add(dCol_1);

 

            dRow = dt.NewRow();

            dRow[0] = addValue;

            dRow[1] = addDisplay;

            dt.Rows.Add(dRow);

            int tempFor1 = dtIn.Rows.Count;

            for (iCount = 0; iCount < tempFor1; iCount++)

            {

                dRow = dt.NewRow();

                if (bDoInt)

                {

                    dRow[0] = dtIn.Rows[iCount][0];

                }

                else

                {

                    dRow[0] = dtIn.Rows[iCount][0].ToString();

                }

                dRow[1] = dtIn.Rows[iCount][1];

                dt.Rows.Add(dRow);

            }

            return dt;

        }

    }

} 

 

For  GetDataHelp.cs we will use the code from the article (again! Instead of the GetData project you can use some real database or some dll, like GetData.dll). After adding the needed code to GetDataHelp.cs the page will look as follows:

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient; 

 

namespace GetData

{

    public class GetDataHelp

    {

        //The method getDataSetCities that returns dataSet with

        //one dataTable "DataTableCities". The dataTable consists of

        //two columns :  "SYMBOL_CITY" and "CITY".

        //The dataTable is filled with the help of

        //the "for" loop; the number of the rows is input parameter.

        //

        public DataSet getDataSetCities(int iRows)

        {

            DataTable dt = new DataTable("DataTableCities");

            DataColumn dc_SYMBOL_CITY;

            DataColumn dc_CITY;

            DataRow dRow;

            DataSet ds = new DataSet();

            string sHelp = "";

            string sHelp_0 = "City";

 

            ds.Clear();

            dc_SYMBOL_CITY = new DataColumn("SYMBOL_CITY", Type.GetType("System.String"));

            dt.Columns.Add(dc_SYMBOL_CITY);

            dc_CITY = new DataColumn("CITY", Type.GetType("System.String"));

            dt.Columns.Add(dc_CITY);

            for (int i = 0; i < iRows; i++)

            {

                dRow = dt.NewRow();

                if (i < 10)

                {

                    dRow["SYMBOL_CITY"] = "00000" + i.ToString();

                }

                else

                {

                    if (i < 100 && i >= 10)

                    {

                        dRow["SYMBOL_CITY"] = "0000" + i.ToString();

                    }

                    else

                    {

                        if (i < 1000 && i >= 100)

                        {

                            dRow["SYMBOL_CITY"] = "000" + i.ToString();

                        }

                        else

                        {

                            dRow["SYMBOL_CITY"] = i.ToString();

                        }

                    }

                }

                sHelp = sHelp_0;

                sHelp = sHelp + "_" + i.ToString();

                if (i % 2 == 0)

                {

                    sHelp = sHelp + "g";

                }

                else if (i % 3 == 0)

                {

                    sHelp = sHelp + "gff";

                }

                else if (i % 5 == 0)

                {

                    sHelp = "abc" + sHelp;

                }

                else if (i % 7 == 0)

                {

                    sHelp = "awc" + sHelp;

                }

                else

                {

                    sHelp = "awt" + sHelp + "g";

                }

                if (i == 0)

                {

                    sHelp = "Sun City";

                }

                dRow["CITY"] = sHelp;

                dt.Rows.Add(dRow);

            }

            ds.Tables.Add(dt);

            return ds;

        }

    }

} 

 

Now we can test our ComboBox_C. Drag and drop the control on the Form1(fig. 15).

 

15.GIF

 

Figure 15.

 

On Load event add the follow code:

 

private void Form1_Load(object sender, EventArgs e)

{

    GetData.GetDataHelp getData = new GetData.GetDataHelp();

    comboBox_C1.DataSource = getData.getDataSetCities(100).Tables[0];

    comboBox_C1.DisplayMember = "CITY";

    comboBox_C1.ValueMember = "SYMBOL_CITY";

    comboBox_C1.C_AddValue = "-1";

    comboBox_C1.C_AddDisplay = "I am first; value=" +

        comboBox_C1.C_AddValue.ToString();

    comboBox_C1.C_AddRow = true;

}

 

Now  run the project and be convinced that  all our tasks are carried out (fig. 16).

 

16.GIF

 

Figure 16.

 

CONCLUSION

 

I hope that this article will help you to build your own Windows control derived from an Windows Forms control and to extend the functionality of an existing control as much as you need.

 

Good luck in programming !

Up Next
    Ebook Download
    View all
    Learn
    View all