Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Visual C# » Building control derived from Windows Forms control in Visual Studio 2005: Part II

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

Reusability and component oriented development is one of the features of .NET development. This approach may be applied to any project. In this article I share how you can build your own Windows control derived from an Windows Forms control in Visual Studio 2005. The examples are written using C#.

Author Rank :
Page Views : 15706
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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).

 

 

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).

 

 

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 !

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Michael Livshitz
Michael is a lead programmer for a large technological firm. He has experience working with .NET projects (ASP.NET and Windows) since 2002. Currently used languages are C#, VB.NET, T-SQL and JavaScript. He also has experience working with C, Visual Basic, Oracle, SQL Server, and Access.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.