Unit1.~cpp
上传用户:wangli
上传日期:2008-04-19
资源大小:3077k
文件大小:5k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include "my_xml.h"
- #pragma hdrstop
- #include "Unit1.h"
- #include "Unit2.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- TListColumn *NewColumn;//创建一个栏
- TListItem *ListItem;//创建一个Items
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- /* myXML xml;
- xml.init("1.xml");
- xmlNode root = xml.root();
- //xmlNode node = root.get_child_node("item1");
- node.set_attribute("日期","2004-11-24");
- node.set_attribute("方式","支出");
- node.set_attribute("金额","88");
- node.set_attribute("说明","买东西");
- node = root.get_child_node("体重");
- ShowMessage(node.value());
- root.set_child_value("身长","'180");
- xml.save(); */
- int ind=lstMain->Selected->Index;
- //ind为要删除的节点号
- _di_IXMLNode node=XMLDoc->DocumentElement;
- int re=node->ChildNodes->Delete(ind);
- XMLDoc->SaveToFile(WideString("1.xml"));
- if (re!=-1)
- lstMain->Items->Delete(ind);
-
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- /*
- lstMain->ViewStyle = vsReport;//定义显示方式
- NewColumn = lstMain->Columns->Add();//增加栏
- NewColumn->Caption = "序号";//添置栏的名称
- NewColumn->Width = 40;
- NewColumn = lstMain->Columns->Add();//增加栏
- NewColumn->Caption = "日期";//添置栏的名称
- NewColumn->Width = 80;
- NewColumn = lstMain->Columns->Add();
- NewColumn->Caption = "方式";
- NewColumn = lstMain->Columns->Add();
- NewColumn->Caption = "金额";
- NewColumn->Width = 80;
- NewColumn = lstMain->Columns->Add();
- NewColumn->Caption = "说明";
- NewColumn->Width = 300; */
- //const TRect &Rect;
- /* const char Names[6][3][10] ={
- {"广东省","广州市","华南"},
- {"上海市", "上海市","华东"},
- {"北京市", "北京市","华北"},
- {"辽宁省", "沈阳市","东北"},
- {"湖北省", "武汉市","华中"},
- {"云南省", "昆明市","西南"}};//准备各项的文字
- for (int i = 0; i < 6; i++)
- {
- ListItem = ListView1->Items->Add();
- ListItem->Caption = Names[i][0];//添置父节点
- ListItem->SubItems->Add(Names[i][1]);//添置子节点
- ListItem->SubItems->Add(Names[i][2]);
- } */
- /* myXML xml;
- xml.init("1.xml");
- xmlNode root = xml.root();
- xmlNode node = root.get_child_node("item1");
- ListItem = ListView1->Items->Add();
- ListItem->Caption = node.attribute("日期");//添置父节点
- ListItem->SubItems->Add(node.attribute("方式"));//添置子节点
- ListItem->SubItems->Add(node.attribute("金额"));
- ListItem->SubItems->Add(node.attribute("说明")); */
- refresh();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::ListView1CustomDrawItem(TCustomListView *Sender,
- TListItem *Item, TCustomDrawState State, bool &DefaultDraw)
- {
- /*if(ListView1->Selected)
- {
- ListView1->ClearSelection();
- Sender->Canvas->Brush->Color=clBlue;
- } */
- //ListView1->Canvas->Brush->Color = clBtnFace;
- /* if (Item->Index%2 != 0)
- ListView1->Canvas->Brush->Color = clBtnFace; */
- }
- //---------------------------------------------------------------------------
- bool __fastcall TForm1::AddTask(int itemnum,String date,
- String method,int account,String con)
- {
- //TODO: Add your source code here
- _di_IXMLNode node=XMLDoc->DocumentElement;
- node=node->AddChild(WideString("Tip"));
- _di_IXMLNode subnode=node->AddChild(WideString("item"));
- subnode->SetText(WideString(itemnum));
- subnode=node->AddChild(WideString("date"));
- subnode->SetText(WideString(date));
- subnode=node->AddChild(WideString("method"));
- subnode->SetText(WideString(method));
- subnode=node->AddChild(WideString("account"));
- subnode->SetText(WideString(account));
- subnode=node->AddChild(WideString("Content"));
- subnode->SetText(WideString(con));
- XMLDoc->SaveToFile(WideString("1.xml"));
- return true;
- } //以上代码很容易理解,读的时候可以对照XML文件的格式
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- //AddTask(1,Date(),"支出",33,"lkdlkdfklkl");
- Form2->Show();
- //
- Form1->refresh();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::refresh()
- {
- XMLDoc->LoadFromFile(WideString("1.xml"));
- //XMLDoc是TXMLDocument的名字
- XMLDoc->Active=true;
- //FillList(); //把XML的内容添加到ListView
- //然后就是如何读取XML的内容(FillList):
- lstMain->Items->BeginUpdate();
- lstMain->Items->Clear();
- _di_IXMLNode node=XMLDoc->DocumentElement;
- //DocumentElement属性用来得到根节点
- _di_IXMLNodeList nodes=node->ChildNodes;
- //得到子节点List
- for(int n=0;n<nodes->Count;n++)
- {
- TListItem* li=lstMain->Items->Add();
- node=nodes->Nodes[n]->ChildNodes->Nodes[0];
- //得到子节点的子节点,也也就是 VTip//Tip//Mode
- int m=String(node->GetText()).ToInt();
- //得到对应节点的Text内容并转换到Int
- li->Caption=m;
- li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[1]->GetText());
- li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[2]->GetText());
- li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[3]->GetText());
- li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[4]->GetText());
- //代码同上
- }
- lstMain->Items->EndUpdate();
- }