event mit String
-
danke schonmal und wie würde man dann das event feuern in dem BSP?
public ref class Button { public: event EventHandler^ Click; } public ref class Form1 { Button^ Button1; void Button1_Click(Object^ sender, EventArgs^ e) { Console::WriteLIne("Button 1 clicked"); } public: Form1() { Button1 = gcnew Button; // Add an event handler to respond to button clicks Button1->Click += gcnew EventHandler(this, &Button1_Click); } }
Weiss jetzt auch nicht genau ob das hier dann nur für einen button geht - das will ich nämlich auhc nicht. Sondern eine Funktion die ein Event auslöst, welches mir dann in bspw.einer textbox was hinzufügt.
-
Keiner ne Ahnung wie ich das mache das ich in einer Funktion ein Event auslöse das einen String in eine Textbox schreibt?
(ohne das die Funktion die Textbox als Objekt hat - sondern übers Event übertragen sozusagen)
-
Schaue doch bei deinem letzten Post nach:
http://www.c-plusplus.net/forum/viewtopic-var-t-is-205337.htmlDie Schritte sind folgede:
1. Delegate definieren, welcher eben ein String als Argument hat
2. Einen event mit diesem Delegaten definieren.
-
ok, bei meinen letzten Projekt habe ich ja ein delegate:
delegate void DelegateThreadTask(System::String^);
schon wegen dem Invoke.
Wie definier ich dann ein Event darauf und feuer SchreibeInTextBox aus einer anderen Funktion? (also ohne das Object^data an die Funktion zu übergeben)#pragma once #using <mscorlib.dll> //#include <string> #include "FW.h" namespace DemoModul2008 { 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::Threading; /// <summary> /// 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. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { //#include "FW.h" // private: Thread ^trd; // private: Thread^ newThread; delegate void DelegateThreadTask(System::String^); public: void SchreibeInTextBox(System::String^ zeichen) //void SchreibeInTextBox(char*) { if (textBox1->InvokeRequired == false) textBox1->AppendText(zeichen); else { DelegateThreadTask ^myThreadDelegate = gcnew DelegateThreadTask(this,&DemoModul2008::Form1::SchreibeInTextBox); this->Invoke(myThreadDelegate,zeichen);//erst Delegate name dann parameter } } public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } // if (trd->IsAlive) trd->Abort();//Tread stoppen } public: System::Windows::Forms::TextBox^ textBox1; protected: private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Button^ button1; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->button1 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // textBox1 // this->textBox1->Location = System::Drawing::Point(12, 66); this->textBox1->Multiline = true; this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(591, 311); this->textBox1->TabIndex = 0; // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 22, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->label1->Location = System::Drawing::Point(100, 14); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(397, 36); this->label1->TabIndex = 1; this->label1->Text = L"multiWin DemoModul 2008"; // // button1 // this->button1->Location = System::Drawing::Point(637, 104); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(57, 44); this->button1->TabIndex = 2; 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(750, 389); this->Controls->Add(this->button1); this->Controls->Add(this->label1); this->Controls->Add(this->textBox1); this->Name = L"Form1"; this->Text = L"Form1"; 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) { this->textBox1->AppendText("TEST"); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Thread^ newThread = gcnew Thread(gcnew ParameterizedThreadStart(DemoModul2008::Form1::repeat)); newThread->IsBackground = true; newThread->Start(this); } static void repeat(Object^ data) { //Form1^ form = safe_cast<Form1^>(data); //nach innen verlagert mainFW(data); } }; }
-
Ich verstehe nicht was Du beabsichtigst zu tun...
Prinzipiell hast Du zwei Objekte, die miteinander kommunizieren.
1. Auf direkte Art durch Methoden aufrufe
2. Durch Callbacks in Form von Delegaten. Hier muss die "Methode" die aufgerufen werden soll allerdings zuerst registriert werden.Hier hat es ein Bsp. mit einem Event:
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/VS2005pro/Framework/CPlusCLI.htmEinzig die Signatur muss umgestellt werden:
Delegat:public delegate void TextHandler(String^ str);
Producer:
public ref class Producer { public: event TextHandler^ TextEvent; private: void FireEvent(String^ str) { if (TextEvent != nullptr) { TextEvent(str); } } }
Consumer:
public ref class Consumer { Producer^ producer; void OnTextEvent(String^ str) { Console::WriteLIne(str); } public: Consumer() { producer = gcnew Producer; producer->TextEvent += gcnew TextHandler(this, &OnTextEvent); } }
Ich konnte das nicht testen, sorry.
Jedoch sollte die Idee klar werden.Simon
-
danke erstmal
Habe es in mein Projekt eingearbeitet.#pragma once #using <mscorlib.dll> //#include <string> #include "FW.h" namespace DemoModul2008 { 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::Threading; /// <summary> /// 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. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { delegate void DelegateThreadTask(System::String^); public: ref class Producer { public: event DelegateThreadTask^ TextEvent; public: void FireEvent(System::String^ str) { TextEvent(str); //geht nicht mit nullptr zu vergeichen /* if (TextEvent != nullptr) { TextEvent(str); } */ } }; /* public: ref class Consumer { Producer^ producer; void OnTextEvent(System::String^ str) { Console::WriteLine(str); } public: Consumer() { producer = gcnew Producer; producer->TextEvent += gcnew TextHandler(this, &Consumer::OnTextEvent); } }; */ public: void SchreibeInTextBox(System::String^ zeichen) //void SchreibeInTextBox(char*) { if (textBox1->InvokeRequired == false) textBox1->AppendText(zeichen); else { DelegateThreadTask ^myThreadDelegate = gcnew DelegateThreadTask(this,&DemoModul2008::Form1::SchreibeInTextBox); this->Invoke(myThreadDelegate,zeichen);//erst Delegate name dann parameter } } public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // Producer^ producer; producer = gcnew Producer; producer->TextEvent += gcnew DelegateThreadTask(this, &Form1::SchreibeInTextBox); } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } // if (trd->IsAlive) trd->Abort();//Tread stoppen } public: System::Windows::Forms::TextBox^ textBox1; protected: private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Button^ button1; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->button1 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // textBox1 // this->textBox1->Location = System::Drawing::Point(12, 66); this->textBox1->Multiline = true; this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(591, 311); this->textBox1->TabIndex = 0; // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 22, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->label1->Location = System::Drawing::Point(100, 14); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(397, 36); this->label1->TabIndex = 1; this->label1->Text = L"multiWin DemoModul 2008"; // // button1 // this->button1->Location = System::Drawing::Point(637, 104); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(57, 44); this->button1->TabIndex = 2; 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(750, 389); this->Controls->Add(this->button1); this->Controls->Add(this->label1); this->Controls->Add(this->textBox1); this->Name = L"Form1"; this->Text = L"Form1"; 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) { this->textBox1->AppendText("Button Clicked"); SetzeVorinitialisierung(); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Thread^ newThread = gcnew Thread(gcnew ParameterizedThreadStart(DemoModul2008::Form1::repeat)); newThread->IsBackground = true; newThread->Start(this); } static void repeat(Object^ data) { //Form1^ form = safe_cast<Form1^>(data); //nach innen verlagert // mainFW(data); mainFW();//darin soll ohne data Objekt SchreibeInTextBox ausgelöst werden } }; }
wenn ich jetzt aber in MainFW()
DemoModul2008::Form1::Producer::FireEvent("TEST");
schreibe bekomm ich die Fehlermeldung Illegal Call of non Static Member Function.
Kann ich nicht mittels Event irgendwie auf die SchreibeInTextBox zugreifen ohne das ich das Objekt mit übergeben muss?Auf jedenfall schonmal danke für die aufklärung mit dem Event
-
Ich empfehle Dir dringend zuerst die Sprache zu vertiefen.
Es ist immer dasselbe. Es sieht so aus, als wüsstest Du nicht was Objekte, Instanzen, Klassen etc. sind. Dies sind jedoch die Grundwerkzeuge und zwingend nötig.Ich würde ein Buch kaufen falls Du noch keines hast.
C++/CLI ist auch nicht di einfachste Sprache.. es gibt viele Fehlerquellen und von der lesbarkeit her gibts auch einfacheres.
Ev. gibts Sprachen die besser geeignet sind um zu lernen was wie funktioniert.Wenn Du vorerst bei der .NET Programmierung bleiben möchtest gibts ja diverse alternativen und sehr gute Bücher.
Gruss Simon
-
simon.gysi schrieb:
Ich empfehle Dir dringend zuerst die Sprache zu vertiefen.
Es ist immer dasselbe. Es sieht so aus, als wüsstest Du nicht was Objekte, Instanzen, Klassen etc. sind. Dies sind jedoch die Grundwerkzeuge und zwingend nötig.Ich würde ein Buch kaufen falls Du noch keines hast.
C++/CLI ist auch nicht di einfachste Sprache.. es gibt viele Fehlerquellen und von der lesbarkeit her gibts auch einfacheres.
Ev. gibts Sprachen die besser geeignet sind um zu lernen was wie funktioniert.Wenn Du vorerst bei der .NET Programmierung bleiben möchtest gibts ja diverse alternativen und sehr gute Bücher.
Gruss Simon
ja ok war vl.ein bisschen dümlich gefragt...
habe jetzt versucht in der SchreibeTextBox über Application::OpenForms an die TextBox zu kommen. Er kompiliert es auch aber zur Laufzeit erhalte ich dann Folgende Exception:"Das Objekt des Typs System.Windows.Forms.FormCollection kann nicht in Typ DemoModul2008.Form1 umgewandelt werden."
Müsste das nicht irgendwie über OpenForms gehen?Hier mein Versuch:
public: static void SchreibeInTextBox(System::String^ zeichen) //void SchreibeInTextBox(char*) { System::Object^ data; //Application::OpenForms(data); data = Application::OpenForms; DemoModul2008::Form1^ form = safe_cast<DemoModul2008::Form1^>(data);
Könntest du mir niocht doch vl.nochmal Helfen
-
Du musst über die System.Windows.Forms.FormCollection (bei dir data) iterieren und dir die richtige Form heraussuchen.
Gruss Simon
-
musst gar nicht iterieren. Einfach nur den richtigen namen reingeben und es ging. Danke dir hast mich auf die Richtige Idee Gebracht