C ++ Builder First Learning and Answer (7)

zhaozj2021-02-16  51

53) Q: Can you introduce TTREENODES and TTREENODE?

A: In the BCB, the node of the tree view is managed by the TTREENODES object, and the tree view can be dynamically increased, deleted, inserted, and mobile nodes, etc. through this object. Each node is a TTREENODE object.

Main properties and functions of TTREENODE:

Count: This property is used to return the number of child nodes owned by a node. It does not include its child nodes.

Item: It is declared as: _property ttreenode * item [int index]; this property is used to access a child node for that node. The parameter index is the location of its child node in all child nodes.

INDEX: This property is used to return the location of the node in all child nodes of its parent nodes.

TEXT: This property is used to set or return the tag of the node.

DELETE: This function is used to delete the node itself.

DELETECHILDREN: This function is used to delete all child nodes of the node.

EditText: This function is used to edit nodes in place.

INDEXOF: It declares as: int _fastcall indexof (Ttreenode * value); this function will return the serial number of a child node of the node. The child node is specified by the parameter value. If the node specified by the parameter value is not the child node of the node, the function will return -1.

Main properties and functions of TTREENODES:

COUNT: This property is used to return the number of nodes in the tree view.

Add: It is: tTREENODE * _FASTCALL Add (Ttreenode, const system :: ANSISTRING S); this function will add a node in the tree view, the new node becomes the last child node of the parent node specified by the parameter node, The parameter S is the label of the new node.

AddChild: It is: tTREENODE * _FASTCALL AddChild (Ttreenode, const system :: ANSISUSTRING S); added new node as the last child node of the node specified by the parameter node.

AddChildfirst: It declares: ttreenode * _fastcall addressfirst (ttreenode, const system :: ANSISTRING S); This function is different from addChild is the first child node for the newly added node as the Node node.

AddFirst: It is: tTREENODE * _FASTCALL AddFirst (Ttreenode, const system :: ANSISTRING S); this function is similar to add, and the new node is the first child node of the parent node of the Node node.

CLEAR: This function will clear the entire tree view.

DELETE: It declares: void _fastcall delete (ttreenode * node); this function will delete the node specified by the parameter node.

INSERT: It is: tTREENODE _FASTCALL INSERT (TTREENODE * Node, const system :: Ansistring s); this function is used to insert a new node after the node specified by the parameter node, and the label of the new node is specified by the parameter S.

7. Graphic list component listview

54) Q: The TreeView component enables the contents of the resource manager's left display area, but it can display the list in a variety of different ways, such as large icons, small icon, simple list mode, and detailed list. What should this be implemented in C Builder?

A: This can be implemented with a listView component, and the ListView component is similar to the list box, but from the component's properties are similar to TreeView. The list established by the ListtTiew component can be displayed in a variety of different ways, such as large icons, small icons, simple list methods, and detailed list. Part of the attribute in the ListView component is the same as the properties in the TreeView component, such as Items, StateImages, etc. There are still some properties to be unique to ListView.

The LARGEIMAGES attribute is used to set a list of icons that store large icons. When the list is displayed in a large icon display, the list uses the icon in this icon list.

The SmallImages property is used to set the list of icons that store small icons. When the list is displayed in a small icon display, the list uses the icon in this icon list.

MultiSelected properties determine if the user can select multiple items at the same time. The default is false.

The property ViewStyle determines the display style and can take such a value.

Vsicon: The list is displayed in large icon, which can be dragged and dropped. Only the first layer node is displayed, and the placement method is determined by Arrangement.

VssMallicon: The list is displayed in a small icon, which can be dragged and dropped. Only the first layer node is displayed, and the placement method is determined by Arrangement.

vslist: Displayed in a simple list and cannot be dragged. The node is vertically placed, only the first layer node is displayed, and the Arrangement does not work.

vSreport: Displayed in a detailed list, the information displayed can be divided into multiple columns. The leftmost column shows icons and text, and the remaining columns displays more information. The node is vertically placed vertically, each layer has been expanded, only the set of columns is only played, and Arrangement does not work.

