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


Join the conversation! Your thoughts help the community grow.