Well, I have been struggling with this some more and I am the same error
message which said something to the effect that I couldn't overload the
contructor...
The errors I am getting now are:
G:\Projects\Rep ort Manager\Win Client\ComboBox .cs(27): The type or namespace
name 'dr' could not be found (are you missing a using directive or an
assembly reference?) ...( five of these)
and
G:\Projects\Rep ort Manager\Win Client\ComboBox .cs(19): The type or namespace
name 'IDataReader' could not be found (are you missing a using directive or
an assembly reference?) (one of these)
Here is my revamped sub-classed ComboBox
using System;
using System.Windows. Forms;
using Microsoft.Pract ices.Enterprise Library.Data;
namespace com.onproject.c ontrols
{
/// <summary>
/// Summary description for ComboBox.
/// </summary>
public class ComboBox : System.Windows. Forms.ComboBox
{
// I overroad the ComboBox constructor to pass in a Database Object
// from the Microsoft.Pract ices.Enterprise Library.Data application
block
public ComboBox(Databa se _db)
// I don't know if this was necessary, but I decided to use a
private copy
{
Database db = _db;
using (IDataReader dr = db.ExecuteReade r
(CommandType.Te xt, "Select id, name,state,root _org_id," +
"original_i d from dbo.period"))
{
while (dr.Read())
{ /*************** *************** *************** *******
*
*************** *************** *************** *******/
this.Items.Add( new Win_Client.Peri od(dr.GetInt32( 0),
dr.GetString(1) , dr.GetInt32(2),
dr.GetInt32(3), dr.GetInt32(4)) );
}
}
}
}
}
In Form1 here are my initial declarations of the control at the top of the
Form1 class
private com.onproject.c ontrols.ComboBo x cmbPeriod;
Database db = DatabaseFactory .CreateDatabase ("soComply") ;
Here is the rest of the code regarding cmbPeriod:
//
// cmbPeriod
//
this.cmbPeriod = new com.onproject.c ontrols.ComboBo x(db);
this.cmbPeriod. Location = new System.Drawing. Point(88, 120);
this.cmbPeriod. Name = "cmbPeriod" ;
this.cmbPeriod. Size = new System.Drawing. Size(121, 21);
this.cmbPeriod. TabIndex = 0;
this.cmbPeriod. Text = "cmbPeriod" ;
cmbPeriod.Selec tedIndexChanged += new
System.EventHan dler(this.cmbPe riod_SelectedIn dexChanged);
I have attempted to initialize the cmbPeriod in the Form1 constructor and in
the Form1_Load event handler, both with the same results.
This is making me wonder about the Database parameter that gets passed in...
I think the IDataReader should exist from the passed in reference, but it is
not recognized.
My next attempt will be to put the data loading code into a separate public
method and explicitly invoke that after the other stuff is done.
If you have any ideas about this method does not work, please let me know.
"Lars Behrmann" <la***********@ web.de> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Hi Henry
i don't see where the problem is. It should be
no problem to derive from the ComboBox as you
do it and of course so you will have your own
constructor.
What error message do you get ?
Cheers
Lars Behrmann
_______________ __
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
Henry schrieb:
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.Pract ices.Enterprise Library.Data;
namespace com.onproject.c ontrols
{
/// <summary>
/// Summary description for ComboBox.
/// </summary>
public class ComboBox : System.Windows. Forms.ComboBox
{
public ComboBox(Databa se db)
{
using (IDataReader dr = db.ExecuteReade r
(CommandType.Te xt, "Select id,
name,state,root _org_id," +
"original_i d from dbo.period"))
{
while (dr.Read())
{
this.Items.Add( new Win_Client.Peri od(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{retur n 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,in t _original_id)
{
this.id = _id;
this.name = _name;
this.state = _state;
this.root_org_i d = _root_org_id;
this.original_i d = _original_id;
}
public override string ToString()
{
return name;
}
}
}