CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    8

    [RESOLVED] Need help with a Form to Form Transfer Example

    I have been trying to get this figured out for the last week without much luck. I can't seem to grasp which concept to use here.
    I have a form called Transfer and another form called Receiver (which is created at startup and hidden). There is a button on the Transfer form that makes the Receiver form visible, and the close button hides the form again. I'm trying to make it where as something it typed in a textbox on the Transfer form, it's mirrored in the textbox on the Reveiver form (and vice versa). Also, as a checkbox is ckecked on the Transfer form, it is checked on the Receiver form (and vice versa). I've posted the code for the 2 forms below. Could someone please help me out with what I need to do as described?
    Thanks much!

    Here is the Transfer code:

    Code:
    #pragma once
    #include "Receiver.h"
    namespace Transfer {
    
    	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
    	{
    	private: Receiver ^showreceiverForm;
    
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    
    			showreceiverForm = gcnew Receiver;
    
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::TextBox^  TransferTextBox1;
    	private: System::Windows::Forms::Button^  TransferButton1;
    	private: System::Windows::Forms::CheckBox^  TransferCheckBox1;
    	private: System::Windows::Forms::CheckBox^  TransferCheckBox2;
    	protected: 
    
    	protected: 
    
    
    	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->TransferTextBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->TransferButton1 = (gcnew System::Windows::Forms::Button());
    			this->TransferCheckBox1 = (gcnew System::Windows::Forms::CheckBox());
    			this->TransferCheckBox2 = (gcnew System::Windows::Forms::CheckBox());
    			this->SuspendLayout();
    			// 
    			// TransferTextBox1
    			// 
    			this->TransferTextBox1->Location = System::Drawing::Point(69, 19);
    			this->TransferTextBox1->Name = L"TransferTextBox1";
    			this->TransferTextBox1->Size = System::Drawing::Size(155, 20);
    			this->TransferTextBox1->TabIndex = 0;
    			// 
    			// TransferButton1
    			// 
    			this->TransferButton1->Location = System::Drawing::Point(96, 95);
    			this->TransferButton1->Name = L"TransferButton1";
    			this->TransferButton1->Size = System::Drawing::Size(101, 23);
    			this->TransferButton1->TabIndex = 1;
    			this->TransferButton1->Text = L"Show Receiver";
    			this->TransferButton1->UseVisualStyleBackColor = true;
    			this->TransferButton1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// TransferCheckBox1
    			// 
    			this->TransferCheckBox1->AutoSize = true;
    			this->TransferCheckBox1->Location = System::Drawing::Point(70, 55);
    			this->TransferCheckBox1->Name = L"TransferCheckBox1";
    			this->TransferCheckBox1->Size = System::Drawing::Size(55, 17);
    			this->TransferCheckBox1->TabIndex = 2;
    			this->TransferCheckBox1->Text = L"Item 1";
    			this->TransferCheckBox1->UseVisualStyleBackColor = true;
    			// 
    			// TransferCheckBox2
    			// 
    			this->TransferCheckBox2->AutoSize = true;
    			this->TransferCheckBox2->Location = System::Drawing::Point(175, 55);
    			this->TransferCheckBox2->Name = L"TransferCheckBox2";
    			this->TransferCheckBox2->Size = System::Drawing::Size(55, 17);
    			this->TransferCheckBox2->TabIndex = 3;
    			this->TransferCheckBox2->Text = L"Item 2";
    			this->TransferCheckBox2->UseVisualStyleBackColor = true;
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 146);
    			this->Controls->Add(this->TransferCheckBox2);
    			this->Controls->Add(this->TransferCheckBox1);
    			this->Controls->Add(this->TransferButton1);
    			this->Controls->Add(this->TransferTextBox1);
    			this->Name = L"Form1";
    			this->Text = L"Transfer";
    			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) 
    			 {
    				showreceiverForm->Visible = true;
    			 }
    
    	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
    			 {
    
    			 }
    	};
    }
    And here is the Receiver code:

    Code:
    #pragma once
    
    namespace Transfer {
    
    	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 Receiver
    	/// </summary>
    	public ref class Receiver : public System::Windows::Forms::Form
    	{
    	public:
    		Receiver(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Receiver()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::TextBox^  ReceiverTextBox1;
    	private: System::Windows::Forms::CheckBox^  ReceiverCheckBox2;
    	private: System::Windows::Forms::CheckBox^  ReceiverCheckBox1;
    	protected: 
    
    	protected: 
    
    	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->ReceiverTextBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->ReceiverCheckBox2 = (gcnew System::Windows::Forms::CheckBox());
    			this->ReceiverCheckBox1 = (gcnew System::Windows::Forms::CheckBox());
    			this->SuspendLayout();
    			// 
    			// ReceiverTextBox1
    			// 
    			this->ReceiverTextBox1->Location = System::Drawing::Point(58, 25);
    			this->ReceiverTextBox1->Name = L"ReceiverTextBox1";
    			this->ReceiverTextBox1->Size = System::Drawing::Size(176, 20);
    			this->ReceiverTextBox1->TabIndex = 0;
    			// 
    			// ReceiverCheckBox2
    			// 
    			this->ReceiverCheckBox2->AutoSize = true;
    			this->ReceiverCheckBox2->Location = System::Drawing::Point(171, 60);
    			this->ReceiverCheckBox2->Name = L"ReceiverCheckBox2";
    			this->ReceiverCheckBox2->Size = System::Drawing::Size(55, 17);
    			this->ReceiverCheckBox2->TabIndex = 5;
    			this->ReceiverCheckBox2->Text = L"Item 2";
    			this->ReceiverCheckBox2->UseVisualStyleBackColor = true;
    			// 
    			// ReceiverCheckBox1
    			// 
    			this->ReceiverCheckBox1->AutoSize = true;
    			this->ReceiverCheckBox1->Location = System::Drawing::Point(66, 60);
    			this->ReceiverCheckBox1->Name = L"ReceiverCheckBox1";
    			this->ReceiverCheckBox1->Size = System::Drawing::Size(55, 17);
    			this->ReceiverCheckBox1->TabIndex = 4;
    			this->ReceiverCheckBox1->Text = L"Item 1";
    			this->ReceiverCheckBox1->UseVisualStyleBackColor = true;
    			// 
    			// Receiver
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 101);
    			this->Controls->Add(this->ReceiverCheckBox2);
    			this->Controls->Add(this->ReceiverCheckBox1);
    			this->Controls->Add(this->ReceiverTextBox1);
    			this->Name = L"Receiver";
    			this->Text = L"Receiver";
    			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Receiver::Receiver_FormClosing);
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    	private: System::Void Receiver_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
    			{
    			// When the user attempts to close the form, don't close it
    			e->Cancel = true;
    			// Only hide it
    			this->Visible = false;
    			}
    	};
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Need help with a Form to Form Transfer Example

    I really can't imagine why you would want to do that, but ok...

    For the direction from the sender to the receiver, you can expose the control members in question in the receiver form's class by changing them from private to public, and then have the TextChanged and CheckedChanged event handlers in the sender form set the corresponding Text and Checked properties of the controls in the receiver form.

    Now, one could think it could be done quite similarly in the opposite direction (after setting up a Form ^ member in the receiver form to point back to the sender form). But I'm pretty sure attempting that at the same time would send your program into an endless loop (or more precisely, an endless recursion). Unfortunately, I don't have any idea how to get around that problem without too much effort right now. Maybe someone else has an idea how to handle that.

    HTH
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Oct 2010
    Posts
    8

    Re: Need help with a Form to Form Transfer Example

    I tried what you suggested. I started with just the textbox and made the controls public. Then I added a TextChanged event on the Transfer form to make the Receiver form textbox the same, but I'm getting a compile error.

    Transfer.h(134): error C2227: left of '->Text' must point to class/struct/union/generic type

    I'm pretty noobish at this and am probable missing something. Would you be able to please edit what code I have and explain how to make the textbox available across forms?
    Thanks much for any help.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Need help with a Form to Form Transfer Example

    Quote Originally Posted by zasanil View Post
    Transfer.h(134): error C2227: left of '->Text' must point to class/struct/union/generic type
    And what is left of ->Text and how is it declared?

    Would you be able to please edit what code I have and explain how to make the textbox available across forms?
    Maybe, if it isn't too much that has to be changed. But of course I would need to see the code in order to do that, so please post it here. (Please keep in mind to use code tags.) It's generally easier to spot what's wrong with the offending statement if we can see it within some context.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Oct 2010
    Posts
    8

    Re: Need help with a Form to Form Transfer Example

    Very little changed from the origional post other than what was described (public & textchanged events)

    Here is the code as it is now:

    Code:
    #pragma once
    #include "Receiver.h"
    
    namespace Transfer
    {
    
    	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
    	{
    	private: Receiver ^showreceiverForm;
    
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    
    			showreceiverForm = gcnew Receiver;
    
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::TextBox^  TransferTextBox1;
    	private: System::Windows::Forms::Button^  TransferButton1;
    	private: System::Windows::Forms::CheckBox^  TransferCheckBox1;
    	private: System::Windows::Forms::CheckBox^  TransferCheckBox2;
    
    	protected: 
    
    	protected: 
    
    
    	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->TransferTextBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->TransferButton1 = (gcnew System::Windows::Forms::Button());
    			this->TransferCheckBox1 = (gcnew System::Windows::Forms::CheckBox());
    			this->TransferCheckBox2 = (gcnew System::Windows::Forms::CheckBox());
    			this->SuspendLayout();
    			// 
    			// TransferTextBox1
    			// 
    			this->TransferTextBox1->Location = System::Drawing::Point(69, 19);
    			this->TransferTextBox1->Name = L"TransferTextBox1";
    			this->TransferTextBox1->Size = System::Drawing::Size(155, 20);
    			this->TransferTextBox1->TabIndex = 0;
    			this->TransferTextBox1->TextChanged += gcnew System::EventHandler(this, &Form1::TransferTextChanged);
    			// 
    			// TransferButton1
    			// 
    			this->TransferButton1->Location = System::Drawing::Point(96, 95);
    			this->TransferButton1->Name = L"TransferButton1";
    			this->TransferButton1->Size = System::Drawing::Size(101, 23);
    			this->TransferButton1->TabIndex = 1;
    			this->TransferButton1->Text = L"Show Receiver";
    			this->TransferButton1->UseVisualStyleBackColor = true;
    			this->TransferButton1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// TransferCheckBox1
    			// 
    			this->TransferCheckBox1->AutoSize = true;
    			this->TransferCheckBox1->Location = System::Drawing::Point(70, 55);
    			this->TransferCheckBox1->Name = L"TransferCheckBox1";
    			this->TransferCheckBox1->Size = System::Drawing::Size(55, 17);
    			this->TransferCheckBox1->TabIndex = 2;
    			this->TransferCheckBox1->Text = L"Item 1";
    			this->TransferCheckBox1->UseVisualStyleBackColor = true;
    			// 
    			// TransferCheckBox2
    			// 
    			this->TransferCheckBox2->AutoSize = true;
    			this->TransferCheckBox2->Location = System::Drawing::Point(175, 55);
    			this->TransferCheckBox2->Name = L"TransferCheckBox2";
    			this->TransferCheckBox2->Size = System::Drawing::Size(55, 17);
    			this->TransferCheckBox2->TabIndex = 3;
    			this->TransferCheckBox2->Text = L"Item 2";
    			this->TransferCheckBox2->UseVisualStyleBackColor = true;
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 146);
    			this->Controls->Add(this->TransferCheckBox2);
    			this->Controls->Add(this->TransferCheckBox1);
    			this->Controls->Add(this->TransferButton1);
    			this->Controls->Add(this->TransferTextBox1);
    			this->Name = L"Form1";
    			this->Text = L"Transfer";
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    		{
    			showreceiverForm->Visible = true;
    		}
    
    private: System::Void TransferTextChanged(System::Object^  sender, System::EventArgs^  e)
    		{
    			Transfer::Receiver::ReceiverTextBox1->Text = this->TransferTextBox1->Text;
    		}
    	};
    }
    The full (and only) error I'm getting is:

    1>c:\documents and settings\xxxxxxxx\my documents\visual studio 2010\projects\transfer\transfer\Transfer.h(144): error C2227: left of '->Text' must point to class/struct/union/generic type

    I thought declaring the control as public and adding the Transfer::Receiver:: before the control would be enough but as I said, I'm definitely missing something.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Need help with a Form to Form Transfer Example

    Quote Originally Posted by zasanil View Post
    Code:
    private: System::Void TransferTextChanged(System::Object^  sender, System::EventArgs^  e)
    		{
    			Transfer::Receiver::ReceiverTextBox1->Text = this->TransferTextBox1->Text;
    		}
    You are trying to access the text box member of the receiver form as if it were a static member, which it hopefully is not. You did access the form the right way in the button click handler. So try this:

    Code:
    private: System::Void TransferTextChanged(System::Object^  sender, System::EventArgs^  e)
    		{
    			showreceiverForm->ReceiverTextBox1->Text = this->TransferTextBox1->Text;
    		}
    And doesn't that look a bit more logical?
    Last edited by Eri523; November 26th, 2010 at 06:33 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Oct 2010
    Posts
    8

    Re: Need help with a Form to Form Transfer Example

    That did the trick. Thanks a bunch for all your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured