Passing data between forms

Hi,

The program that I've been working on requires one form (e.g. Form2) sending data to a second form (e.g. Form1).

I have followed the following post:
http://www.cplusplus.com/forum/general/115349/

However this is only for one control, in this case a single rich text box.

I have tried do this for more than one control (this is what my program needs).

Note that there are seperate buttons for their corresponding rich text box.

I've tried but I get errors. Hence my post. Until now, I haven't had to work with constructor code and my programming skills are limited. I am pretty certain this is where my errors lie and I'm very keen to learn what I need to do to fix them.

Like I said, this is an add on from the above post. My code for the "add the constructor code" section of Form2.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// first rich text box
Form2(void)
{
	InitializeComponent();
        richTextBox1 = gcnew RichTextBox;//init richTextBox1 here
}
Form2(System::Windows::Forms::RichTextBox^ rtb) 
    : richTextBox1(rtb)//and here
{
	InitializeComponent();
}

// second rich text box

{
	InitializeComponent();
        richTextBox2 = gcnew RichTextBox;//init richTextBox2 here
}
Form2(System::Windows::Forms::RichTextBox^ rtb2) 
    : richTextBox1(rtb2)//and here
{
	InitializeComponent();
}
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::RichTextBox^ richTextBox2;


This is obviously wrong as I'm unsure how to structure code in this section of the form. Any advice would be great!

Thanks,
closed account (z05DSL3A)
sambos wrote:
The program that I've been working on requires one form (e.g. Form2) sending data to a second form (e.g. Form1).
Can you explain what you are trying to do in a little more detail? How are the two forms related? Are you trying to implement a dialog box?
Hi Grey Wolf...

The program is something I've been working on for a couple of months and I've started with next to no programming knowledge. With that said, I have managed to learn a thing or two. So my knowledge of the correct terminology etc of programming will no doubt reflect this.

Essentially I have a main form (Form1). From this form, four other forms (Form2 - 5) can be loaded. In each of these forms the user answers a series of questions (using yes or no radiobuttons) via following a flowchart of sorts.

I would like the results of these questions displayed on Form1 when the user returns to it.

Anyway, to simplify this I thought I would ask how more than one piece of information (in this case text) can be delivered back to the first form.

For example:
Form1 has two richTextBoxes (richTextBox1 and richTextBox2) and a button that displays Form2. Form2 has two buttons (button1 and button2). Each of these generate text in richTextBox1 and richTextBox2 respectively.

I hope this makes sense... I think my error lies in how I've written the above code (see first post).

Thanks for your time.


closed account (z05DSL3A)
I would think that you should read up on Properties in C++/CLI.

If you have two form classes, Form1 and Form2. In Form1 you have a textbox and a button, and in Form2 you have another textbox and an OK and Cancel.

In Form2 you can have a property along the lines of:
1
2
3
4
5
6
7
8
9
10
11
public: property System::String^ SomeText
			{
				System::String^ get()
				{
					return textBox1->Text;
				}
				void set(System::String^ text)
				{
					textBox1->Text = text;
				}
			}


and in Form1 you have something like:

1
2
3
4
5
6
7
8
9
10
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				 Form2^ frm = gcnew Form2;
				 frm->SomeText = textBox1->Text;
				
				 frm->ShowDialog(this);

				 textBox1->Text = frm->SomeText;

			 }


Hope this helps, I have not got time to go into it in more detail
Thanks Grey Wolf!

You suggested code worked well. It took me a little to adapt it to my program but now it's working just as I wanted it to.
Topic archived. No new replies allowed.