Building the Address control: Part II


In Part 1 of this article (Part 1; Part 2; Part 3) we have defined our task  and have prepared a basis for creation and testing the Address control. In this part we are going to build the control.

 

We have decided that our control should includes Label and textBox controls. In order to "force" all controls to behave as a whole unit at change of the sizes follow the following order at designing.

 

Inside of the Address project from "Windows Forms" tab drag and drop on the Address[Design] user control a panel named panelAddress (fig.4). Drag and drop on the

 

Figure 4.

 

panelAddress a label named labelAddress; set the Text property to "Address" and the Dock property to "Left" (fig.5). Drag and drop on the panelAddress a panel named

 

Figure 5.

 

panelTextBox; set the Dock property to "Left" (fig 6). Drag and drop on the

 

Figure 6.

 

panelTextBox  a textbox named textBoxAddress; set the Dock property to "Fill"(fig. 7).

 

Figure 7.

 

OK ! We have finished our design (see fig. 8).

 

Figure 8.

 

Now we can pass to the FormAddress[Design] . Drag and drop on the FormAddres labels , textboxes , etc. (see table 1 and fig. 9).

 

Table 1.

Control Name Text
label labelCityList Cities
label labelSymbolFind Symbol
label labelCityFind City
label labelCity City
label labelStreet Street
label labelBuilding #
label labelApartment Apartment
label labelEntrance Entrance
label labelZIP ZIP
groupBox groupBox1 Find
textBox textBoxCitySymbolFind
textBox textBoxCityFind
textBox textBoxCity
textBox textBoxStreet
textBox textBoxBuilding
textBox textBoxApartment
textBox textBoxEntrance
textBox textBoxZIP
listBox listBoxCity
button buttonOK OK
button buttonClean Clean
button buttonExit Exit

 

Figure 9.

 

The Address control and FormAddress code should includes properties  ( such as Street, Apartment, City, DataTable of cities and so on) and methods , that allow to carry out our  task.

 

Namespaces used in the Address control and FormAddress :

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

 

Add to the Address.cs code page the following code:

 

#region "forClass"

//value of the width textBoxAddress property; default value = 100

int textBox_Width = 100;

//value of the textBoxAddress height property; default value = 26

int textBox_Height = 26;

//value of the labelAddress width property; default value = 40

int label_Width = 40;

//value of the textBoxAddress Multiline property; default value = "true"

bool textBox_Multiline = true;

string Symbol_City = "000000";

string Street = "Green Street";

string Building = "5";

string Apartment = "9";

string Entrance = "A";

string ZIP = "99999";

string ADDRESS = "ADDRESS";

string City = "Sun City";

DataTable dt_Cities = new DataTable ();

string Value_Member ="";

string Display_Member = "";

//Length of the Symbol_City string that has to be used for "find" process

int Length_Symbol = 6;

#endregion

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

//The method  that allows to adjust the control sizes

//according to the sizes of the textBox

private void init()

{

          panelTextBox.Width = textBox_Width ;

          panelAddress.Width = textBox_Width + label_Width + 8;

          panelAddress.Height = C_TextBox_Height ;

          this.Height = C_TextBox_Height + 2;

          this.Width = panelAddress.Width + 2;

}

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

//The method showAddress that shows the FormAddress form

//as a modal dialog if the adr_OK property of the FormAddress

//equals "true"

private void showAddress()

{

          FormAddress fAddress = new FormAddress ();

          fillProperties_Form(fAddress);

          fAddress.ShowDialog ();

          if (fAddress.adr_OK )

          {

                   getFromProperties_Form (fAddress);

          }

}

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

//The method fillProperties_Form that sets

//the FormAddress properties

private void fillProperties_Form(FormAddress f)

{

          ADDRESS = getAddress();

          f.adr_APARTMENT = Apartment ;

          f.adr_BUILDING = Building  ;

          f.adr_ENTRANCE = Entrance ;

          f.adr_ZIP = ZIP;

          f.adr_SYMBOL_CITY = Symbol_City ;

          f.adr_STREET = Street;

          f.adr_CITY = City;

          f.adr_Address = ADDRESS;

          f.adr_Display_Member = Display_Member ;

          f.adr_Value_Member = Value_Member ;

          f.adr_DataTableCities = dt_Cities ;

          f.adr_lengthCode = Length_Symbol ;

}

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

//The method getFromProperties_Form that sets

//the properties of the control

private void getFromProperties_Form(FormAddress f)

{

          this.C_APARTMENT = f.adr_APARTMENT ;

          this.C_BUILDING = f.adr_BUILDING  ;

          this.C_ENTRANCE = f.adr_ENTRANCE;

          this.C_ZIP = f.adr_ZIP;

          this.C_SYMBOL_CITY = f.adr_SYMBOL_CITY;

          this.C_STREET = f.adr_STREET;

          this.C_CITY = f.adr_CITY;

          this.C_Address = f.adr_Address;

}

 

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

//The method that returns "Address" as one string

