Winform qui communiquent entre elles (avec des threads)

Signaler
Messages postés
1
Date d'inscription
jeudi 10 mai 2012
Statut
Membre
Dernière intervention
10 mai 2012
-
Bonjour,

voila j'ai actuellement 2Forms l'une est déclaré dans le main (FormA), la seconde (Form1) via un thread. Ce que j'aimerais c'est que ce que j'écris dans FormA apparaisse dans les textBox de formA ET de Form1...

le pb est que je ne sais pu quoi utiliser entre les invoke, databinding bidirectionals, pipes... qu'est ce qui est le plus simple? (il faut à tout moment que je puisse interagir avec les 2 fenètres aussi :s il me semble que c'est des threads asynchrone et que jusque la je suis bon...)

voici mon code:
FormA.h
#pragma once
#include "Form1.h"

namespace d {

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>
/// Summary for FormA
/// </summary>
public ref class FormA : public System::Windows::Forms::Form
{
public:
FormA(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~FormA()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::TextBox^  textBoxReadA;
private: System::Windows::Forms::TextBox^  textBoxWrite;
protected: 

protected: 

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->textBoxReadA = (gcnew System::Windows::Forms::TextBox());
this->textBoxWrite = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
// 
// textBoxReadA

// 
this->textBoxReadA->AcceptsReturn = true;
this->textBoxReadA->Location = System::Drawing::Point(21, 13);
this->textBoxReadA->Multiline = true;
this->textBoxReadA->Name = L"textBoxReadA";
this->textBoxReadA->ReadOnly = true;
this->textBoxReadA->ScrollBars = System::Windows::Forms::ScrollBars::Both;
this->textBoxReadA->Size = System::Drawing::Size(357, 336);
this->textBoxReadA->TabIndex = 0;
// 
// textBoxWrite
// 
this->textBoxWrite->Location = System::Drawing::Point(13, 389);
this->textBoxWrite->Name = L"textBoxWrite";
this->textBoxWrite->Size = System::Drawing::Size(264, 20);
this->textBoxWrite->TabIndex = 1;
// 
// button1
// 
this->button1->Location = System::Drawing::Point(298, 387);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(79, 21);
this->button1->TabIndex = 2;
this->button1->Text = L"Write";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &FormA::button1_Click);
// 
// FormA
// 
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(402, 422);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBoxWrite);
this->Controls->Add(this->textBoxReadA);
this->Name = L"FormA";
this->Text = L"FormA";
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {


textBoxReadA->AppendText(textBoxWrite->Text);
textBoxReadA->Text += "\r\n";				
//to see the last down message in the textbox
textBoxReadA->SelectionStart = textBoxReadA->TextLength;
textBoxReadA->ScrollToCaret();
 }

 
};
}


le Form1.h (les boutons ne sont pas actifs pour le moment)
#pragma once

namespace d 
{

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>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();

//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}


public: System::Windows::Forms::TextBox^  textBoxRead;

private: System::Windows::Forms::Button^  button1;
private: System::Windows::Forms::Button^  button2;


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->textBoxRead = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
// 
// textBoxRead
// 
this->textBoxRead->AcceptsReturn = true;
this->textBoxRead->Location = System::Drawing::Point(12, 22);
this->textBoxRead->Multiline = true;
this->textBoxRead->Name = L"textBoxRead";
this->textBoxRead->ScrollBars = System::Windows::Forms::ScrollBars::Both;
this->textBoxRead->Size = System::Drawing::Size(483, 340);
this->textBoxRead->TabIndex = 0;
// 
// button1
// 
this->button1->Location = System::Drawing::Point(57, 383);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(144, 35);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
// 
// button2
// 
this->button2->Location = System::Drawing::Point(271, 383);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(195, 34);
this->button2->TabIndex = 2;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
// 
// Form1
// 
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(503, 435);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBoxRead);
this->Name = L"Form1";
this->Text = L"Form1";

this->ResumeLayout(false);
this->PerformLayout();

}


#pragma endregion
};
}



le main (d.cpp)
// d.cpp : main project file.

#include "stdafx.h"
//#include "Form1.h"
#include "FormA.h"

using namespace System;
using namespace System::Threading;
using namespace d;


// Simple threading scenario:  Start a Shared method running
// on a second thread.
public ref class ThreadWindow
{
public:

   // The ThreadProc method is called when the thread starts.
   // It loops ten times, writing to the console and yielding 
   // the rest of its time slice each time, and then ends.
   static void ThreadProc()
   {
   Application::EnableVisualStyles();
   Application::SetCompatibleTextRenderingDefault(false); 
   
   Application::Run(gcnew Form1());
   Thread::Sleep( 0 );
   }

};




[STAThreadAttribute]
int main(array<System::String ^> ^args)
{

// Enabling Windows XP visual effects before any controls are created
Thread^ oThread = gcnew Thread( gcnew ThreadStart( &ThreadWindow::ThreadProc ) );
oThread->Start();
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it
Application::Run(gcnew FormA());

return 0;
}


d'avance merci