Merhaba arkadaslar bu makalemizde C++ da Xml dosyasindaki verileri okuyup ListView nesnesinde gosterimini saglayacagiz.
C++ da Windows Form nasil ekleriz? Konusunu daha onceki makalede anlatmistim. Onceki makaleye ulaşmak için Buraya tiklayabilirsiniz.
Sekil 1
Sekil 2
MyForm.h
#pragma once
#include <windows.h>
#using <System.dll>
#using <System.Xml.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
namespace cpplistviewreadxmlfile {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Xml;
using namespace System::IO;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ListView^ listView1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->listView1 = (gcnew System::Windows::Forms::ListView());
this->SuspendLayout();
//
// listView1
//
this->listView1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->listView1->HideSelection = false;
this->listView1->Location = System::Drawing::Point(0, 0);
this->listView1->Name = L"listView1";
this->listView1->Size = System::Drawing::Size(664, 332);
this->listView1->TabIndex = 0;
this->listView1->UseCompatibleStateImageBehavior = false;
this->listView1->View = System::Windows::Forms::View::Details;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(666, 332);
this->Controls->Add(this->listView1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{
listView1->GridLines = true;
listView1->Columns->Add("id", 40);
listView1->Columns->Add("title", 150);
listView1->Columns->Add("artist", 150);
listView1->Columns->Add("country", 70);
listView1->Columns->Add("company", 150);
listView1->Columns->Add("price", 50);
listView1->Columns->Add("year", 50);
String^ strFilename = L"catalog.xml";
XmlDocument^ xmlDoc = gcnew XmlDocument;
if (File::Exists(strFilename))
{
xmlDoc->Load(strFilename);
XmlElement^ elmProperty = xmlDoc->DocumentElement;
XmlNodeList^ lstProperties = elmProperty->ChildNodes;
for each (XmlNode ^ node in lstProperties)
{
ListViewItem^ lviProperty = gcnew ListViewItem(node->FirstChild->InnerText); // id
lviProperty->SubItems->Add(node->FirstChild->NextSibling->InnerText); // title
lviProperty->SubItems->Add(node->FirstChild->NextSibling->NextSibling->InnerText); // artist
lviProperty->SubItems->Add(node->FirstChild->NextSibling->NextSibling->NextSibling->InnerText); // country
lviProperty->SubItems->Add(node->FirstChild->NextSibling->NextSibling->NextSibling->NextSibling->InnerText); // company
lviProperty->SubItems->Add(node->FirstChild->NextSibling->NextSibling->NextSibling->NextSibling->NextSibling->InnerText); // price
lviProperty->SubItems->Add(node->FirstChild->NextSibling->NextSibling->NextSibling->NextSibling->NextSibling->NextSibling->InnerText); // year
listView1->Items->Add(lviProperty);
}
}
else
MessageBox::Show("The " + strFilename + " file was not found");
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cpplistviewreadxmlfile::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede gorusmek uzere. Bahadir SAHIN
0 comments:
Yorum Gönder