Attribute Columns When the property ViewStyle is set to vSreport, each item in the list can be divided into multiple columns, which is used to set the number of columns and columns.

When designing, pressing the omitted number button in the Column Attribute Value column to open a listView Columns editor, which can generate and edit the title of column and column in this editor.

When the value of the property ColumnClick is true, the title in the list can be used as a button. The most common application of this property is in the resource manager of Windows 95, click the file name or several other column headings, the file in the list will Re-order according to the file name.

Properties Items contains a collection of all items in the list. Similar to the items attribute in the TreeView component, this property also has a project editor, you can use this editor to increase, delete items, set items, and title, etc. Its use method is basically the same as the Items editor of the TreeView component.

The iconOptions property is used to set the icon display mode. It also contains several sub-properties:

(1) Arrangement property: When this property is equal to IATOP, the icon is displayed from left to right in the top of the ListView component; when IALEFT is equal, the icon is arranged from top to bottom in the left side of the component.

(2) AutoarRange Properties: When True, the icon will be automatically rearranged when the icon is added, deleted, or the mobile icon.

(3) WrapText Attributes: When True, the title will be in the title when the title of the project exceeds the width of the icon.

In addition, there is a running timelination

The Selected property is used to return the selected item in the list.

The TopItem property is used to return the top-top items currently visible in the list.

55) Q: Please write a program to help me understand and memorize the ListView control? A: Ok, choose the New Application menu item of the File menu, open an empty project, place a listView control on the form, two imagelist controls, a listbox control.

Select the ImageList1 control, set its height and width properties to 32. Double-click ImageList1 control to open an image list edit box. Click the Add button to select a large icon file. Click the Open button. Then click the OK button. The same method adds a small icon to the imagelist2 control. Select the TreeView control to modify its largeimages attribute to ImageList1.

Modify its SmallImages attribute to ImageList2. At the ListView control, click the right mouse button, select the Columns Editor option (of course, we can click "..." on the right side of the property columns). The Columns editing dialog is popped up. Click the Add New Tool button to join four lists, select them, modify their CAPTION properties to name, size, type, modification time.

Close the list editing dialog.

Click the right mouse button on the ListView control (same we just click on "..." on the right side of the ITMES property), select the Items Editor option. The Items Editing dialog box is popped (almost similar to TreeView's Items Editor), and enter the tree structure document. Here, click on the "New Item" button to create a new item, the "NewSubItem" button is used to set the child of this item. "Delete" is used to delete an item, including its child. In the Sublimation Editing box, CAPTION is the title of this item, and the other two icons.

The onclick event code for writing the list box is as follows:

Void __fastcall tform1 :: ListBox1click (Tobject * Sender)

{

Switch (ListBox1-> ItemIndex)

{

Case 0: listview1-> viewstyle = vsicon;

Break;

Case 1: listview1-> viewstyle = vssmallicon;

Break;

Case 2: listview1-> viewstyle = vslist;

Break;

Case 3: listview1-> viewstyle = vSreport;

Break;

}

}

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

Change the viewstyle attribute in ListView to vSreport, otherwise you will not get the above display method.

Finally, compile and run this program, choose different types, we can see the effect of the TreeView control.

56) Q: Can I talk about the icon management in TreeView, ListView?

A: The graphic list component is difficult to understand, the main problem is not easy to master it using the icon. The graphic list component manages all icons using a component named ImageList, iMageList provides an index number for each icon, so you don't have to indicate the name of the icon when the TreeView or ListView component reference is referenced. With ImageList central management icons, users use the icon index marks to reduce the burden of programmers from a certain angle, but also make them a lot of numbers that must be remembered when they are programmed. Weaked the advantage of ImageList to a certain extent, perhaps with a better way in the version after C Builder. 57) Q: How to create a list view?

A: We will explain. I will establish an interface as shown.

And write the following code:

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

#include

#pragma HDRSTOP

#include "unit1.h"

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

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

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

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER)

{

}

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

Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)

