C++ Array Metoduyla ListView da Verileri Göstermek
Merhaba arkadaşlar bu makalemizde C++ da array metodunu kullanarak listview da verilerin gösterimini sağlayacağız. ListView da seçili satırı label de göstereceğiz.
C++ da Windows Form nasıl ekleriz? Konusunu daha önceki makalede anlatmıştım. Önceki makaleye ulaşmak için Buraya tıklayabilirsiniz.
Şekil 1
MyForm.h
#pragma once
#pragma once
#include <windows.h>
#include<iostream>
namespace cpplistviewarrayofvalues {
using namespace std;
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::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;
private: System::DirectoryServices::DirectorySearcher^ directorySearcher1;
private: System::Windows::Forms::Label^ label1;
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->directorySearcher1 = (gcnew System::DirectoryServices::DirectorySearcher());
this->label1 = (gcnew System::Windows::Forms::Label());
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(278, 590);
this->listView1->TabIndex = 0;
this->listView1->UseCompatibleStateImageBehavior = false;
this->listView1->View = System::Windows::Forms::View::Details;
this->listView1->SelectedIndexChanged += gcnew System::EventHandler(this, &MyForm::listView1_SelectedIndexChanged);
//
// directorySearcher1
//
this->directorySearcher1->ClientTimeout = System::TimeSpan::Parse(L"-00:00:01");
this->directorySearcher1->ServerPageTimeLimit = System::TimeSpan::Parse(L"-00:00:01");
this->directorySearcher1->ServerTimeLimit = System::TimeSpan::Parse(L"-00:00:01");
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label1->Location = System::Drawing::Point(5, 607);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(45, 20);
this->label1->TabIndex = 1;
this->label1->Text = L"label1";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(280, 647);
this->Controls->Add(this->label1);
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);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
listView1->GridLines = true;
listView1->Columns->Add("Id", 100);
listView1->Columns->Add("Car Models", 150);
cli::array<String^>^ arr1 = gcnew cli::array<String^>{"1", "2", "3", "4", "5", "6","7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
"31", "32", "33", "34"};
cli::array<String^>^ arr2 = gcnew cli::array<String^>{"Volvo", "BMW", "Ford", "Mazda",
"Astra", "Corsa", "Insignia", "Zafira", "Mokka",
"Avensis", "Corolla", "Yaris", "Auris", "RAV4",
"126p", "Panda", "Punto", "500", "Tipo",
"A4", "A6", "Q7", "R8", "A7"
"Megane", "Captur", "Scenic", "Kadjar", "Espace"
"Golf", "Passat", "Tiguan", "Beetle", "Touran"
};
ListViewItem^ item;
for (int i = -1; i <31; i++)
{
item = gcnew ListViewItem();
item->Text = Convert::ToString(arr1[i+1]);
item->SubItems->Add(Convert::ToString(arr2[i + 1]));
listView1->Items->Add(item);
item->BackColor = item->Index % 2 == 0 ? System::Drawing::Color::LightCyan : System::Drawing::Color::White;
item->ForeColor = item->Index % 2 == 0 ? System::Drawing::Color::Red : System::Drawing::Color::Navy;
}
}
private: System::Void listView1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
if (listView1->SelectedItems->Count)
{
String^ str1= listView1->SelectedItems[0]->SubItems[0]->Text;
String^ str2 = listView1->SelectedItems[0]->SubItems[1]->Text;
label1->Text = "Selected Car Model: " + listView1->SelectedItems[0]->SubItems[0]->Text + " - " + listView1->SelectedItems[0]->SubItems[1]->Text;
}
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cpplistviewarrayofvalues::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN