C ++ Builder master advanced (3) Design DBTREEVIEW components in BCB (continued)

zhaozj2021-02-16  40

// DBTREEVIEW Detailed design .CPP file

__fastcall tdbtreeview :: TDBTREEVIEW (Tcomponent * Owner)

: TTREEVIEW (OWNER)

{

/ / Initialize the relevant data in the constructor

FDATALINK = New TfieldDataLink; // Establish a data connection class

Fparentidfield = ""; // Set the node father ID

FPRIMARYIDFIELD = ""; // Set the node master identity to empty

Fdisplayfield = ""; // Set the display field to empty

Factive = false; // Set to non-active status

FallowModifyDB = false; // Setting the data set is not allowed to modify

}

__fastcall tdbtreeView :: ~ tdbtreeview ()

{// Release the resource allocated in the destructor

DELETE FDATALINK; / / Release Data Connection Class

}

Void __fastcall tdbtreeview :: setActive (Bool Value)

{

/ / Set the status is activity

If (Factive! = Value) // If the specified state is the same as the current state, do not process, return directly

{//otherwise

If (value) // If the specified value is true

{

IF (fdataLink-> DataSource == NULL) // If you do not specify a data source, an exception is thrown

Throw Exception ("Missing Datasource Property.");

IF (fdataLink-> DataSource-> DataSet == NULL) / / Otherwise, if the data set is not specified in the data source, an exception is also thrown

Throw Exception ("INVALID DATASOURCE.");

If (fparentidfield == ") // If the parent identity field is empty, throw an exception

Throw Exception ("Missing ParentidField Property);

If (fprimaryidfield == ") // If the primary identification field is empty, throw an exception

Throw Exception ("Missing Primaryidfield Property.");

If (fdisplayfield == ") // If the display field is empty, an exception is thrown

Throw Exception ("Missing DisplayField Property.");

// Supplementary description: From the above exceptions, you should not see the environment you need to this component:

/ / Must specify the data source, dataset, node parent identification field, primary sign field, display field

CLEARALLNODES (); // Delete all nodes

FillTreenodes (0, null); // Plug all data to each node

}

Else

{// If the specified value is false

CLEARALLNODES (); // Delete all nodes

}

Factive = value; // Assign the specified value to Factive

}

}

Void __fastcall tdbtreeview :: setDataSource (DB :: TDataSource * Value)

{// set data source

IF (value! = fdataLink-> DataSource) // If the specified value is different from the current value,

{

FdataLink-> DataSource = value; // Set the current value of the data to the specified value}

}

TDataSource * __fastcall tdbtreeview :: getDataSource ()

{// Get data source

Return fdataLink-> DataSource; // Return to the data source

}

Void __fastcall tdbtreeview :: clearllnodes ()

{// Remove all nodes, call the related method of TreeView to translate the selection node delete

For (int i = 0; I items-> count; i )

Delete (TDBTREENODEDATA *) (Items-> Item [i] -> data);

This-> items-> beginupdate ();

This-> items-> clear ();

This-> items-> endupdate ();

}

Void __fastcall tdbtreeview :: Filltreenodes (int ParentID, Ttreenode * Node)

{// Plip data to node

TQuery * AQuery = New TQuery (this); // Create a data set control

ANSISUSTRING STRSQL, STRTEXT;

INT IID, IParentID;

Int iimageIndex, IselectedIndex;

TTREENODE * TREENODE;

TDBTREENODEDATA NODEDATA;

// open the relevant data table with the specified condition

AQuery-> DatabaseName = ((ttable *) (fdataLink-> DataSource-> Dataset) -> DatabaseName;

AQuery-> Close (); // Turns off the data set

AQuery-> sql-> clear (); // Empty the original query condition

strsql = "SELECT *" ((Ttable *) (fdataLink-> DataSource-> Dataset) -> Tablename "Where";

STRSQL = fparentidfield "=: pid";

AQuery-> sql-> add (strsql); // Specify new query conditions

AQuery-> parambyname ("pid") -> asinteger = parentID; // get the parent identification field content

AQuery-> Open (); // Open Data Set

this-> items-> beginupdate (); // Preparing to update DBTREEVIEW display

While (! AQuery-> EOF) // Data does not go to the data intent, then

{

Strtext = AQuery-> FieldByName (fdisplayfield) -> asstring; // get the display field content

IID = AQUERY-> FieldByName (fPrimaryIDfield) -> asinteger; // get the primary identification field content

iParentID = AQUERY-> FieldByName (fparentidfield) -> asinteger; // get the parent identification field content

Treenode = this-> items-> addchild (node, strtext); // Add this sub-node

IIMAGEINDEX = ISELECTEDINDINDEX = -1; // Set node bitmap and selection time bitmap set to empty (-1)

IF (FonsetImageIndex) FonsetImageIndex (this, IID, iParentID, Treenode-> Level, IIMAGEX, ISELECTEDINDEX); // Trigger related events if the bitmap setting changes

Treenode-> ImageIndex = iimageIndex; // Setting the node bitmap and the bitmap

Treenode-> SELECTEDINDEX = ISELECTEDINDEX;

NodeData.id = IID; // Set node identity and parent identity

NodeData.parentId = iParentID;

AddDataTonode (Treenode, NodeData); // Add data to the specified node

FillChildtreenodes (IID, TREENODE, FALSE); // Fill child nodes

AQuery-> Next (); // Move to the next record

}

this-> items-> endupdate (); // Update DBTREEVIEW components

AQuery-> Close (); // Turns off the data set

Delete aquery; / / Delete temporary created data set controls

}

"Not complete"

转载请注明原文地址:https://www.9cbs.com/read-26450.html

New Post(0)