C ++ Builder master advancement (3) Design DBTreeView Components in BCB (continued)

zhaozj2021-02-16  31

// Continued

Void __fastcall tdbtreeview :: FillChildtreenodes (int ParentID, Ttreenode * Node, Bool Nest)

{/ Pack the child node with the specified value

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

ANSISUSTRING STRSQL, STRTEXT;

INT IID, IParentID;

Int iimageIndex, IselectedIndex;

TTREENODE * TREENODE;

TDBTREENODEDATA NODEDATA;

/ / Set the parameters of the data set

AQuery-> DatabaseName = (TTable *) (fdataLink-> DataSource-> Dataset) -> DatabaseName; // Setting Database Name

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

AQuery-> SQL-> CLEAR (); // Clear the original SQL statement

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

STRSQL = fparentidfield "=: pid";

AQuery-> SQL-> Add (strsql); // Specify a new SQL statement

AQuery-> parambyname ("pid") -> asinteger = ParentId;

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

this-> items-> beginupdate (); // Start updating DBTREEVIEW component display

While (! AQuery-> EOF) // Not until the end of the data set

{

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 the relevant data obtained above to the new node

IIMAGEINDEX = ISELECTEDINDEX = -1; // Set node bitmap and selection time bitmap

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

Treenode-> ImageIndex = iimageIndex; // Set node bitmap and selected time bitmap

Treenode-> SELECTEDINDEX = ISELECTEDINDEX;

NodeData.id = IID; // Get the node master identity, the parent ID, and add a new node according to it

NodeData.parentId = iParentID;

AddDataTonode (Treenode, NodeData);

If (Nest) FillChildTreenodes (IID, TREENODE); // If the specified parameter NEST is TRUE, the recursive call is used to populate all sub-nodes.

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

this-> items-> endupdate (); // End DBTREEVIEW component update

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

Delete aquery; / / Delete temporary created data set controls

}

Void __fastcall tdbtreeview :: adddatatonode (Ttreenode * Node, TDBTREENODEDATA & DATA)

{// Add data to node

TDBTREENODEDATA * PDATA = New TDBTREENODEDATA;

* pdata = data;

Node-> Data = PDATA;

}

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

TDBTREENODEDATA __FASTCALL TDBTREEVIEW :: getnodedata (ttreenode * node)

{// acquire the data of the specified node

Return * (TDBTREENODEDATA *) (Node-> DATA);

}

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

Void __fastcall tdbtreeview :: loaded (void)

{// call the original LOAD method

TCUSTOMTREEVIEW :: loaded ();

/ * TODO: LOAD * /

}

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

Void __fastcall tdbtreeview :: NOTIFICATION (Classes :: Tcomponent * Acomponent, Classes :: TOPERATION OPERATION)

{

// Event response

TCUSTOMTREEVIEW :: NOTIFICATION (Acomponent, Operation); // Call the original method

IF ((Operation == Opremove) && (ACOMPONENT == DataSource)) // If the operation is Opremove and the data connection is not empty and the specified component is a data source, then

DataSource = NULL; // Make the data source is empty

}

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

Void __fastcall tdbtreeview :: Edit (Const tagtvitema & item)

{// Edit Node

TCUSTOMTREEVIEW :: Edit (item); // Call the original method

If (FallowModifyDB) // If you allow modification, then

{

TTREENODE * NODE;

Ansistring DatabaseName = ((Ttable *) (fdataLink-> DataSource-> Dataset) -> DatabaseName;

Ansistring Tablename = ((Ttable *) (fdataLink-> DataSource-> Dataset) -> TABLENAME;

TDBTREENODEDATA NODEDATA;

TQuery * AQuery = New TQuery (this); // Newly built a dataset

ANSISUSTRING STRSQL;

IF (item.State & TVIF_PARAM! = 0) Node = (Ttreenode *) (Item.lParam); Else Node = items-> getnode (item.hitem);

Nodedata = getnodedata (node);

strsql = "update" Tablename "set" fdisplayfield "=: newdisptext where" fprimaryidfield "=: id"; // Update the data set with the specified condition

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

AQuery-> DatabaseName = DatabaseName; // Set database connection

AQuery-> SQL-> CLEAR (); // Clear the original SQL statement

AQuery-> sql-> add (strsql); // Add SQL statement

AQuery-> parambyname ("newdisptext") -> asstring = node-> text; // assigns the parameters in SQL

AQuery-> parambyname ("id") -> asinteger = nodedata.id;

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

Delete aquery; / / Delete temporary created data set controls

}

}

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

Void __fastcall tdbtreeview :: expand (ttreenode * node)

{// Expand the specified node

TCUSTOMTREEVIEW :: Expand (node); // Call the original method

TDBTREENODEDATA NODEDATA;

TTREENODE * ANODE;

For (anode = node-> getfirstchild (); anode; anode = node-> getNextChild (anode)) // Traverse the child node of the specified node

{

NodeData = getnodedata (anode); // get node data

If (anode-> count == 0) // If the node data is 0, the child node is filled.

Fillchildtreenodes (NodeData.ID, Anode);

}

}

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

Void __fastcall tdbtreeview :: Keydown (Word & Key, Classes :: TshiftState Shift)

{

// Keyboard processing

TwinControl :: KeyDown (Key, Shift); // Call the original keyboard processing method

IF (key == vk_f2 && shift == tshiftState ()) // if it is Shift F2

{

/ * Handle 'F2' Key * /

IF (this-> selected! = null) // Edit the currently selected node

this-> selected-> edittext ();

}

}

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

Void __fastcall tdbtreeview :: change (ttreenode * node) {// Handling node change event

TCUSTOMTREEVIEW :: Change (node); // Call the original method

Static ttreenode * OldNode = NULL;

TTREENODE * SELECTEDNODE = this-> SELECTED; / / Get selected node pointer

TDBTREENODEDATA NODEDATA;

TTable * atable = (ttable *) (fdataLink-> DataSource-> DataSet); // Specify a data set

IF (OldNode == SELECTEDNODE) ​​RETURN; // If the old node is equal to the selected node, return directly

IF (atable == null) return; // If the data set is directly returned

IF (SELECTEDNODE == NULL) RETURN; // Return directly if there is no selected node

NodeData = getnodedata (selectedNode); / / get the specified node data

ATABLE-> setKey (); // Find data records as specified

ATABLE-> FieldByname (FPRIMARYIDFIELD) -> asinteger = nodedata.id;

ATABLE-> gotoKey ();

OldNode = SELECTEDNODE; // Old node is equal to the selected node

}

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

Bool __fastcall tdbtreeview :: CustomDrawItem (Ttreenode * Node, TcustomDrawState Stage, Bool & PaintImages)

{

// Self-painted node

Bool result; // Call the original self-painted method

Result = TCUSTOMTREEVIEW :: CustomDrawItem (Node, State, Stage, PaintImages);

/ * Todo: ... * /

// TDBTREENODEDATA NODEDATA = GetNodedata (Node);

Return Result;

}

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

Void __fastcall tdbtreeview :: fullxpand (void)

{// Expand all nodes

If (! Active) // If the dbtreeview is not an active state, an exception is thrown

Throw Exception ("DBTREEVIEW IS INACTIVE.");

this-> items-> beginupdate (); // Start updating DBTREEVIEW

ClearalLnodes (); // Clear all nodes

FillChildtreenodes (0, null, true); // Fill child nodes

this-> items-> endupdate (); // End Update

TCUSTOMTREEVIEW :: fullxpand (); // Call the original method to expand all nodes

}

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

Void __fastcall tfieldnameproperty :: getValues ​​(Classes :: TgetstrProc PROC)

{// get a field value

INT I;

TDBTreeView * AdbtreeView; AdbtreeView = (TDBTREEVIEW *) getcomponent (0);

IF (AdbtreeView-> DataSource! = null) // If the DBTREEVIEW data source is not empty, then

{// Traverse all fields and fill it into the relevant field properties

For (i = 0; i datasource-> dataset-> fieldcount; i )

Proc (AdbtreeView-> DataSource-> DataSet-> Fields-> Fields [i] -> fieldname);

}

Else

{/ / Otherwise throwing anomalies

Throw Exception ("Missing Datasource Property.");

}

}

TTYPEINFO * ANSISUSTRINGTYPEINFO (VOID)

{

// Define type information

TtypeInfo * TypeInfo = New TTYPEINFO;

TYPEINFO-> Name = "ANSISTRING";

TypeInfo-> Kind = tklstring;

Return TypeInfo;

}

"Seeing a DBTREEVIEW development summary"

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

New Post(0)