C++ da My Sql Veritabanindaki Verileri ComboBox ta Gostermek ve Secili Indeksteki Veriyi TextBoxta Gosterimini Saglamak
Merhaba arkadaslar bu makalemizde C++ da My Sql veritabanindaki tablomuzdaki verileri Combobox ta gosterecegiz. Sonrasinda Comboboxta secili indeksine bagli verileri TextBox icerisinde gosterimini saglayacagiz. TextBox in Multiline ozelligini true yapin. Bu ornegimizde Combobox taki secili yazarin kitaplarini textbox ta gosterece iz.
C++ da Windows Form nasil ekleriz? Konusunu daha onceki makalede anlatmistim. onceki makaleye ulasmak icin Buraya t klayabilirsiniz.
Ilk once Manage Nuget Packages browser kismindan libmysql i kurunuz. Daha sonra References kismina sag tiklayalim. Add Referencesi secip tiklayalim. Aclan Add references penceresinde MySql.Data yi secip, OK butonuna tiklayalim.
Biz bu ornegimizde MySql de book semasindaki worldclassics tablosuna baglanacagiz. (Schema:book)
ekil 1
MyForm.h
#pragma once
namespace cppcombobox {
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::ComboBox^ comboBox1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
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->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// comboBox1
//
this->comboBox1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 14.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->comboBox1->ForeColor = System::Drawing::Color::Tomato;
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(12, 12);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(356, 31);
this->comboBox1->TabIndex = 0;
this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &MyForm::comboBox1_SelectedIndexChanged);
//
// textBox1
//
this->textBox1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 14.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->textBox1->ForeColor = System::Drawing::SystemColors::MenuHighlight;
this->textBox1->Location = System::Drawing::Point(12, 49);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
this->textBox1->Size = System::Drawing::Size(365, 242);
this->textBox1->TabIndex = 1;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(389, 327);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->comboBox1);
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";
MySqlConnection^ con = gcnew MySqlConnection(str);
MySqlCommand^ cmd = gcnew MySqlCommand("Select * From book.worldclassics; ", con);
MySqlDataReader^ rd;
try
{
MySqlDataAdapter^ da = gcnew MySqlDataAdapter();
da->SelectCommand = cmd;
DataTable^ dt = gcnew DataTable();
da->Fill(dt);
BindingSource^ bs = gcnew BindingSource();
bs->DataSource = dt;
comboBox1->DataSource = bs;
comboBox1->DisplayMember = "Author";
comboBox1->ValueMember = "Book";
textBox1->Text = "";
}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
}
}
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
String^ str = comboBox1->SelectedIndex.ToString() +
L" " +
comboBox1->SelectedValue;
textBox1->Text = str + "\r\n" + textBox1->Text;
//textBox1->Focus();
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cppcombobox::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görü mek üzere. Bahad r AH N