C++ ListViewda Arama Yapmak ve Aranan Kayıt Bilgilerini TextBoxta Göstermek
Merhaba arkadaşlar bu makalemizde C++ da ListView nesnesinde My Sql veritabanındaki verilerin gösterimini sağlayacağız. TextBox a girileni ListView da arayacağız. Bulunan kayıt satırının arka plan ve yazı rengini değiştireceğiz. Bulunan kayıt bilgilerini TextBox ta 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.
İlk
önce Manage Nuget Packages browser kısmından libmysql i kurunuz. Daha sonra
References kısmına sağ tıklayalım. Add Referencesi seçip tıklayalım. Açılan Add
references penceresnde MySql.Data yı seçip, OK butonuna tıklayalım.
Biz
bu örneğimizde MySql de book şemasındaki worldclassics tablosuna bağlanacağız. (Schema:book)
MySql
veritabanımızı Şekil 1 deki gibi oluşturuyoruz.
Formumuzu çalıştırdığımızda My Sql
Veritabanımızdaki dbemployee tablomuz DataGridView nesnesinde aşağıdaki gibi
görülecektir.
Şekil 1
MyForm.h
#pragma once
namespace cpplistviewshowtextbox {
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
MySql::Data::MySqlClient;
/// <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::Windows::Forms::TextBox^ txtSearch;
private:
System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private:
System::Windows::Forms::Label^ label3;
private:
System::Windows::Forms::Label^ label4;
private:
System::Windows::Forms::TextBox^ txtId;
private:
System::Windows::Forms::TextBox^ txtAuthor;
private: System::Windows::Forms::TextBox^ txtBook;
private:
System::Windows::Forms::TextBox^ txtPrice;
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->txtSearch
= (gcnew
System::Windows::Forms::TextBox());
this->label1 =
(gcnew
System::Windows::Forms::Label());
this->label2 =
(gcnew
System::Windows::Forms::Label());
this->label3 =
(gcnew
System::Windows::Forms::Label());
this->label4 =
(gcnew
System::Windows::Forms::Label());
this->txtId = (gcnew
System::Windows::Forms::TextBox());
this->txtAuthor
= (gcnew
System::Windows::Forms::TextBox());
this->txtBook =
(gcnew
System::Windows::Forms::TextBox());
this->txtPrice
= (gcnew
System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// listView1
//
this->listView1->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->listView1->HideSelection
= false;
this->listView1->Location
= System::Drawing::Point(4, 56);
this->listView1->Name
= L"listView1";
this->listView1->Size
= System::Drawing::Size(768, 589);
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);
//
// txtSearch
//
this->txtSearch->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtSearch->Location
= System::Drawing::Point(12, 12);
this->txtSearch->Name
= L"txtSearch";
this->txtSearch->Size
= System::Drawing::Size(232, 27);
this->txtSearch->TabIndex
= 2;
this->txtSearch->TextChanged
+= gcnew System::EventHandler(this, &MyForm::txtSearch_TextChanged);
//
// label1
//
this->label1->AutoSize
= true;
this->label1->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label1->Location
= System::Drawing::Point(12, 682);
this->label1->Name
= L"label1";
this->label1->Size
= System::Drawing::Size(65, 22);
this->label1->TabIndex
= 3;
this->label1->Text
= L"Book
Id :";
//
// label2
//
this->label2->AutoSize
= true;
this->label2->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label2->Location
= System::Drawing::Point(12, 726);
this->label2->Name
= L"label2";
this->label2->Size
= System::Drawing::Size(59, 22);
this->label2->TabIndex
= 4;
this->label2->Text
= L"Author
:";
//
// label3
//
this->label3->AutoSize
= true;
this->label3->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label3->Location
= System::Drawing::Point(362, 687);
this->label3->Name
= L"label3";
this->label3->Size
= System::Drawing::Size(50, 22);
this->label3->TabIndex
= 5;
this->label3->Text
= L"Book
:";
//
// label4
//
this->label4->AutoSize
= true;
this->label4->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label4->Location
= System::Drawing::Point(362, 731);
this->label4->Name
= L"label4";
this->label4->Size
= System::Drawing::Size(50, 22);
this->label4->TabIndex
= 6;
this->label4->Text
= L"Price
:";
//
// txtId
//
this->txtId->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtId->Location
= System::Drawing::Point(95, 682);
this->txtId->Name
= L"txtId";
this->txtId->Size
= System::Drawing::Size(243, 27);
this->txtId->TabIndex
= 7;
//
// txtAuthor
//
this->txtAuthor->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtAuthor->Location
= System::Drawing::Point(95, 723);
this->txtAuthor->Name
= L"txtAuthor";
this->txtAuthor->Size
= System::Drawing::Size(243, 27);
this->txtAuthor->TabIndex
= 8;
//
// txtBook
//
this->txtBook->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtBook->Location
= System::Drawing::Point(418, 684);
this->txtBook->Name
= L"txtBook";
this->txtBook->Size
= System::Drawing::Size(345, 27);
this->txtBook->TabIndex
= 9;
//
// txtPrice
//
this->txtPrice->Font
= (gcnew
System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtPrice->Location
= System::Drawing::Point(418, 726);
this->txtPrice->Name
= L"txtPrice";
this->txtPrice->Size
= System::Drawing::Size(345, 27);
this->txtPrice->TabIndex
= 10;
//
// MyForm
//
this->AutoScaleDimensions
= System::Drawing::SizeF(6, 13);
this->AutoScaleMode
= System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize
= System::Drawing::Size(775, 808);
this->Controls->Add(this->txtPrice);
this->Controls->Add(this->txtBook);
this->Controls->Add(this->txtAuthor);
this->Controls->Add(this->txtId);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->txtSearch);
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)
{
String^ str = L"datasource=localhost;port=3306;username=root;password=2344";
listView1->GridLines
= true;
listView1->Columns->Add("Id", 40);
listView1->Columns->Add("Author", 250);
listView1->Columns->Add("Book", 380);
listView1->Columns->Add("Price", 80);
String^ sql = "Select
* From book.worldclassics";
MySqlConnection^ con = gcnew MySqlConnection();
con->ConnectionString
= str;
con->Open();
MySqlCommand^ cmd = gcnew MySqlCommand(sql, con);
MySqlDataAdapter^ da = gcnew MySqlDataAdapter();
MySqlDataReader^ dr =
cmd->ExecuteReader();
ListViewItem^ item = gcnew ListViewItem();
while
(dr->Read())
for (int i = 1; i
<= 1; i++)
{
item =
listView1->Items->Add(dr->GetInt32(0).ToString());
item->SubItems->Add(dr->GetString(1));
item->SubItems->Add(dr->GetString(2));
item->SubItems->Add(dr->GetString(3));
}
con->Close();
txtSearch->Text
= "1";
}
private: System::Void
txtSearch_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
txtId->Clear();
txtAuthor->Clear();
txtBook->Clear();
txtPrice->Clear();
int id = System::Convert::ToInt32(txtSearch->Text);
for (int i = 0; i <
listView1->Items->Count; i++)
{
listView1->Items[System::Convert::ToInt32(id -
1)]->Selected = true;
listView1->Items[i]->BackColor
= System::Drawing::Color::White;
listView1->Items[i]->ForeColor
= System::Drawing::Color::Black;
if (id =
System::Convert::ToInt32(txtSearch->Text))
{
listView1->Items[id
- 1]->BackColor = System::Drawing::Color::OrangeRed;
listView1->Items[id
- 1]->ForeColor = System::Drawing::Color::White;
}
}
}
private: System::Void
listView1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
if
(listView1->SelectedItems->Count)
{
txtId->Text
= listView1->SelectedItems[0]->SubItems[0]->Text;
txtAuthor->Text
= listView1->SelectedItems[0]->SubItems[1]->Text;
txtBook->Text
= listView1->SelectedItems[0]->SubItems[2]->Text;
txtPrice->Text
= listView1->SelectedItems[0]->SubItems[3]->Text;
}
listView1->SelectedItems->Clear();
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cpplistviewshowtextbox::MyForm form;
Application::Run(% form);
}
Bir
makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır
ŞAHİN
0 comments:
Yorum Gönder