I can't include instance of global form variable in user control. Makes errors.

In Program.h is ProgramItems ref class which has static Window ^window (form). I want to include it in Search.h (user control), so from it I can add controls to window and other stuff. But, when I include "Program.h" in Search.h i get different errors on compile. Basicly all I want to do is to have global window variable that works for any usercontrol in my program.
Here is my code:

Program.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Window.h"
#include "Program.h"

using namespace System;
using namespace System::Windows::Forms;

[STAThread]
int main() {

	Application::EnableVisualStyles();
	
	Application::SetCompatibleTextRenderingDefault(false);

	ProgramItems::window = gcnew SMSALENalozi::Window();

	Application::Run(ProgramItems::window);

	return 0;
}

Program.h:
1
2
3
4
5
6
7
8
9
10
11
#pragma once

#include "Window.h"

ref class ProgramItems {

public:

	static SMSALENalozi::Window ^window;

};

Window.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include "Search.h" //When include it makes errors.

namespace SMSALENalozi {

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

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Window()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::PictureBox^  menu_background_picturebox;
	private: System::Windows::Forms::PictureBox^  logo_picturebox;
	private: System::Windows::Forms::Label^  ulogujse_label;
	private: System::Windows::Forms::TextBox^  passwordwriting_textbox;
        ...OTHER FORM CODE.
	private: System::Void passwordwriting_textbox_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {

		if (e->KeyChar == (char)13) {

			if (passwordwriting_textbox->Text == "KRASTA") {

				Search ^search = gcnew Search();

				this->Controls->Clear();

				this->Controls->Add(search);

				search->Focus();

				e->Handled = true;

			}

		}

	}
};
}

Search.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once

#include "Program.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

namespace SMSALENalozi {

	/// <summary>
	/// Summary for Search
	/// </summary>
	public ref class Search : public System::Windows::Forms::UserControl
	{
	public:
		Search(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			trazi_label->Parent = search_background_picturebox;
			searchby_combobox->SelectedIndex = 0;
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Search()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::PictureBox^  search_background_picturebox;
	protected:

	private: System::Windows::Forms::Label^  trazi_label;
	private: System::Windows::Forms::TextBox^  accountwriting_textbox;
        ...OTHER CODE
	private: System::Void accountwriting_textbox_PreviewKeyDown(System::Object^  sender, System::Windows::Forms::PreviewKeyDownEventArgs^  e) {

		if (e->KeyCode == Keys::Enter) {

			this->Hide();

		}

	}
};
}


Thanks :)!
Last edited on
What are the error messages ?
This is error: http://imgur.com/a/m1LbH
Try this:
Program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Window.h"
#include "Program.h"

using namespace System;
using namespace System::Windows::Forms;

[STAThread]
int main() 
{

	Application::EnableVisualStyles();	
	Application::SetCompatibleTextRenderingDefault(false);	
	SMSALENalozi::ProgramItems::window = gcnew SMSALENalozi::Window();
	Application::Run(SMSALENalozi::ProgramItems::window);

	return 0;
}


Program.h
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

#include "Window.h"

namespace SMSALENalozi
{
  ref class ProgramItems 
  {
  public:
    static Window^ window;
  };
}


What version of Visual Studio do you use ?
Not working :(. Microsoft Visual Studio Express 2015. When I include Window in other classes there is no problem, but if I include it in Window.h then this error happens. Well I don't actually include Window.h in window.h, I include search.h which included program.h which includes window.h.
Last edited on
I created a VS 2015 CE solution with the code you showed and got it working. However some code was missing so it shows only an empty window. Maybe you can add the missing code.

https://www.dropbox.com/sh/ugf5msp6kjgyd5n/AACozc_3iT8GO9fnaRuC5WmKa?dl=0
I tried your code and it worked perfectly. But then I saw there was no "include Program.h", so I wrote it and got the same error as in my original project. Try it.
Last edited on
Can someone help me with this :/?
Looks like a classic case of circular includes to me.

Search.h includes Program.h which includes Window.h which includes Search.h which includes Program.h which includes Window.h...

you get the point.

http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies
Yes, but #pragma was supposed to prevent that?
Can someone help with this, I really need to get it done.
Yes, but #pragma was supposed to prevent that?

Include guards and #pragma once solve the problem of #including a file multiple times. Neither one resolves circular dependencies. Did you visit the SO link?
It works, thank you :)! Very much!
Last edited on
Topic archived. No new replies allowed.