In diesem Visual C + + Programm lernen Sie das befüllen von einer ListBox und das Löschen einzelner Elemente. Des Weiteren lernen Sie, wie sie ein zweites Formular erstellen und dieses aufrufen bzw. Daten zwischen den beiden Formularen austauschen. Auch das Sortieren der Zahlen in einer Listbox bzw. das automatische Generieren von Zufallszahlen lernen Sie kennen.
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit2.h" #include "Unit3.h" #include "Unit4.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2 *Form2; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { Form3->Show(); } //--------------------------------------------------------------------------- void __fastcall TForm2::Button2Click(TObject *Sender) { if(Form2->RadioButton1->Checked) { for(int i=0;i<Form2->ListBox1->Items->Count;i++) { //ShowMessage(Form2->ListBox1->Items->Strings[i]); for(int j=0;j<Form2->ListBox1->Items->Count;j++) { if(Form2->ListBox1->Items->Strings[j].ToInt()>Form2->ListBox1->Items->Strings[i].ToInt()) { String hilf=Form2->ListBox1->Items->Strings[j]; Form2->ListBox1->Items->Strings[j]=Form2->ListBox1->Items->Strings[i]; Form2->ListBox1->Items->Strings[i]=hilf; } } } } if(Form2->RadioButton2->Checked) { for(int i=0;i<Form2->ListBox1->Items->Count;i++) { //ShowMessage(Form2->ListBox1->Items->Strings[i]); for(int j=0;j<Form2->ListBox1->Items->Count;j++) { if(Form2->ListBox1->Items->Strings[j].ToInt()<Form2->ListBox1->Items->Strings[i].ToInt()) { String hilf=Form2->ListBox1->Items->Strings[j]; Form2->ListBox1->Items->Strings[j]=Form2->ListBox1->Items->Strings[i]; Form2->ListBox1->Items->Strings[i]=hilf; } } } } } //--------------------------------------------------------------------------- void __fastcall TForm2::Button3Click(TObject *Sender) { Form2->ListBox1->DeleteSelected(); } //--------------------------------------------------------------------------- void __fastcall TForm2::Button4Click(TObject *Sender) { Form4->Show(); } //--------------------------------------------------------------------------- void __fastcall TForm2::ListBox1KeyPress(TObject *Sender, System::WideChar &Key) { if(int(Key)==8) Form2->ListBox1->DeleteSelected(); } //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit2.h" #include "Unit3.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm3 *Form3; //--------------------------------------------------------------------------- __fastcall TForm3::TForm3(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm3::Button1Click(TObject *Sender) { Form2->ListBox1->Items->Add(Form3->Edit1->Text); Form3->Edit1->SetFocus(); } //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- #include <vcl.h> #include <cstdlib> #include <time.h> #pragma hdrstop #include "Unit4.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm4 *Form4; //--------------------------------------------------------------------------- __fastcall TForm4::TForm4(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm4::Button1Click(TObject *Sender) { for(int i=0;i<Form4->Edit1->Text.ToInt();i++) { Form4->Memo1->Lines->Add(rand()%150); } } //--------------------------------------------------------------------------- void __fastcall TForm4::Button2Click(TObject *Sender) { Form4->Close(); for(int i=0;i<Form4->Memo1->Lines->Count;i++) { Form2->ListBox1->Items->Add(Form4->Memo1->Lines->Strings[i]); } } //---------------------------------------------------------------------------