Building the Address control: Part I


Each developer has some "idle"  period when one of  her/his projects is already finished (may be that needed to be made it  is one-two "clicks"), and the next one should be begun in a month. Well! Now you do have some time to share your idea with someone for whom it can be interesting at present, because tomorrow (in a week or in a month) you are going to "plunge into the battle" with  your new project or/and tools.

 

OK! In this article (Part 1; Part 2; Part 3) I am going to share some ideas which do work in real projects. I will begin with the Address control (by the way, it can be some Family control, etc.; the core is the idea of such kind control), which is used as "independent unit" (dll) and allows  in your .NET applications to avoid "tiresome" coding and designing. The examples are written using C#.

 

We will define our task as follows. On a form our control should look (see fig.1) as Label and TextBox with the text property like that: Street NumBuilding/NumApartment, NumEntrance, City, ZIP (for example: Green Street 5/9, A, Sun City, 99999). With any trying to change text our control changes to editing mode: some special address form appears which includes a few TextBoxes (TextBoxStreet, TextBoxBuilding, etc.) and  special controls for choosing/finding city (see fig.2); the list of cities is got as dataTable.

 

 

Figure 1.

 

 

Figure 2.

 

First of all we should create a solution for building our control. The solution consists of three projects: the first project ( Windows Control Library) named "Address" has Output Type of  "Class Library"; the second one named "UC_Test" has Output Type of "Windows Application"; the third project named "GetData" has Output Type of  "Class Library". The "Address" project includes such items as "User Control" named "Address" and "Windows Form" named "FormAddress". In fact this project will be our "independent unit" (dll). The project "UC_Test" includes only one Windows Form named "Form1". With its help we just test our "Address" control. The "GetData" project includes only one class (so far! In the next projects we can add some methods to this class and add some more classes) named "GetDataHelp". This project is "independent unit" (dll) and helps us to get needed data.The solution is shown on fig.3.

 

 

Figure 3.

 

Now we are ready to add  some code. First of all we should "supply" our control with a dataTable  (see above: the list of cities). In order to test our control we don't need any special database. We just can create a method which returns dataTable (or dataSet) in a format which we require. Let it be table "DataTableCities" with two columns: the first column named "SYMBOL_CITY" , and the second column named "CITY". Of course we don't need the real name of cities (New York, Washington, etc); to test control we just need something like : City_1, City_A, etc. And the last : depending on our desire the quantity (ten, one hundred, thousands) of the cities in our table should be changed. With all this in mind we can add to the GetDataHelp.cs class the following:

 

to "using" region: 

 

using System.Data;

using System.Data.SqlClient; 

 

to the class itself :

 

//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";

            }

            dRow["CITY"] = sHelp;

            dt.Rows.Add (dRow);

      }

      ds.Tables.Add(dt);

      return ds;

}

 

OK! Now we have solution with three projects, we have dataset what we need - that is: all is ready to create our "Address" control. As usually, let's drink a cup of coffee before we shall pass to the second part.

 

Good luck in programming !