Windows Forms -> wo wird nicht event-gebundener quelltext implementiert
-
Hallo ich habe einen Socket-Chat geschrieben und eine GUI oberfläche mit Windows Forms gebaut.
nun habe ich da ein kleines Problem und zwar muss ich die Methode, die zum lesen des Datenstroms notwendig ist, ständig laufen lassen, bis etwas in ein Textfeld eingegebn wird. nur wo implementiere ich diesen Quelltext?
Hier mal mein Code:
#pragma once #include <ctime> namespace Chat { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Zusammenfassung für Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Konstruktorcode hier hinzufügen. // text=""; IP=""; Port=""; Cnct=false; } protected: /// <summary> /// Verwendete Ressourcen bereinigen. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::TextBox^ txt_insert; private: System::Windows::Forms::TextBox^ txt_msg; private: System::Windows::Forms::Button^ btn_snd; protected: protected: protected: protected: private: bool Cnct; Socket *chatclient; //ServerSocket *chatsrv(55); String ^text; String ^IP; String ^Port; private: System::Windows::Forms::Label^ lbl_Port; private: System::Windows::Forms::TextBox^ txt_Port; private: System::Windows::Forms::Button^ btn_srvr; private: System::Windows::Forms::Label^ lbl_IP; private: System::Windows::Forms::Label^ lblIP; /// <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) { System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid)); this->txt_insert = (gcnew System::Windows::Forms::TextBox()); this->txt_msg = (gcnew System::Windows::Forms::TextBox()); this->btn_snd = (gcnew System::Windows::Forms::Button()); this->lbl_Port = (gcnew System::Windows::Forms::Label()); this->txt_Port = (gcnew System::Windows::Forms::TextBox()); this->btn_srvr = (gcnew System::Windows::Forms::Button()); this->lbl_IP = (gcnew System::Windows::Forms::Label()); this->lblIP = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // txt_insert // this->txt_insert->BackColor = System::Drawing::SystemColors::InactiveCaption; this->txt_insert->Location = System::Drawing::Point(15, 365); this->txt_insert->Multiline = true; this->txt_insert->Name = L"txt_insert"; this->txt_insert->ReadOnly = true; this->txt_insert->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; this->txt_insert->Size = System::Drawing::Size(238, 59); this->txt_insert->TabIndex = 0; this->txt_insert->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged); // // txt_msg // this->txt_msg->Location = System::Drawing::Point(15, 71); this->txt_msg->MaxLength = 0; this->txt_msg->Multiline = true; this->txt_msg->Name = L"txt_msg"; this->txt_msg->ReadOnly = true; this->txt_msg->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; this->txt_msg->Size = System::Drawing::Size(326, 256); this->txt_msg->TabIndex = 2; this->txt_msg->TextChanged += gcnew System::EventHandler(this, &Form1::txt_msg_TextChanged); // // btn_snd // this->btn_snd->Location = System::Drawing::Point(264, 365); this->btn_snd->Name = L"btn_snd"; this->btn_snd->Size = System::Drawing::Size(77, 59); this->btn_snd->TabIndex = 3; this->btn_snd->Text = L"Send"; this->btn_snd->UseVisualStyleBackColor = true; this->btn_snd->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1); // // lbl_Port // this->lbl_Port->AutoSize = true; this->lbl_Port->Location = System::Drawing::Point(135, 17); this->lbl_Port->Name = L"lbl_Port"; this->lbl_Port->Size = System::Drawing::Size(29, 13); this->lbl_Port->TabIndex = 6; this->lbl_Port->Text = L"Port:"; // // txt_Port // this->txt_Port->Location = System::Drawing::Point(166, 14); this->txt_Port->MaxLength = 5; this->txt_Port->Name = L"txt_Port"; this->txt_Port->Size = System::Drawing::Size(63, 20); this->txt_Port->TabIndex = 7; // // btn_srvr // this->btn_srvr->BackColor = System::Drawing::SystemColors::Control; this->btn_srvr->Location = System::Drawing::Point(263, 12); this->btn_srvr->Name = L"btn_srvr"; this->btn_srvr->Size = System::Drawing::Size(75, 23); this->btn_srvr->TabIndex = 8; this->btn_srvr->Text = L"Start Server"; this->btn_srvr->UseVisualStyleBackColor = false; this->btn_srvr->Click += gcnew System::EventHandler(this, &Form1::btn_cnct_Click); // // lbl_IP // this->lbl_IP->AutoSize = true; this->lbl_IP->Location = System::Drawing::Point(12, 17); this->lbl_IP->Name = L"lbl_IP"; this->lbl_IP->Size = System::Drawing::Size(20, 13); this->lbl_IP->TabIndex = 4; this->lbl_IP->Text = L"IP:"; this->lbl_IP->Click += gcnew System::EventHandler(this, &Form1::label1_Click); // // lblIP // this->lblIP->AutoSize = true; this->lblIP->Location = System::Drawing::Point(38, 17); this->lblIP->Name = L"lblIP"; this->lblIP->Size = System::Drawing::Size(48, 13); this->lblIP->TabIndex = 10; this->lblIP->Text = L"Look Up"; this->lblIP->Click += gcnew System::EventHandler(this, &Form1::label1_Click_1); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::SystemColors::Control; this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"$this.BackgroundImage"))); this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center; this->ClientSize = System::Drawing::Size(359, 443); this->Controls->Add(this->lblIP); this->Controls->Add(this->btn_srvr); this->Controls->Add(this->txt_Port); this->Controls->Add(this->lbl_Port); this->Controls->Add(this->lbl_IP); this->Controls->Add(this->btn_snd); this->Controls->Add(this->txt_msg); this->Controls->Add(this->txt_insert); this->DoubleBuffered = true; this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D; this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon"))); this->Name = L"Form1"; this->Text = L"Socket Chat V1.0 Server"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) { if(chatclient->dataAvailable()) { String ^sHelper = gcnew String(chatclient->readLine().c_str()); txt_msg->Text=txt_msg->Text+"Partner: "+sHelper+"\r\n"; } } private: System::Void labesdaaaaaaaal1_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void listBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { } private: System::Void txt_msg_TextChanged(System::Object^ sender, System::EventArgs^ e) { txt_msg->SelectionStart = txt_msg->Text->Length; txt_msg->ScrollToCaret(); } private: System::Void pictureBox1_Click_1(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { if(txt_insert->Text!="") { string sHelper; MarshalString ( txt_insert->Text, sHelper ); txt_msg->Text= txt_msg->Text+"Ich: "+txt_insert->Text+"\r\n"; chatclient->write(sHelper+'\n'); txt_insert->Text=""; } } private: void MarshalString ( String ^ s, string& os ) { //Konvertiert String ^ --> std::string //Direkt aus der MSDN kopiert //http://msdn.microsoft.com/de-de/library/1b4az623(VS.80).aspx using namespace Runtime::InteropServices; const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars)); } private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void btn_cnct_Click(System::Object^ sender, System::EventArgs^ e) { if(txt_Port->Text!="") { string sHelper1; Port= txt_Port->Text; MarshalString ( Port, sHelper1 ); int iHelper=atoi (sHelper1 .c_str ()); txt_msg->Text="Waiting for connection, listening on Port "+Port+"....\r\n"; ServerSocket chatsrv(iHelper); chatclient = chatsrv.accept(); if(chatclient!=NULL) { txt_msg->Text+="Connection established!\r\n\r\n"; txt_insert->ReadOnly=false; txt_insert->BackColor=SystemColors::Control; btn_srvr->Text="Connected..."; btn_srvr->Enabled=false; } } else { MessageBox::Show("Please enter Port Number"); } } private: System::Void txt_IP_TextChanged(System::Object^ sender, System::EventArgs^ e) { } private: System::Void btn_get_Click(System::Object^ sender, System::EventArgs^ e) { if(chatclient->dataAvailable()) { String ^sHelper = gcnew String(chatclient->readLine().c_str()); txt_msg->Text=txt_msg->Text+"Partner: "+sHelper+"\r\n"; } } private: System::Void label1_Click_1(System::Object^ sender, System::EventArgs^ e) { system("ipconfig&Pause"); } }; }
-
Moin, moin
als erstes würde ich Dir empfehlen den Code zu trennen, dass heißt, lager Deinen Code in die CPP aus. Den Code vom Designer belässt Du in der Header.
Form1.h
#pragma once namespace Test { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Zusammenfassung für Form1 /// /// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch /// die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern, /// das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt. /// Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen /// arbeiten, die diesem Formular zugewiesen sind. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Konstruktorcode hier hinzufügen. // } protected: /// <summary> /// Verwendete Ressourcen bereinigen. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: 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->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(72, 56); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 0; this->button1->Text = L"button1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(371, 266); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e); }; }
Form1.cpp
// Test.cpp: Hauptprojektdatei. #include "stdafx.h" #include "Form1.h" using namespace Test; [STAThreadAttribute] int main(array<System::String ^> ^args) { // Aktivieren visueller Effekte von Windows XP, bevor Steuerelemente erstellt werden Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Hauptfenster erstellen und ausführen Application::Run(gcnew Form1()); return 0; } System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show("Test"); }
Hier noch mal einen Link dazu: http://www.rb-softarticle.com/Code%20trennen.aspx
Und den Code Deiner Methode packst Du auch in die CPP, musst sie aber zuvor in der Header vereinbaren.
Gruß
Doug_HH