[RESOLVED] Refer to control in another source file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: [RESOLVED] Refer to control in another source file

  1. #1
    Join Date
    Apr 2017
    Posts
    23

    [RESOLVED] Refer to control in another source file

    Hello.
    I had stuck on problem. I'm creating my program and I've got a problem with refer to the controls in another source files. It's my code:
    Code:
    // GlowneOkno.h
    #include "Funkcje.h"
    
    #pragma once
    
    namespace TWMasterBot {
    
    	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>
    	/// Podsumowanie informacji o GlowneOkno
    	/// </summary>
    	public ref class GlowneOkno : public System::Windows::Forms::Form
    	{
    	public: System::Windows::Forms::TextBox^  GUI_Input_Login;
            private: System::Windows::Forms::Button^  GUI_Button_DodajKonto;
    	public:
    		GlowneOkno(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: W tym miejscu dodaj kod konstruktora
    			//
                            DirsExists();
    			InisExists();
    		}
    
    	protected:
    		/// <summary>
    		/// Wyczyść wszystkie używane zasoby.
    		/// </summary>
    		~GlowneOkno()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Wymagana metoda obsługi projektanta  nie należy modyfikować 
    		/// zawartość tej metody z edytorem kodu.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->components = ( gcnew System::ComponentModel::Container() );
    			System::ComponentModel::ComponentResourceManager^  resources = ( gcnew System::ComponentModel::ComponentResourceManager( GlowneOkno::typeid ) );
                            this->GUI_Input_Login = ( gcnew System::Windows::Forms::TextBox() );
                            this->GUI_Button_DodajKonto = ( gcnew System::Windows::Forms::Button() );
                            // 
    			// GUI_Input_Login
    			// 
    			this->GUI_Input_Login->Location = System::Drawing::Point( 323, 73 );
    			this->GUI_Input_Login->Name = L"GUI_Input_Login";
    			this->GUI_Input_Login->Size = System::Drawing::Size( 126, 20 );
    			this->GUI_Input_Login->TabIndex = 7;
                            // 
    			// GUI_Button_DodajKonto
    			// 
    			this->GUI_Button_DodajKonto->Location = System::Drawing::Point( 236, 29 );
    			this->GUI_Button_DodajKonto->Name = L"GUI_Button_DodajKonto";
    			this->GUI_Button_DodajKonto->Size = System::Drawing::Size( 75, 25 );
    			this->GUI_Button_DodajKonto->TabIndex = 3;
    			this->GUI_Button_DodajKonto->Text = L"< Dodaj";
    			this->GUI_Button_DodajKonto->UseVisualStyleBackColor = true;
    			this->GUI_Button_DodajKonto->Click += gcnew System::EventHandler( this, &GlowneOkno::GUI_Button_DodajKonto_Click );
                            // 
    			// GlowneOkno
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF( 6, 13 );
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->BackColor = System::Drawing::Color::White;
    			this->ClientSize = System::Drawing::Size( 1283, 839 );
    			this->Controls->Add( this->GUI_Button_Zwin );
    			this->Controls->Add( this->GUI_Button_Nawigacja );
    			this->Controls->Add( this->GUI_Button_Synchronizuj );
    			this->Controls->Add( this->GUI_Button_Aktualizuj );
    			this->Controls->Add( this->GUI_Gropu_Timery );
    			this->Controls->Add( this->GUI_Group_MainPanel );
    			this->Controls->Add( this->MainTab );
    			this->Icon = ( cli::safe_cast<System::Drawing::Icon^>( resources->GetObject( L"$this.Icon" ) ) );
    			this->MinimumSize = System::Drawing::Size( 1299, 878 );
    			this->Name = L"GlowneOkno";
    			this->RightToLeft = System::Windows::Forms::RightToLeft::No;
    			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
    			this->Text = L"TW Master Bot v1.0 by Genotypek";
    			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler( this, &GlowneOkno::GlowneOkno_FormClosing );
    			this->MainTab->ResumeLayout( false );
    			this->MainTabWWW->ResumeLayout( false );
    			this->MainTabBudowanie->ResumeLayout( false );
    			this->MainTabRuchWojsk->ResumeLayout( false );
    			this->GUI_Group_RuchWojsk->ResumeLayout( false );
    			this->GUI_Group_RuchWojsk->PerformLayout();
    			this->GUI_Group_Ruchy_Wyswietlanie->ResumeLayout( false );
    			this->GUI_Group_Ruchy_Wyswietlanie->PerformLayout();
    			this->GUI_Group_Ruchy_Interwal->ResumeLayout( false );
    			this->GUI_Group_Ruchy_Jednostki->ResumeLayout( false );
    			this->GUI_Group_Ruchy_Jednostki->PerformLayout();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Szlachcice ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Rycerz ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Katapulty ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Tarany ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_CK ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_LNK ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_LK ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Zwiadowcy ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Luki ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Topory ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Miecze ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Piki ) )->EndInit();
    			this->GUI_Group_Ruchy_DataDojsciaAtaku->ResumeLayout( false );
    			this->GUI_Group_Ruchy_DataDojsciaAtaku->PerformLayout();
    			this->GUI_Group_Ruchy_WspolrzedneCelu->ResumeLayout( false );
    			this->GUI_Group_Ruchy_WspolrzedneCelu->PerformLayout();
    			this->GUI_Group_Ruchy_LicznikiRuchow->ResumeLayout( false );
    			this->GUI_Group_Ruchy_LicznikiRuchow->PerformLayout();
    			this->Ruchy_Group_Ruch_ZWioski->ResumeLayout( false );
    			this->Ruchy_Group_Ruch_ZWioski->PerformLayout();
    			this->MainTabKoordynatorAtakow->ResumeLayout( false );
    			this->MainTabRekrutacja->ResumeLayout( false );
    			this->MainTabPrzegladArmii->ResumeLayout( false );
    			this->MainTabBalansSurowcow->ResumeLayout( false );
    			this->MainTabMapa->ResumeLayout( false );
    			this->MainTabRaporty->ResumeLayout( false );
    			this->MainTabLog->ResumeLayout( false );
    			this->MainTabLog->PerformLayout();
    			this->GUI_Group_MainPanel->ResumeLayout( false );
    			this->GUI_Group_MainPanel->PerformLayout();
    			this->GUI_Gropu_Timery->ResumeLayout( false );
    			this->GUI_Gropu_Timery->PerformLayout();
    			this->GUI_Group_TimeryPrzychodzacych1->ResumeLayout( false );
    			this->GUI_Group_TimeryPrzychodzacych1->PerformLayout();
    			this->GUI_Group_TimeryPrzychodzacych2->ResumeLayout( false );
    			this->GUI_Group_TimeryPrzychodzacych2->PerformLayout();
    			this->GUI_Group_TimeryAkcji->ResumeLayout( false );
    			this->GUI_Group_TimeryAkcji->PerformLayout();
    			this->ResumeLayout( false );
    
    		}
    #pragma endregion
    
    private: System::Void GUI_Button_DodajKonto_Click( System::Object^  sender, System::EventArgs^  e )
    {
    	DodajKonto();
    }
    };
    }
    Code:
    // Funkcje.h
    // Many another includes
    #include "ZarzadzanieIni.h"
    Code:
    // ZarzadzanieIni.h
    void DirsExists();
    void InisExists();
    void DodajKonto();
    Code:
    // ZarzadzanieIni.cpp
    #include <string>
    #include <fstream>
    #include <Windows.h>
    #include "Funkcje.h"
    #include "GlowneOkno.h"
    
    using namespace TWMasterBot;
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    std::string IniDirs[] = {"UserData"};
    std::string IniFiles[] = { "UserData/SavedAccounts.ini" };
    
    void DirsExists( )
    {
    	for ( int i = 0; i < sizeof( IniDirs ) / sizeof( IniDirs[0] ); i++ )
    		if ( !dirExists( IniDirs[i] ) )
    			CreateDirectoryA( IniDirs[i].c_str(), NULL );
    }
    
    void InisExists()
    {
    	for ( int i = 0; i < sizeof( IniDirs ) / sizeof( IniDirs[0] ); i++ )
    	{
    		struct stat buffer;
    		if ( !(stat( IniFiles[i].c_str(), &buffer ) == 0) )
    		{
    			std::ofstream { IniFiles[i].c_str() };
    		}
    	}
    }
    
    void DodajKonto()
    {
            // 1st try:
    	GlowneOkno::GUI_Input_Login->Text = "Test"; // Generates error : a nonstatic member reference must be relative to a specific object
    
            // 2nd try:
            GlowneOkno^ GUI_Input_Login;
    	GUI_Input_Login->Text = "Test"; // It can compile, but doesn't change the text
    
            // 3rd try:
            GlowneOkno GUI_Input_Login;
    	GUI_Input_Login.Text = "Test"; // It can compile too, but doesn't work as 2nd try
    
            // 4th try:
            // When changing the GUI_Input_Login in GlowneOkno.h to public: static, simply:
            GlowneOkno::GUI_Input_Login->Text = "Test"; // Works, but I know, that i shouldn't change manually the code in this section to static, so after all designer will remove my static
            
    }
    Clicking my GUI_Button_DodajKonto starts the function DodajKonto(); in ZarzadzanieIni.cpp
    I'm trying to do it, but I have already no idea. I was looking in google for 3 days and nothing helped.
    Only one of my 4 tries works, but it's not correct too.
    Could you help me how to access my controls in whole program?
    Greetings, genotypek.

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

    Re: Refer to control in another source file

    You don't explain what the problem you're stuck on actually is, and the structural relationship between your source files isn't quite obvious, so mainly I can just give you a qualified guess. But there's a really common problem called mutual inclusion which is specific to scenarios like yours. It has been discussed around here quite some times. The most recent discussion of it should be this one: http://forums.codeguru.com/showthrea...is-not-working Many others can be found using forum search.

    The master cure to this kind of problem is strict separation of method declarations (prototypes) in .h files and implementations in .cpp files. The only method implementation that always have to remain intact in the .h file is InitializeComponent(), since it is maintained by the Forms Designer, which expects to find it exclusively at this place. (A Form class' constructor implementation may also be left in the .h file in many cases, if it's rather simple.)
    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
    Apr 2017
    Posts
    23

    Re: Refer to control in another source file

    Okay, i have read it and changed some code.
    My actual code is:

    GlowneOkno.h [Form]
    Code:
    #pragma once
    
    #include "ZarzadzanieIni.h"
    
    namespace TWMasterBot {
    
    	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>
    	/// Podsumowanie informacji o GlowneOkno
    	/// </summary>
    	public ref class GlowneOkno : public System::Windows::Forms::Form
    	{
    	public: System::Windows::Forms::TextBox^  GUI_Input_Login;
            private: System::Windows::Forms::Button^  GUI_Button_DodajKonto;
    	public:
    		GlowneOkno(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: W tym miejscu dodaj kod konstruktora
    			//
                            DirsCreate();
    			InisCreate();
    		}
    
    	protected:
    		/// <summary>
    		/// Wyczyść wszystkie używane zasoby.
    		/// </summary>
    		~GlowneOkno()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Wymagana metoda obsługi projektanta  nie należy modyfikować 
    		/// zawartość tej metody z edytorem kodu.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->components = ( gcnew System::ComponentModel::Container() );
    			System::ComponentModel::ComponentResourceManager^  resources = ( gcnew System::ComponentModel::ComponentResourceManager( GlowneOkno::typeid ) );
                            this->GUI_Input_Login = ( gcnew System::Windows::Forms::TextBox() );
                            this->GUI_Button_DodajKonto = ( gcnew System::Windows::Forms::Button() );
                            // 
    			// GUI_Input_Login
    			// 
    			this->GUI_Input_Login->Location = System::Drawing::Point( 323, 73 );
    			this->GUI_Input_Login->Name = L"GUI_Input_Login";
    			this->GUI_Input_Login->Size = System::Drawing::Size( 126, 20 );
    			this->GUI_Input_Login->TabIndex = 7;
                            // 
    			// GUI_Button_DodajKonto
    			// 
    			this->GUI_Button_DodajKonto->Location = System::Drawing::Point( 236, 29 );
    			this->GUI_Button_DodajKonto->Name = L"GUI_Button_DodajKonto";
    			this->GUI_Button_DodajKonto->Size = System::Drawing::Size( 75, 25 );
    			this->GUI_Button_DodajKonto->TabIndex = 3;
    			this->GUI_Button_DodajKonto->Text = L"< Dodaj";
    			this->GUI_Button_DodajKonto->UseVisualStyleBackColor = true;
    			this->GUI_Button_DodajKonto->Click += gcnew System::EventHandler( this, &GlowneOkno::GUI_Button_DodajKonto_Click );
                            // 
    			// GlowneOkno
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF( 6, 13 );
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->BackColor = System::Drawing::Color::White;
    			this->ClientSize = System::Drawing::Size( 1283, 839 );
    			this->Controls->Add( this->GUI_Button_Zwin );
    			this->Controls->Add( this->GUI_Button_Nawigacja );
    			this->Controls->Add( this->GUI_Button_Synchronizuj );
    			this->Controls->Add( this->GUI_Button_Aktualizuj );
    			this->Controls->Add( this->GUI_Gropu_Timery );
    			this->Controls->Add( this->GUI_Group_MainPanel );
    			this->Controls->Add( this->MainTab );
    			this->Icon = ( cli::safe_cast<System::Drawing::Icon^>( resources->GetObject( L"$this.Icon" ) ) );
    			this->MinimumSize = System::Drawing::Size( 1299, 878 );
    			this->Name = L"GlowneOkno";
    			this->RightToLeft = System::Windows::Forms::RightToLeft::No;
    			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
    			this->Text = L"TW Master Bot v1.0 by Genotypek";
    			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler( this, &GlowneOkno::GlowneOkno_FormClosing );
    			this->MainTab->ResumeLayout( false );
    			this->MainTabWWW->ResumeLayout( false );
    			this->MainTabBudowanie->ResumeLayout( false );
    			this->MainTabRuchWojsk->ResumeLayout( false );
    			this->GUI_Group_RuchWojsk->ResumeLayout( false );
    			this->GUI_Group_RuchWojsk->PerformLayout();
    			this->GUI_Group_Ruchy_Wyswietlanie->ResumeLayout( false );
    			this->GUI_Group_Ruchy_Wyswietlanie->PerformLayout();
    			this->GUI_Group_Ruchy_Interwal->ResumeLayout( false );
    			this->GUI_Group_Ruchy_Jednostki->ResumeLayout( false );
    			this->GUI_Group_Ruchy_Jednostki->PerformLayout();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Szlachcice ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Rycerz ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Katapulty ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Tarany ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_CK ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_LNK ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_LK ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Zwiadowcy ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Luki ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Topory ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Miecze ) )->EndInit();
    			( cli::safe_cast<System::ComponentModel::ISupportInitialize^>( this->GUI_Picture_Ruch_Piki ) )->EndInit();
    			this->GUI_Group_Ruchy_DataDojsciaAtaku->ResumeLayout( false );
    			this->GUI_Group_Ruchy_DataDojsciaAtaku->PerformLayout();
    			this->GUI_Group_Ruchy_WspolrzedneCelu->ResumeLayout( false );
    			this->GUI_Group_Ruchy_WspolrzedneCelu->PerformLayout();
    			this->GUI_Group_Ruchy_LicznikiRuchow->ResumeLayout( false );
    			this->GUI_Group_Ruchy_LicznikiRuchow->PerformLayout();
    			this->Ruchy_Group_Ruch_ZWioski->ResumeLayout( false );
    			this->Ruchy_Group_Ruch_ZWioski->PerformLayout();
    			this->MainTabKoordynatorAtakow->ResumeLayout( false );
    			this->MainTabRekrutacja->ResumeLayout( false );
    			this->MainTabPrzegladArmii->ResumeLayout( false );
    			this->MainTabBalansSurowcow->ResumeLayout( false );
    			this->MainTabMapa->ResumeLayout( false );
    			this->MainTabRaporty->ResumeLayout( false );
    			this->MainTabLog->ResumeLayout( false );
    			this->MainTabLog->PerformLayout();
    			this->GUI_Group_MainPanel->ResumeLayout( false );
    			this->GUI_Group_MainPanel->PerformLayout();
    			this->GUI_Gropu_Timery->ResumeLayout( false );
    			this->GUI_Gropu_Timery->PerformLayout();
    			this->GUI_Group_TimeryPrzychodzacych1->ResumeLayout( false );
    			this->GUI_Group_TimeryPrzychodzacych1->PerformLayout();
    			this->GUI_Group_TimeryPrzychodzacych2->ResumeLayout( false );
    			this->GUI_Group_TimeryPrzychodzacych2->PerformLayout();
    			this->GUI_Group_TimeryAkcji->ResumeLayout( false );
    			this->GUI_Group_TimeryAkcji->PerformLayout();
    			this->ResumeLayout( false );
    
    		}
    #pragma endregion
    
    private: System::Void GUI_Button_DodajKonto_Click( System::Object^  sender, System::EventArgs^  e )
    {
    	DodajKonto();
    }
    };
    }
    Kontrolki.h
    Code:
    #pragma once
    
    #include "GlowneOkno.h"
    
    namespace TWMasterBot
    {
    	public ref class Controls
    	{
    		public:
    			static System::Windows::Forms::TextBox^ GUI_Input_Login;
    	};
    }
    ZarzadzanieIni.h
    Code:
    #pragma once
    
    #include <string>
    
    bool DirExist(const std::string& dirName_in);
    void DirsCreate();
    void Iniscreate();
    void DodajKonto();
    ZarzadzanieIni.cpp
    Code:
    #include <string>
    #include <fstream>
    #include <Windows.h>
    
    std::string IniDirs[] = { "UserData" };
    std::string IniFiles[] = { "UserData/SavedAccounts.ini" };
    
    bool dirExists( const std::string& dirName_in )
    {
    	DWORD ftyp = GetFileAttributesA( dirName_in.c_str() );
    	if ( ftyp == INVALID_FILE_ATTRIBUTES )
    		return false;  //something is wrong with your path!
    
    	if ( ftyp & FILE_ATTRIBUTE_DIRECTORY )
    		return true;   // this is a directory!
    
    	return false;    // this is not a directory!
    }
    
    void DirsCreate()
    {
    	for ( int i = 0; i < sizeof( IniDirs ) / sizeof( IniDirs[0] ); i++ )
    		if ( !dirExists( IniDirs[i] ) )
    			CreateDirectoryA( IniDirs[i].c_str(), NULL );
    }
    
    void InisCreate()
    {
    	for ( int i = 0; i < sizeof( IniDirs ) / sizeof( IniDirs[0] ); i++ )
    	{
    		struct stat buffer;
    		if ( !( stat( IniFiles[i].c_str(), &buffer ) == 0 ) )
    		{
    			std::ofstream { IniFiles[i].c_str() };
    		}
    	}
    }
    
    void DodajKonto()
    {
    	TWMasterBot::Controls::GUI_Input_Login->Text = "Test";
    }
    I created another class in Kontrolki.h
    What I want to do is to change Form controls properties from another cpp file, not only in GlowneOkno.h
    I'm using #pragma once, so it shouldnt include each other. The code is compiling, but after clicking button and doing DodajKonto() function VisualStudio gives me this error:
    Unsupported exception System.NullReferenceException: "Object reference not set to object instance."

    How can I avoid it and change my GUI_Input_Login Text property to "Test", or any another property value?
    Greetings, genotypek.

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

    Re: Refer to control in another source file

    #pragma once does not avoid the mutual inclusion problem. In this regard, it just makes the difference between "something is already defined" errors or infinite inclusion recursion on one hane, and "something is not defined" errors on the other hand. At any rate, mutual inclusion is not the problem here anyway: The mutual inclusion problem always makes a program fail compilation, while yours compiles successfully but then fails execution.

    The null reference exception is no surprise here: Your global variable GUI_Input_Login never gets assigned any value. How should it know on its own which text box to refer to? One place where you can assign the text box (reference handle) to that variable is the GlowneOkno constructor.

    However, this kind of use of global variables is bad Programming practice. Even though assigning the variable in the constructor ensures that the variable has been assigned once construction of your GlowneOkno instance is complete, there's no guaranty that not any piece of your code, that uses the variable, gets called from somewhere under the hood of the .NET framework before the variable got assigned, and you still get that exception. Also, this kind of practice makes programs hard to maintain and extend. What, for instance, if you later decide to create another instance of GlowneOkno?

    One alternative to your global variable is using the framework infrastructure to access your text box. This particular text box can, for instance, be referred to as:

    Code:
    Application::OpenForms["GlowneOkno"]->Controls["GUI_Input_Login"]
    This doesn't solve the general problems of global variable use, but technically it uses a global variable maintainef by the framework instead of your own, so it's at least a small step closer to a clean solution. A clean solution is most probably possible, but finding it needs more knowledge of the internal workings of your code than I have.

    Kontrolki.h does not need to include GlowneOkno.h - you're not referring to your own Form class derivate, just to the gloally known TextBox class.

    To find the location in your program where an uncaught exception gets thrown, just run the program in the debugger. It will break at the point where the exception gets thrown. The exception message box you get when you run the program outside of the he debugger should also give you some detailed diagnostic information that allows to locate the point of failure, but that's more complicated than simply letting the debugger drop you there.
    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
    Apr 2017
    Posts
    23

    Re: Refer to control in another source file

    It still doesn't work.
    I have totally no idea what to do, especially that I don't know completely what instance is and how it works.

    Debugger shows exception in ZarzadzanieIni.cpp, function DodajKonto(), line 34: (this function is calling by clicking button in GlowneOkno.h)
    Name:  Instance.jpg
Views: 22
Size:  5.2 KB
    Sorry, but I'm from poland and I've got polish version of Visual Studio.

    The same error is showing in this same place as before. I don't know why this method doesn't work?
    Maybe I'm still doing something wrong?

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

    Re: Refer to control in another source file

    Quote Originally Posted by Genotypek View Post
    [...] don't know completely what instance is and how it works.
    https://en.wikipedia.org/wiki/Instan...puter_science) There's also a polish article about that topic (link at the left edge of the page) but it's rather short and I can't tell how good it is, because I don't understand it.

    Your polish language screen shot is no problem; the relevant parts are understandable to me. But please, if possible, store screen shots you want to post in PNG format rather than JPEG. The lossy JPEG compression tends to make text hard to read, and sometimes it even becomes just unreadable.

    Getting a null reference exception on that line, assuming the form and control names are correct, indicates, that the GlowneOkno form object (instance) hasn't been constructed (completely) at that point of execution. (The item accessors used here return null instead of throwing an exception it the item requested isn't found in the collection.) If that code is in a button click handler, do I assume correctly that the exception is thrown as soon as you click the button? If so, is the GlowneOkno form visible on screen at that point?

    What does the call stack look like when the exception is thrown?
    Last edited by Eri523; May 11th, 2017 at 01:34 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
    Apr 2017
    Posts
    23

    Re: Refer to control in another source file

    Yes, the window is visible on the screen and is constructed completely, the program works perfectly until I'll click this button and call the function in Zarzadzanie.ini
    My program looks exactly like this, when executed (PNG):
    Name:  Executed program.jpg