private string getAddress()

{

          string sResult="";

          sResult= Street + " " + Building + "/" + Apartment +

                   ", " + Entrance + " " + ZIP + ", " + City;

          return (sResult);

}

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

//The method textBoxAddress_KeyUp that executes the showAddress  

// method on the KeyUp event of the textBoxAddress

private void textBoxAddress_KeyUp

          (object sender, System.Windows.Forms.KeyEventArgs e)

{

          showAddress ();

}

 

//The method labelAddress_DoubleClick that executes  

//the showAddress method on the DoubleClick event of the labelAddress

private void labelAddress_DoubleClick(object sender, System.EventArgs e)

{

          showAddress ();

}

 

//One of opportunities to apply the init() method is

//to "call" it from the Address_Resize event

private void Address_Resize(object sender, System.EventArgs e)

{

//                          init();

}

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

#region "properties"

//The C_TextBox_Width property that allows to set:

//the panelTextBox width property,

//the panelAddress width property,

//the Address control width property

public int C_TextBox_Width

{

          get

          {

                   return textBox_Width;

          }

          set

          {

                   textBox_Width =value ;

                   panelTextBox.Width =textBox_Width ;

                   panelAddress.Width =textBox_Width + label_Width + 8;

                   this.Width = panelAddress.Width + 2;

          }

}

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

public int C_TextBox_Height

{

          get

          {

                   return textBox_Height;

          }

          set

          {

                   textBox_Height = value ;

                   panelAddress.Height = C_TextBox_Height ;

                   this.Height = C_TextBox_Height + 2;

          }

}

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

public bool  C_TextBox_Multiline

{

          get

          {

                   return textBox_Multiline;

          }

          set

          {

                   textBox_Multiline = value;

                   textBoxAddress.Multiline = textBox_Multiline;

          }

}

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

public string C_CITY

{

          get

          {

                   return City;

          }

          set

          {

                   City = value;

          }

}

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

public string C_SYMBOL_CITY

{

          get

          {

                   return Symbol_City;

          }

          set

          {

                   Symbol_City = value;

          }

}

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

public string C_STREET

{

          get

          {

                   return Street;

          }

          set

          {

                   Street = value;

          }

}

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

public string C_BUILDING

{

          get

          {

                   return Building;

          }

          set

          {

                   Building = value;

          }

}

 

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

public string C_APARTMENT

{

          get

          {

                   return Apartment;

          }

          set

          {

                   Apartment = value;

          }

}

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

public string C_ENTRANCE

{

          get

          {

                   return Entrance;

          }

          set

          {

                   Entrance = value;

          }

}

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

public string C_ZIP

{

          get

          {

                   return ZIP;

          }

          set

          {

                   ZIP = value;

          }

}

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

public string C_Address

{

          get

          {

                   ADDRESS = getAddress ();

                   return ADDRESS;

          }

          set

          {

                   ADDRESS = value;

                   textBoxAddress.Text = ADDRESS;  

          }

}

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

//The C_LengthSymbol property that sets the length of

//the Symbol_City string that has to be used for "find" process

//of the FormAddress form.

public int C_LengthSymbol

{

          set

          {

                   Length_Symbol = value;

          }

}

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

public DataTable C_DataTableCities

{

          set

          {

                   dt_Cities = value;

          }

}

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

public string C_Value_Member

{

          set

          {

                   Value_Member = value;

          }

}

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

public string C_Display_Member

{

          set

          {

                   Display_Member = value;

          }

}

#endregion

 

Add to the FormAddress.cs code page the following code:

 

#region "forClass"

string Symbol_City = "";

string Street = "Green Street";

string Building = "5";

string Apartment = "9";

string Entrance = "A";

string ZIP = "99999";

string City = "Sun City";

string ADDRESS = "";

DataTable dt_Cities = new DataTable();

string Value_Member ="";

string Display_Member = "";

string Title = "ADDRESS CONTROL";

bool b_OK = false;

int length_Code ;

#endregion

 

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

private string getAddress()

{

          string sResult="";

          sResult= Street + " " + Building + "/" + Apartment +

                   ", " + Entrance + " " + ZIP + ", " + City;

          return (sResult);

}

 

 

private void FormAddress_Load(object sender, System.EventArgs e)

{

          try

          {

                   listBoxCity.SelectedValue = Symbol_City ;

          }

          catch

          {

                   listBoxCity.SelectedIndex =0;

                   Symbol_City = listBoxCity.SelectedValue.ToString().Trim();

          }

}

 

private void listBoxCity_SelectedIndexChanged(object sender, System.EventArgs e)

{

          try

          {

                   textBoxCity.Text =listBoxCity.Text.Trim ();

          }

          catch

          {

                   textBoxCity.Text = "";

          }   

}

 

//The method textBoxCitySymbolFind_TextChanged that   

//executes "find" proccess on the TextChanged event

//of the textBoxCitySymbolFind.

private void textBoxCitySymbolFind_TextChanged(object sender, System.EventArgs e)

