Multiple forms & headers

Hello,

I have two programming ideas that I would like to merge. Here's a simplified description of them both (both are windows forms)...

Program 1:
Form1's code has a label but no text. The text that is inserted into this label comes from a header file called Text.h. It's code looks something like this:
1
2
3
4
5
6
7
8
#pragma once
#ifndef _TEXT_FUNCTIONS
#define _TEXT_FUNCTIONS

void LabelText(System::Windows::Forms::Label^ label){
    label->Text = L"Text goes here!";
}
#endif 


Form1 has #include "Text.h" and LabelText().

Program 2:
Form1 has a button and when clicked, loads a new form (Form2) and hides Form1. Form1 has #include "Form2.h"

Note: Both programs work by themselves without errors.

My question...
I can't seem to merge these ideas and I'm wondering if it's a forward declare issue / include (I'm more than likely wrong as my knowledge is VERY limited).

This is what I want in the one program.
Form1 has a button that loads Form2. Form2 has a label but the text from that label comes from Text.h as described above.

My intial thought was that Form1 needs #include "Form2.h". Form2 would then need #include "Text.h"... But no luck...

Can anyone in simple terms explain what I need to do here or at least, give me some key words to Google so that I can start heading in the right direction?!?!

Thanks in advance.
Hi,

I've done some more reading / trial and error programming but I'm getting no where.

In the above post Text.h uses 'void' to declare the function that is used in Form2.h. I'm wondering if this is where my problem is. I.e. should I be using 'extern' or something else perhaps because the function is used / seen in other files?...

Also, would I need to include Text.h in my main source file as well as Form2.h? I have actually tried these ideas but I'm obviously getting errors. Arrgghhh, I feel as thought I'm going backwards.

Help / suggestions would be greatly appreciated!
It looks like I solved the above problem myself and as usual, it was a simple issue.

By chance, I commented out the #include "Form2.h" in Form2.cpp, leaving just #include "stdAfx.h". After this, the program described above worked fine. I'm thinking that Form1.h already had #include "Form2.h" and this was the problem. Note: the main *.cpp file wasn't changed.

So for anyone interested, the following is the code for Form1's button to open Form2 that has a label in it. A button in Form2 generates text in the label with the text coming from the header Text.h:

Form1.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include "Form2.h"

// WinForm automated code goes here...

#pragma endregion

// the following hides Form1 and displays Form2 
// via the button btnForm2

private: System::Void btnForm2_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->Hide();
				 Form2^ form2 = gcnew Form2();
				 form2->ShowDialog();
			 } 


Form2.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
 
#include "Text.h"

// WinForm automated code goes here...

#pragma endregion

// The following calls TextFunction from Text.h and places text into
// lblText in Form2 via the button btnChangeText

private: System::Void btnChangeText_Click(System::Object^  sender, System::EventArgs^  e) {
				 TextFunction(lblText);
			 }


Form2.cpp:
1
2
3
 
#include "StdAfx.h"
//#include "Form2.h" 


Text.h:
1
2
3
4
5
6
7
8
9
10
11
12
 
#pragma once
#ifndef _TESTING_TEXT_FUNCTION
#define _TESTING_TEXT_FUNCTION

// the function 'TextFunction' for changing the text in lblText of Form2.

void TextFunction(System::Windows::Forms::Label^ someLabel)
{
    someLabel->Text = "Text has been changed using Text.h!";
}
#endif 


I hope this helps anyone who has the problem.

Thanks
Topic archived. No new replies allowed.