#pragma once ///////////////////////////////////////////////////////////////////// // Form1.h - Declares a form displaying files and directories // // in a list box. Provides a browse control written // // in C# that allows users to select a path to search, // // using a treeView control. // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2004 // ///////////////////////////////////////////////////////////////////// // Notes: // // This project demonstrates how to communicate between two // // forms. The same techniques work for any two classes that // // are defined in seperate modules. // ///////////////////////////////////////////////////////////////////// #using #include "BrowseForm.h" namespace demoWinForm { 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::Text; /// /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::ListBox ^ listBox1; private: System::Windows::Forms::TextBox ^ textBox1; private: System::Windows::Forms::Button ^ button1; private: System::Windows::Forms::StatusBar ^ statusBar1; private: System::Windows::Forms::Button ^ button2; private: bool _showFiles, _showDirs; private: System::Windows::Forms::Button ^ button3; private: System::ComponentModel::Container ^ components; void InitializeComponent(void) { this->listBox1 = gcnew System::Windows::Forms::ListBox(); this->textBox1 = gcnew System::Windows::Forms::TextBox(); this->button1 = gcnew System::Windows::Forms::Button(); this->statusBar1 = gcnew System::Windows::Forms::StatusBar(); this->button2 = gcnew System::Windows::Forms::Button(); this->button3 = gcnew System::Windows::Forms::Button(); this->SuspendLayout(); // // listBox1 // this->listBox1->Anchor = (System::Windows::Forms::AnchorStyles)(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) | System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right); this->listBox1->Font = gcnew System::Drawing::Font("Tahoma", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0); this->listBox1->ItemHeight = 16; this->listBox1->Location = System::Drawing::Point(16, 80); this->listBox1->Name = "listBox1"; this->listBox1->Size = System::Drawing::Size(464, 372); this->listBox1->TabIndex = 0; this->listBox1->DoubleClick += gcnew System::EventHandler(this, &demoWinForm::Form1::listBox1_DoubleClick); // // textBox1 // this->textBox1->AcceptsReturn = true; this->textBox1->Anchor = (System::Windows::Forms::AnchorStyles)((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right); this->textBox1->Font = gcnew System::Drawing::Font("Tahoma", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0); this->textBox1->Location = System::Drawing::Point(16, 16); this->textBox1->Name = "textBox1"; this->textBox1->Size = System::Drawing::Size(464, 23); this->textBox1->TabIndex = 1; this->textBox1->Text = ""; this->textBox1->TextChanged += gcnew System::EventHandler(this, &demoWinForm::Form1::textBox1_TextChanged); // // button1 // this->button1->Anchor = System::Windows::Forms::AnchorStyles::Top; this->button1->Font = gcnew System::Drawing::Font("Tahoma", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0); this->button1->Location = System::Drawing::Point(16, 48); this->button1->Name = "button1"; this->button1->Size = System::Drawing::Size(128, 23); this->button1->TabIndex = 2; this->button1->Text = "ShowFiles"; this->button1->Click += gcnew System::EventHandler(this, &demoWinForm::Form1::button1_Click); // // statusBar1 // this->statusBar1->Location = System::Drawing::Point(0, 476); this->statusBar1->Name = "statusBar1"; this->statusBar1->Size = System::Drawing::Size(496, 22); this->statusBar1->TabIndex = 3; this->statusBar1->Text = "statusBar1"; this->statusBar1->PanelClick += gcnew System::Windows::Forms::StatusBarPanelClickEventHandler(this, &demoWinForm::Form1::statusBar1_PanelClick); // // button2 // this->button2->Anchor = System::Windows::Forms::AnchorStyles::Top; this->button2->Font = gcnew System::Drawing::Font("Tahoma", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0); this->button2->Location = System::Drawing::Point(352, 48); this->button2->Name = "button2"; this->button2->Size = System::Drawing::Size(128, 23); this->button2->TabIndex = 2; this->button2->Text = "ShowDirs"; this->button2->Click += gcnew System::EventHandler(this, &demoWinForm::Form1::button2_Click); // // button3 // this->button3->Anchor = System::Windows::Forms::AnchorStyles::Top; this->button3->Font = gcnew System::Drawing::Font("Tahoma", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0); this->button3->Location = System::Drawing::Point(184, 48); this->button3->Name = "button3"; this->button3->Size = System::Drawing::Size(128, 23); this->button3->TabIndex = 2; this->button3->Text = "Browse"; this->button3->Click += gcnew System::EventHandler(this, &demoWinForm::Form1::button3_Click); // // Form1 // this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(496, 498); this->Controls->Add(this->statusBar1); this->Controls->Add(this->button1); this->Controls->Add(this->textBox1); this->Controls->Add(this->listBox1); this->Controls->Add(this->button2); this->Controls->Add(this->button3); this->Name = "Form1"; this->Text = "Form1"; this->Load += gcnew System::EventHandler(this, &demoWinForm::Form1::Form1_Load); this->ResumeLayout(false); } // //----< called after form completes loading >---------------------- private: System::Void Form1_Load( System::Object ^ sender, System::EventArgs ^ e) { this->Text = "C++ Form Demo"; listBox1->Items->Clear(); statusBar1->Text = "Please Enter Desired Path"; textBox1->Text = "c:\\SU"; button1->Text = "Don't Show Files"; button2->Text = "Don't Show Dirs"; _showFiles = true; _showDirs = true; textBox1_TextChanged(this,e); } //----< handler for textBox text change >-------------------------- private: System::Void textBox1_TextChanged( System::Object ^ sender, System::EventArgs ^ e) { StringBuilder^ path = gcnew StringBuilder(textBox1->Text); try { // strip spaces from path int len = path->Length; for(int i=len-1; i>=0; --i) if((*path)[i] == ' ') path->Remove(i,1); // Attempt to set current directory to this path. // FCL ensures that user's path will be reset at end of program. Directory::SetCurrentDirectory(path->ToString()); listBox1->Items->Clear(); if(_showDirs) { array^ dirs = Directory::GetDirectories(path->ToString(),"*.*"); for(int i=0; iLength; ++i) { listBox1->Items->Add(dirs[i]); } } if(_showFiles) { array^ files = Directory::GetFiles(path->ToString(),"*.*"); for(int i=0; iLength; ++i) { String^ file = Path::GetFileName(files[i]); listBox1->Items->Add(file); } } statusBar1->Text = ""; } catch(Exception^ ) { statusBar1->Text = "invalid path"; } } // //----< handler for Show Files button >---------------------------- private: System::Void button1_Click( System::Object ^ sender, System::EventArgs ^ e) { if(!_showFiles) { _showFiles = true; button1->Text = "Don't Show Files"; } else { _showFiles = false; button1->Text = "Show Files"; } textBox1_TextChanged(this,e); } //----< handler for Show Dirs button >----------------------------- private: System::Void button2_Click( System::Object ^ sender, System::EventArgs ^ e) { if(!_showDirs) { _showDirs = true; button2->Text = "Don't Show Dirs"; } else { _showDirs = false; button2->Text = "Show Dirs"; } textBox1_TextChanged(this,e); } //----< handler for Browse button >-------------------------------- private: System::Void button3_Click( System::Object ^ sender, System::EventArgs ^ e) { BrowseForm^ bf = gcnew BrowseForm(); bf->setBaseForm(this); // allows BrowseForm to call back // due to a syntactical problem in the BrowseForm, I haven't resolved // yet, this delegate is not being used. The design strategy is // correct, but needs a fix I haven't figured out yet bf->setPathEvent += gcnew BrowseForm::setPathDelegate(this,&demoWinForm::Form1::setPath); bf->Show(); bf->Update(); } // //----< handler for statusbar clicked >---------------------------- private: System::Void statusBar1_PanelClick( System::Object ^ sender, System::Windows::Forms::StatusBarPanelClickEventArgs ^ e) { // not used } //---< provides way for another class to pass path to this form >-- public: void setPath(String^ path) { textBox1->Text = path; System::EventArgs^ e ; textBox1_TextChanged(this,e); } //----< handler for listBox1 selection >--------------------------- private: System::Void listBox1_DoubleClick( System::Object ^ sender, System::EventArgs ^ e) { StringBuilder^ msg = gcnew StringBuilder("Do something with "); int index = listBox1->SelectedIndex; String^ selected = static_cast(listBox1->Items[index]); msg->Append(selected); MessageBox::Show(msg->ToString()); } }; }