{

          string sCode = textBoxCitySymbolFind.Text.Trim ();  

          if (sCode.Length == length_Code)

          {

                   listBoxCity.SelectedValue = sCode;          

          }

}

 

//The method textBoxCityFind_TextChanged that   

//executes "find" proccess on the TextChanged event

//of the textBoxCityFind.

private void textBoxCityFind_TextChanged(object sender, System.EventArgs e)

{

          listBoxCity.SelectedIndex = listBoxCity.FindString(textBoxCityFind.Text.Trim (),0);               

}

 

//The method buttonOK_Click that   

//executes "Close" proccess on the Click event

//of the buttonOK and allows to pass all changes of

//the FormAddress to the Address control

private void buttonOK_Click(object sender, System.EventArgs e)

{

          b_OK = true;

          this.Close ();

}

 

 

private void buttonClean_Click(object sender, System.EventArgs e)

{

          b_OK = false;

          textBoxCity.Text = "";

          textBoxCitySymbolFind.Text = "";

          textBoxCityFind.Text = "";

          textBoxApartment.Text = "";

          textBoxBuilding.Text = "";

          textBoxEntrance.Text = "";

          textBoxZIP.Text = "";

          textBoxStreet.Text = "";               

}

 

//The method buttonExit_Click that   

//executes "Close" proccess on the Click event

//of the buttonExit and doesn't allow to pass changes  

//of the FormAddress to the Address control

private void buttonExit_Click(object sender, System.EventArgs e)

{

          b_OK = false;

          this.Close ();           

}

 

#region "properties"

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

public string adr_SYMBOL_CITY

{

          get

          {

                   try

                   {

                             Symbol_City = listBoxCity.SelectedValue.ToString().Trim();

                   }

                   catch

                   {

                             listBoxCity.SelectedIndex =0;

                             Symbol_City = listBoxCity.SelectedValue.ToString().Trim();

                   }

                   return Symbol_City;

          }

          set

          {

                   Symbol_City = value;

          }

}

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

public string adr_STREET

{

          get

          {

                   Street = textBoxStreet.Text  ;

                   return Street;

          }

          set

          {

                   Street = value;

                   textBoxStreet.Text = Street ;

          }

}

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

public string adr_BUILDING

{

          get

          {

                   Building = textBoxBuilding.Text ;

                   return Building;

          }

          set

          {

                   Building = value;

                   textBoxBuilding.Text = Building;

          }

}

 

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

public string adr_APARTMENT

{

          get

          {

                   Apartment = textBoxApartment.Text ;

                   return Apartment;

          }

          set

          {

                   Apartment = value;

                   textBoxApartment.Text = Apartment;

          }

}

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

public string adr_ENTRANCE

{

          get

          {

                   Entrance = textBoxEntrance.Text ;

                   return Entrance;

          }

          set

          {

                   Entrance = value;

                   textBoxEntrance.Text = Entrance;

          }

}

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

public string adr_ZIP

{

          get

          {

                   ZIP = textBoxZIP.Text ;

                   return ZIP;

          }

          set

          {

                   ZIP = value;

                   textBoxZIP.Text = ZIP;

          }

}

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

public string adr_CITY

{

          get

          {

                   City = textBoxCity.Text ;

                   return City;

          }

          set

          {

                   City = value;

                   textBoxCity.Text = City;

          }

}

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

public string adr_Address

{

          get

          {

                   ADDRESS = getAddress ();

                   return ADDRESS;

          }

          set

          {

                   ADDRESS = value;

          }

}

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

public bool adr_OK

{

          get

          {

                   return b_OK;

          }

}

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

public int adr_lengthCode

{

          set

          {

                   length_Code = value;

          }

}

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

 

public DataTable adr_DataTableCities

{

          set

          {

                   dt_Cities = value;

                   if (dt_Cities.Rows.Count == 0 )

                   {

                             MessageBox.Show (" There are no records in " +

                                      "DataTableCities " ,Title,MessageBoxButtons.OK ,

                                      MessageBoxIcon.Stop );

                   }

                   else

                   {

                             try

                             {

                                      listBoxCity.ValueMember = Value_Member ;

                                      listBoxCity.DisplayMember = Display_Member ;

                                      listBoxCity.DataSource = dt_Cities;

                             }

                             catch(Exception E)

                             {

                                      MessageBox.Show (" There is a problem with " +

                                                "DataTableCities : " +

                                                E.Message,Title,MessageBoxButtons.OK ,

                                                MessageBoxIcon.Stop );

                             }

                   }

          }

}

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

public string adr_Value_Member

{

          set

          {

                   Value_Member = value;

          }

}

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

public string adr_Display_Member

{

          set

          {

                   Display_Member = value;

          }

}

#endregion

 

OK! Now we have our Address control (don't forget to build/rebuild solution!). As usually, let's drink a cup of coffee before we shall pass to the third part.

 

Good luck in programming !


Similar Articles