Войти

Показать полную графическую версию : [C++Builder 2009] TTreeView


crashtuak
17-04-2009, 19:11
Вот есть ини файл:
[Options]
CategoryCount=5
ItemsCount=6

[Category]
1=System
2=Video
3=Internet
4=Games
5=Drivers

[Item_1]
name="Item 1"
category=3

[Item_2]
name="Item 2"
category=3

[Item_3]
name="Item 3"
category=4

[Item_4]
name="Item 4"
category=2

[Item_5]
name="Item 5"
category=5

[Item_6]
name="Item 6"
category=1
Потом вот код построения с ини файла дерева:
void __fastcall TForm1::Button4Click(TObject *Sender)
{
int CategoryCount = StrToInt(category->ReadString("Options", "CategoryCount", NULL));
int ItemCount = StrToInt(category->ReadString("Options", "ItemsCount", NULL));
for(int cat = 1; cat <= CategoryCount; cat++) {
TreeView1->Items->Add(NULL,category->ReadString("Category", IntToStr(cat), NULL));
for(int item = 1; item <= ItemCount; item++) {
if(category->ReadString("Item_"+IntToStr(item), "category", NULL) == IntToStr(cat))
{
TreeView1->Items->Add(TreeView1->Items->Item[cat-1],category->ReadString("Item_"+IntToStr(item), "name", NULL));
}
}
}
}
Но при таком коде все сабитемы добавляются не как саб итемы а как хрен знает что :):
http://pic.ipicture.ru/uploads/090417/thumbs/57jivumS4k.jpg (http://ipicture.ru/Gallery/Viewfull/17491734.html)
А надо так:
http://pic.ipicture.ru/uploads/090417/thumbs/T332RBWUM2.jpg (http://ipicture.ru/Gallery/Viewfull/17491829.html)

Alan85
17-04-2009, 21:08
TreeView1->Items->Add(TreeView1->Items->Item[cat-1],category->ReadString("Item_"+IntToStr(item), "name", NULL)); »
замени на
TreeView1->Items->AddChild(TreeView1->Items->Item[cat-1],category->ReadString("Item_"+IntToStr(item), "name", NULL));
и будет все ок

crashtuak
20-04-2009, 20:39
замени на
Код:
TreeView1->Items->AddChild(TreeView1->Items->Item[cat-1],category->ReadString("Item_"+IntToStr(item), "name", NULL));
и будет все ок »
Заменил и вот смотрите прикрепленный рисунок.
Вопрос не снят.

Alan85
20-04-2009, 22:24
Извини не все учел в цикле (не проверил). Вот как вышло:

TTreeNode *tn; // тут добавил указатель
int CategoryCount = StrToInt(category->ReadString("Options", "CategoryCount", NULL));
int ItemCount = StrToInt(category->ReadString("Options", "ItemsCount", NULL));
for(int cat = 1; cat <= CategoryCount; cat++) {
tn=TreeView1->Items->Add(NULL,category->ReadString("Category", IntToStr(cat), NULL)); // тут сохранил в нем адрес текущей ветки
for(int item = 1; item <= ItemCount; item++) {
if(category->ReadString("Item_"+IntToStr(item), "category", NULL) == IntToStr(cat))
{
TreeView1->Items->AddChild(tn,category->ReadString("Item_"+IntToStr(item), "name", NULL)); // ну а тут добавил в текущую ветку все что надо
}
}
}

crashtuak
21-04-2009, 07:10
Alan85, Спасибо, но я вот додумался вот к этому:
int iii=0;
int CategoryCount = StrToInt(category->ReadString("Options", "CategoryCount", NULL));
int ItemCount = StrToInt(category->ReadString("Options", "ItemsCount", NULL));
for(int cat = 1; cat <= CategoryCount; cat++) {
TreeView1->Items->Add(NULL,category->ReadString("Category", IntToStr(cat), NULL));
for(int item = 1; item <= ItemCount; item++) {
if(category->ReadString("Item_"+IntToStr(item), "category", NULL) == IntToStr(cat))
{
TreeView1->Items->AddChild(TreeView1->Items->Item[cat-1+iii],category->ReadString("Item_"+IntToStr(item), "name", NULL));
iii=iii+1 ;
}
}
}Но там соравно был один недостаток, но который можно было обойти. Но твой вариант круче :).




© OSzone.net 2001-2012