#pragma once

namespace Listenfelder {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Text;
	using namespace System::Drawing;

	/// <summary>
	/// Zusammenfassung für Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();

         // Drag&Drop-Quelle mit Daten füllen
			dragSource->Items->AddRange(gcnew array< System::Object^>(7) {"Spinnen", "Medusa", 
				                                                          "Teufel", "Schnecken", 
				                                                          "Golem", "Skylla", "Charybdis"});
	
         // Drag&Drop-Ziel aktivieren  
         dropTarget->AllowDrop = true;

		}

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  dropTarget;
	private: System::Windows::Forms::ListBox^  dragSource;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;

	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->dropTarget = (gcnew System::Windows::Forms::ListBox());
			this->dragSource = (gcnew System::Windows::Forms::ListBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// dropTarget
			// 
			this->dropTarget->FormattingEnabled = true;
			this->dropTarget->Location = System::Drawing::Point(321, 65);
			this->dropTarget->Name = L"dropTarget";
			this->dropTarget->Size = System::Drawing::Size(167, 173);
			this->dropTarget->TabIndex = 7;
			this->dropTarget->DragDrop += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::dropTarget_DragDrop);
			this->dropTarget->DragEnter += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::dropTarget_DragEnter);
			// 
			// dragSource
			// 
			this->dragSource->FormattingEnabled = true;
			this->dragSource->Location = System::Drawing::Point(104, 65);
			this->dragSource->Name = L"dragSource";
			this->dragSource->Size = System::Drawing::Size(167, 173);
			this->dragSource->TabIndex = 6;
			this->dragSource->QueryContinueDrag += gcnew System::Windows::Forms::QueryContinueDragEventHandler(this, &Form1::dragSource_QueryContinueDrag);
			this->dragSource->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::dragSource_MouseDown);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label2->Location = System::Drawing::Point(321, 25);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(105, 24);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Ihre Wahl:";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->Location = System::Drawing::Point(104, 25);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(87, 24);
			this->label1->TabIndex = 4;
			this->label1->Text = L"Fundus:";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(593, 262);
			this->Controls->Add(this->dropTarget);
			this->Controls->Add(this->dragSource);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Name = L"Form1";
			this->Text = L"Drag&Drop für Listenfelder";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: 
      // Drag-Daten aufnehmen
      void dragSource_MouseDown(Object^  sender, MouseEventArgs^  e) 
      {
         String^ item;
         int index = dragSource->SelectedIndex;
               
         if (index >= 0)
         {
            item = (String^) dragSource->Items[index]; 
               
            DragDropEffects result = dragSource->DoDragDrop(item, 
                                                DragDropEffects::Move);
                   
            if (result == DragDropEffects::Move)
            {
               dragSource->Items->RemoveAt(index);
            }    
            else if (result == DragDropEffects::None)
            {
               MessageBox::Show("Verschieben Sie die ausgewählten Objekte in das "
                                "zweite Listenfeld", "Falsches Drop-Ziel");
            }   
         } 
      }

      // kein Drag aus der Anwendung heraus
      void dragSource_QueryContinueDrag(Object^  sender, QueryContinueDragEventArgs^  e) 
      {
         Form^ f = dragSource->FindForm();
         Point offset = SystemInformation::WorkingArea.Location;

         if(   ((Control::MousePosition.X - offset.X) < f->DesktopBounds.Left) 
            || ((Control::MousePosition.X - offset.X) > f->DesktopBounds.Right) 
            || ((Control::MousePosition.Y - offset.Y) < f->DesktopBounds.Top) 
            || ((Control::MousePosition.Y - offset.Y) > f->DesktopBounds.Bottom)) 
         {
            e->Action = DragAction::Cancel;
         }
      }


      // Drag-Daten prüfen und Drop-Operation anzeigen
      void dropTarget_DragEnter(Object^  sender, DragEventArgs^  e) 
      {
         if (e->Data->GetDataPresent(DataFormats::Text))
         {
            e->Effect = DragDropEffects::Move;
         }
      }

      // Drag-Daten übernehmen
      void dropTarget_DragDrop(Object^  sender, DragEventArgs^  e) 
      {
         if (e->Data->GetDataPresent(DataFormats::Text))
         {
            dropTarget->Items->Add((String^) e->Data->GetData(DataFormats::Text));
         }
      }
};
}

