#pragma once
#include "Kontakt.h"

namespace Adressbuch {

	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;
	using namespace System::Diagnostics;
	using namespace System::Collections::Generic;
	using namespace System::Data::OleDb;

	/// <summary>
	/// Zusammenfassung für Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	private:
		List<Kontakt^>^ meineKontakte;
		OleDbConnection^ connection;
		OleDbCommand^ command;
		OleDbDataReader^ lesen;	
		int dsindex;

	public:
		Form1(void)
		{
			InitializeComponent();
			//
			meineKontakte = gcnew List<Kontakt^>;
			//
		}

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  textBox7;
	private: System::Windows::Forms::TextBox^  textBox6;
	private: System::Windows::Forms::TextBox^  textBox5;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Button^  button3;

	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBox7 = (gcnew System::Windows::Forms::TextBox());
			this->textBox6 = (gcnew System::Windows::Forms::TextBox());
			this->textBox5 = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(76, 313);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(116, 23);
			this->button1->TabIndex = 28;
			this->button1->Text = L"Kontakt speichern";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// label6
			// 
			this->label6->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(116, 276);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(39, 13);
			this->label6->TabIndex = 27;
			this->label6->Text = L"E-Mail:";
			// 
			// label5
			// 
			this->label5->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(83, 235);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(72, 13);
			this->label5->TabIndex = 26;
			this->label5->Text = L"Mobilnummer:";
			// 
			// label4
			// 
			this->label4->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(109, 194);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(46, 13);
			this->label4->TabIndex = 25;
			this->label4->Text = L"Telefon:";
			// 
			// label3
			// 
			this->label3->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(104, 153);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(51, 13);
			this->label3->TabIndex = 24;
			this->label3->Text = L"Wohnort:";
			// 
			// label2
			// 
			this->label2->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(114, 112);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(41, 13);
			this->label2->TabIndex = 23;
			this->label2->Text = L"Straße:";
			// 
			// label1
			// 
			this->label1->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(117, 71);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(38, 13);
			this->label1->TabIndex = 22;
			this->label1->Text = L"Name:";
			// 
			// textBox7
			// 
			this->textBox7->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox7->Location = System::Drawing::Point(156, 272);
			this->textBox7->Name = L"textBox7";
			this->textBox7->Size = System::Drawing::Size(135, 20);
			this->textBox7->TabIndex = 21;
			// 
			// textBox6
			// 
			this->textBox6->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox6->Location = System::Drawing::Point(156, 231);
			this->textBox6->Name = L"textBox6";
			this->textBox6->Size = System::Drawing::Size(115, 20);
			this->textBox6->TabIndex = 20;
			// 
			// textBox5
			// 
			this->textBox5->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox5->Location = System::Drawing::Point(156, 190);
			this->textBox5->Name = L"textBox5";
			this->textBox5->Size = System::Drawing::Size(115, 20);
			this->textBox5->TabIndex = 19;
			// 
			// textBox4
			// 
			this->textBox4->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox4->Location = System::Drawing::Point(201, 149);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(105, 20);
			this->textBox4->TabIndex = 18;
			// 
			// textBox3
			// 
			this->textBox3->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox3->Location = System::Drawing::Point(156, 149);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(39, 20);
			this->textBox3->TabIndex = 17;
			// 
			// textBox2
			// 
			this->textBox2->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox2->Location = System::Drawing::Point(156, 108);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(115, 20);
			this->textBox2->TabIndex = 16;
			// 
			// textBox1
			// 
			this->textBox1->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->textBox1->Location = System::Drawing::Point(156, 67);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(115, 20);
			this->textBox1->TabIndex = 15;
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(198, 313);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(116, 23);
			this->button2->TabIndex = 29;
			this->button2->Text = L"Kontakte anzeigen";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// button4
			// 
			this->button4->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->button4->Enabled = false;
			this->button4->Location = System::Drawing::Point(197, 21);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(25, 25);
			this->button4->TabIndex = 31;
			this->button4->Text = L"->";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
			// 
			// button3
			// 
			this->button3->Anchor = System::Windows::Forms::AnchorStyles::None;
			this->button3->Enabled = false;
			this->button3->Location = System::Drawing::Point(169, 21);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(25, 25);
			this->button3->TabIndex = 30;
			this->button3->Text = L"<-";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(390, 367);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->textBox7);
			this->Controls->Add(this->textBox6);
			this->Controls->Add(this->textBox5);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Name = L"Form1";
			this->Text = L"Adressbuch";
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
					meineKontakte->Add(gcnew Kontakt(textBox1->Text, textBox2->Text, textBox3->Text, 
						textBox4->Text, textBox5->Text, textBox6->Text, textBox7->Text));

					// Datensatz in die Access-Datenbank einfügen
					command->CommandText = "INSERT INTO Kontakte (Name, Straße, PLZ, Ort, Telefon, Mobilnummer, [E-Mail]) VALUES ('"
						+ textBox1->Text + "', '" + textBox2->Text + "', '" + textBox3->Text + "', '" + textBox4->Text
						+ "', '" + textBox5->Text + "', '" + textBox6->Text + "', '" + textBox7->Text + "')";
					command->ExecuteNonQuery();

					textBox1->Text = "";
					textBox2->Text = "";
					textBox3->Text = "";
					textBox4->Text = "";
					textBox5->Text = "";
					textBox6->Text = "";
					textBox7->Text = "";
				}
	private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) {
					if (connection != nullptr)
						connection->Close();
				}
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
					dsindex = 0;
					button3->Enabled = true;
					button4->Enabled = true;
					button1->Enabled = false;

					textBox1->Text = meineKontakte[0]->Name;
					textBox2->Text = meineKontakte[0]->STR;
					textBox3->Text = meineKontakte[0]->PLZ;
					textBox4->Text = meineKontakte[0]->Ort;
					textBox5->Text = meineKontakte[0]->Tel;
					textBox6->Text = meineKontakte[0]->Telmobil;
					textBox7->Text = meineKontakte[0]->Email;   
				}
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
					String^ connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application::StartupPath + "\\Kontakte.accdb";
					OleDbConnection^ connection = gcnew OleDbConnection(connectionString);
					try
					{
						//Datenbankverbindung herstellen
						connection->Open();

						//Daten aus der Datenbank holen und in die Liste kopieren
						command = connection->CreateCommand();
						command->CommandText = "SELECT * FROM Kontakte";
						lesen = command->ExecuteReader();

						while (lesen->Read())
						{
							meineKontakte->Add(gcnew Kontakt(lesen->GetString(1), lesen->GetString(2), lesen->GetString(3),
								lesen->GetString(4), lesen->GetString(5), lesen->GetString(6), 
								lesen->GetString(7)));                  
						}
						lesen->Close();
					}
					catch (OleDbException^ ex)
					{
						MessageBox::Show(ex->Message);
					}
					catch (Exception^ ex)
					{
						MessageBox::Show(ex->Message);
					}
				}
	private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) //vorblättern
				{
					dsindex++;
					if (dsindex > Kontakt::Anzahl - 1)
						dsindex = 0;

					textBox1->Text = meineKontakte[dsindex]->Name;
					textBox2->Text = meineKontakte[dsindex]->STR;
					textBox3->Text = meineKontakte[dsindex]->PLZ;
					textBox4->Text = meineKontakte[dsindex]->Ort;
					textBox5->Text = meineKontakte[dsindex]->Tel;
					textBox6->Text = meineKontakte[dsindex]->Telmobil;
					textBox7->Text = meineKontakte[dsindex]->Email;
				}
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) //zurückblättern
				{
					dsindex--;
					if (dsindex < 0 )
						dsindex = Kontakt::Anzahl - 1;

					textBox1->Text = meineKontakte[dsindex]->Name;
					textBox2->Text = meineKontakte[dsindex]->STR;
					textBox3->Text = meineKontakte[dsindex]->PLZ;
					textBox4->Text = meineKontakte[dsindex]->Ort;
					textBox5->Text = meineKontakte[dsindex]->Tel;
					textBox6->Text = meineKontakte[dsindex]->Telmobil;
					textBox7->Text = meineKontakte[dsindex]->Email;
				}
	};
}

