H
Henry
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database object from the calling class, but the compliler doesn't like it when I try to provide and overrided contructor. See the code below:
Do I have to stick with the standard ComboBox constructor?
If so, how else could I share a common reference to my data source?
My goal is to encapsulate the data with the object and get the data manipulation code out of the Windows form in which the object is used. I'd like to do this for all my controls as much as possible. I am concerned about duplicating things like data connections and so I was trying to figure out a way to use a common object for that purpose.
using System;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace com.onproject.controls
{
/// <summary>
/// Summary description for ComboBox.
/// </summary>
public class ComboBox : System.Windows.Forms.ComboBox
{
public ComboBox(Database db)
{
using (IDataReader dr = db.ExecuteReader
(CommandType.Text, "Select id, name,state,root_org_id," +
"original_id from dbo.period"))
{
while (dr.Read())
{
this.Items.Add(new Win_Client.Period(dr.GetInt32(0), dr.GetString(1), dr.GetInt32(2),
dr.GetInt32(3), dr.GetInt32(4)));
}
}
}
}
}
I want to use the period.id field as the index for the ComboBox.Items collection, so that I can pass that value to other controls.
I am still not quite sure how to do that.
I made the combobox display the period.name field by overriding the ToString method of the Period class.
//sample of the Period class that I am using to populate
// the combobox.
using System;
namespace Win_Client
{
/// <summary>
/// Summary description for Period.
/// </summary>
public class Period
{
//private fields
private int id;
private string name;
private int state;
private int root_org_id;
private int original_id;
//public properties
public int Id{get{return id;} set{id= value;}}
public int State{get{return state;} set{state= value;}}
public int Root_Org_Id{get{return root_org_id;} set{root_org_id= value;}}
public int Original_Id{get{return original_id;} set{original_id= value;}}
// constructor populates the fields
public Period(int _id, string _name, int _state,
int _root_org_id,int _original_id)
{
this.id = _id;
this.name = _name;
this.state = _state;
this.root_org_id = _root_org_id;
this.original_id = _original_id;
}
public override string ToString()
{
return name;
}
}
}
Do I have to stick with the standard ComboBox constructor?
If so, how else could I share a common reference to my data source?
My goal is to encapsulate the data with the object and get the data manipulation code out of the Windows form in which the object is used. I'd like to do this for all my controls as much as possible. I am concerned about duplicating things like data connections and so I was trying to figure out a way to use a common object for that purpose.
using System;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace com.onproject.controls
{
/// <summary>
/// Summary description for ComboBox.
/// </summary>
public class ComboBox : System.Windows.Forms.ComboBox
{
public ComboBox(Database db)
{
using (IDataReader dr = db.ExecuteReader
(CommandType.Text, "Select id, name,state,root_org_id," +
"original_id from dbo.period"))
{
while (dr.Read())
{
this.Items.Add(new Win_Client.Period(dr.GetInt32(0), dr.GetString(1), dr.GetInt32(2),
dr.GetInt32(3), dr.GetInt32(4)));
}
}
}
}
}
I want to use the period.id field as the index for the ComboBox.Items collection, so that I can pass that value to other controls.
I am still not quite sure how to do that.
I made the combobox display the period.name field by overriding the ToString method of the Period class.
//sample of the Period class that I am using to populate
// the combobox.
using System;
namespace Win_Client
{
/// <summary>
/// Summary description for Period.
/// </summary>
public class Period
{
//private fields
private int id;
private string name;
private int state;
private int root_org_id;
private int original_id;
//public properties
public int Id{get{return id;} set{id= value;}}
public int State{get{return state;} set{state= value;}}
public int Root_Org_Id{get{return root_org_id;} set{root_org_id= value;}}
public int Original_Id{get{return original_id;} set{original_id= value;}}
// constructor populates the fields
public Period(int _id, string _name, int _state,
int _root_org_id,int _original_id)
{
this.id = _id;
this.name = _name;
this.state = _state;
this.root_org_id = _root_org_id;
this.original_id = _original_id;
}
public override string ToString()
{
return name;
}
}
}