{

Const Char Names [6] [3] [10] =

{{"Guangdong Province", "Guangzhou", "South China"}

{"Shanghai", "Shanghai", "East China"},

{"Beijing", "Beijing", "North China"}

{"Liaoning Province", "Shenyang", "Northeast"}

{"Hubei Province", "Wuhan", "Huazhong"}

{"Yunnan Province", "Kunming", "Southwest"}}; // Prepare the text

TListColumn * newcolumn; // Create a column

TListItem * ListItem; // Create an items

ListView1-> ViewStyle = vSreport; // Define display mode

Button3-> enabled = false;

NewColumn = listview1-> columns-> add (); // increase bar

NewColumn-> CAPTION = "province"; // Add a name of the column

Newcolumn = listview1-> columns-> add ();

Newcolumn-> CAPTION = "Provincial Association";

Newcolumn = listview1-> columns-> add ();

NewColumn-> CAPTION = "Orientation"; for (INT i = 0; i <6; i )

{

ListItem = listview1-> items-> add ();

ListItem-> caption = names [i] [0]; // Add a parent node

ListItem-> Subitems-> add (names [i] [1]); // Add sub-node

ListItem-> Subitems-> Add (Names [i] [2]);

}

} / * Some friends here always feel why also create TListColumn * newcolumn;

TLISTITEM * LISTITEM; actually we want to do this, we will open columns when you create it, Items is equal to another project, they don't exist, we have to use this method, so we can only use this method to dynamically Created J * /

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

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

TLISTITEM * TEMP;

Temp = listview1-> items-> add ();

TEMP-> CAPTION = Edit1-> text;

Temp-> Subitems-> Add (edit2-> text);

Temp-> Subitems-> Add (edit3-> text);

}

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

Void __fastcall tform1 :: button2click (Tobject * Sender)

{

Listview1-> items-> clear ();

}

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

Void __fastcall tform1 :: radiobutton1click (Tobject * Sender)

{

Listview1-> viewstyle = vSreport;

Button3-> enabled = false;

}

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

Void __fastcall tform1 :: radiobutton2click (TOBJECT * SENDER)

{

Listview1-> viewstyle = vsicon;

Button3-> enabled = true;

}

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

Void __fastcall tform1 :: button3click (TOBJECT * Sender)

{

Listview1-> alphasort (); // This function is to put the items in the list view follow their label alphabetical order, and successfully returns True.

}

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

58) Q: What do I want to do with TreeView, ListView?

A: We are generally done by onchange or onchangeing events. We look at their applications and differences through an example, we put a TreeView and a MEMO component in the form, and in TreeView's onchange and onchangeing respectively. Write the following code: // -------------------------------------------- -------------------------------

#include

#pragma HDRSTOP

#include "unit1.h"

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

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

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

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER)

{

}

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

Void __fastcall tform1 :: tvwchange (Ttreenode * Node)

{

IF (node-> text == "Liaoning")

Memo1-> Lines-> Add ("I'm Change!");

}

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

Void __fastcall tform1 :: tvwchangeing (TOBJECT * Sender, Ttreenode * Node,

Bool & AllowChange

{

ALLOWCHANGE = FALSE;

IF (node-> text == "Liaoning")

Memo1-> Lines-> Add ("I'm Chaning!");

}

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

The operation is as follows:

Only onchangeing has played a role, why? Look at the following code:

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

#include

#pragma HDRSTOP

#include "unit1.h"

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

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

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

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER) {

}

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

Void __fastcall tform1 :: tvwchange (Ttreenode * Node)

{

IF (node-> text == "Liaoning")

Memo1-> Lines-> Add ("I'm Change!");

}

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

Void __fastcall tform1 :: tvwchangeing (TOBJECT * Sender, Ttreenode * Node,

Bool & AllowChange

{

// allowchange = false; this is the same as that writes this sentence into allowchange = true

IF (node-> text == "Liaoning")

Memo1-> Lines-> Add ("I'm Chaning!");

Two events have played, and onchangeing happened first onchange, so the reason is clear: all of this is caused by onchangeing parameters, so pay attention to this parameter.

In ListView, just change the above Node-> text to item-> caption.

(to be continued)

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

New Post(0)