Views: 24
Size:  25.8 KB
    This button is the button with focus.
    My code is really much bigger, than I posted here, but it's only another controls, like you can see.

    The call stock looks:
    Name:  Call stock.jpg
Views: 22
Size:  12.4 KB
    Code:
    TW Master Bot.exe!DodajKonto() line 33
    TW Master Bot.exe!TWMasterBot::GlowneOkno::GUI_Button_DodajKonto_Click(System::Object^ sender, System::EventArgs^ e) line 2479
    [external code]
    [The following frames may be incorrect and / or missing, no symbols loaded for user32.dll]
    TW Master Bot.exe!(array<System::String^>^ args) line 15
    [external code]
    (^^^^ If the picture would be unreadable)

    I think, that the problem is not here, because if I'll add static at control declaration in GlowneOkno.h:
    Code:
    public: static System::Windows::Forms::TextBox^  GUI_Input_Login;
    //instead:
    // public: System::Windows::Forms::TextBox^  GUI_Input_Login;
    simply in ZarzadzanieIni.cpp:
    Code:
    #include "GlowneOkno.h"
    
    void DodajKonto()
    {
    	TWMasterBot::GlowneOkno::GUI_Input_Login->Text = "Test";
    }
    Works fine, but I know that I shouldn't change anything in managed space by designer, because it will automatically overwrited.
    What is wrong there?
    Greetings, Genotypek.

    PS. Thank you for the link to instance definition, english isn't problematic for me, so I don't have to read polish thread to get informations.
    PS2. I have saved these screens as PNG, uploaded as PNG now, and it is as .jpg again on the forum... :/
    Last edited by Genotypek; May 11th, 2017 at 03:48 PM.

  8. #8
    Join Date
    Apr 2017
    Posts
    23

    Re: Refer to control in another source file

    @Refresh

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,661

    Re: Refer to control in another source file

    Sorry, I've sort of ran out of ideas regarding your issue. The next logical step for me would be to grab the code you posted, try to reconstruct you project and reproduce your problem, but I didn't have enough spare time to do that yet.

    What I can give you is another shot-in-the-dark guess, but this one is even more vague than the last one: Could it be that your two forms involved may be part of the same application, but have been created by two distinct process instances of that application? This would explain the exception you get. If so, we'd be talking about inter-process communication (IPC), which is a whole different ballgame and rather complex.

    And, re screen shots...

    Quote Originally Posted by Genotypek View Post
    PS2. I have saved these screens as PNG, uploaded as PNG now, and it is as .jpg again on the forum... :/
    Unfortunately, I can reproduce that. This is sad, because screen shot (and other image) details may really be important in developer discussions. Once there were times where that worked...
    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.

  10. #10
    Join Date
    Apr 2017
    Posts
    23

    Re: Refer to control in another source file

    I removed my all another forms from this project and that still doesn't work, an expection is still thrown.
    It's strange, because simply provide controls to this function as aguments works... But I won't be able to do it in the future for every function. :/
    After all, thank you for your time, maybe I'll try to find an answer on another forums like this, maybe more people, who aren't on this forum will come up with a solution.
    Thank you one more time!
    Greeting, Genotypek.

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,661

    Re: Refer to control in another source file

    In the meantime I had another idea: As your screen shot shows, your form is rather complex. So maybe the text box you're looking for is located in a group box. In this case, the text box is no direct child of the form but of the group box (which is, in turn, a child of the form). If this is the case, chances are that you're more successful accessing the text box like this:

    Code:
    Application::OpenForms["GlowneOkno"]->Controls->Find("GUI_Input_Login", true)[0]
    Last edited by Eri523; May 21st, 2017 at 08:29 AM. Reason: Added missing array index to code snippet
    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.

  12. #12
    Join Date
    Apr 2017
    Posts
    23

    Re: Refer to control in another source file

    Thank you so much!
    That works perfectly.
    Sorry for this problem, it was my fault at the beginning. I tried to do my code as compact as possible and I didn't post the code snip with groupbox, because I didn't know that it's important in this situation.
    I almost lost my hope, but you restored it!
    Thank you so much one more time.
    Best regards, genotypek.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)