Access text box from a .cpp file not the original .h.

So if I access the textBox9 from the Form1.h file I have no issue editing the contents by simply doing this

textBox9->Text = "whatever";

However when I try to do this in a separate .cpp... Well all I get is errors.

I've tried

namespace::textBox9->Text
Form1::textBox9->Text
System::textBox9->Text
this->textBox9->Text

changed textBox9 to public from private.

Tried a few other things (like a dozen). And nothing seems to be working. It just doesn't want to access that form a separate file. I'm completely lost.
You will forward-declare textBox9 in the header, but it will be defined in another C++ file. You'll use extern to link to that object so that you don't create a new object.
Sorry I'm pretty new. None of what you said makes any sense to me. I guess I should have mentioned that.

So there are 2 files Form1.h which apparently has no problem whatsoever accessing textBox9 I can do whatever I want from there

And something.cpp which regardless of the fact that I did #include "Forms1.h" just keeps saying "nope don't recognize it".

I quickled googled extern but I've never used it before. Could you please provide an example. Here's what I'm workign with

Form1.h

private: System::Windows::Forms::TextBox^ textBox9;

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
somefunction();
}



something.cpp

int somefunction() {
textBox9->Text = "Yeah this never works";
}

What exactly do you want here? Why are you trying to access this outside of the file? Why not make a setter function?

I decided to give you a silly example with a static variable to help you out anyways though.
Form1.cpp:
1
2
3
4
5
6
7
8
class Form1
{
    private: static int x;
    private: friend void some_function();
    public:  static int getX(){ return x; }
};

int Form1::x = 2;

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Form1.h"

void some_function()
{
    Form1::x = 5;
}

int main(int argc, char* argv[])
{
    some_function();
    std::cout << Form1::getX() << std::endl;
    return 0;
}


This will print 5 to the screen. If you comment out the call to some_function in main, then it should print 2 instead.
Personally, I cannot think of a decent reason for doing this on what I assume is a Windows form app designed in Visual Studio.
Last edited on
Yes I am designing a window in Visual Studio. I want to edit the code in a .cpp file. But all the form stuff is in the .h file. And it appears to only want to recognize the objects from the .h file.

I tried doing

Form1::textBox9->Text = "doesnt work";

tried

namespace::Form1::textBox9->Text = "doesnt work either";

Just keeps saying that the name space I use in Form1.h is not recognized. Same goes with Form1. It's as if Form1.h is not even being read in even though I'm doing #include "Form1.h".

The reason you have to do it this way is because if I try to move the form files to the .cpp.... Guess what they aren't recognized there either. Probably because I'm not declaring them properly.

I'm very new at this stuff so more complex things like this are totally alien to me.

With your example the issue for me is I'm using pre-built code..

private: System::Windows::Forms::TextBox^ textBox9;

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
somefunction();
}


And if I try using the namespace in Form1.h it just tells me to go.....
namespace::Form1::textBox9->Text = "doesnt work either";

What is the name of the namespace in Form1? I am going to bet you a million bucks it is not namespace, because namespace is a keyword in c++

You are going to have to post more of this Form1.h file for others not familiar with Visual Studio to help.
If this is your first attempt at creating a GUI I would recommend something simpler like SFML instead of WinForms in Visual Studio. Especially if you do not understand the syntax of C++.
Last edited on
Yeah the namespace name is "arr" without the quoataions. But no matter what

arr::Form1::
Form::
arr::
this->
System::

Everything I could possibly think of. Just isn't working.

I don't mind posting the code but it's way too big for this.

When I'm in Form1.h in the top right corner it says arr::Form1

But when I go to retard.cpp (I named it that because it was pissing me off)

its

using namespace std;

........
a ton of code
......
int calky() {
arr::Form1::Card1Box->Text = "BOOOOZA!";
return 6969;
}

And that never works

Just always says it doesn't recognize Form1, doesn't recognize arr. It's as if they are completely separate entities... even though they are not.
I have modified my example to work within namespace arr. Although I still think this is a bit silly.
Form1.h:
1
2
3
4
5
6
7
8
9
10
namespace arr
{
    class Form1
    {
        private: static int x;
        private: friend void some_function();
        public:  static int getX(){ return x; }
    };
    int Form1::x = 2;
}

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Form1.h"

namespace arr
{
    void some_function()
    {
        Form1::x = 5;
    }
}

int main(int argc, char* argv[])
{
    arr::some_function();
    std::cout << arr::Form1::getX() << std::endl;
    return 0;
}
I think the problem here is if I wrap the contents of Form1.h with class it will stop working.

If I try to do
'

namespace arr
{
void something ()
{
&arr::Form1::Card1Box->Text = "BOOOOZA!";
}
}

In another file called arr.cpp which is supposed to be the "main file".

I get the following error

error C2227: left of '->Text' must point to class/struct/union/generic type

It's saying Card1Box needs to be a pointer. But I think it just means that it has no idea what Card1Box is. Ofcourse I tried

arr::Form1
and
Form1::

For good measure

I never get to int main cause I already have an error by the time I try to reference Card1Box->Text.

Card1Box->Text is what I'm trying to access. It is declared like this

(I changed it to public, but it was private and neither works)
Form1.h

namespace arr
{
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
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ NumHandsBox;

public: System::Windows::Forms::TextBox^ Card1Box;
private: System::Windows::Forms::TextBox^ Card2Box;


private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ Flop1box;
private: System::Windows::Forms::TextBox^ Flop2Box;
private: System::Windows::Forms::TextBox^ Flop3Box;
private: System::Windows::Forms::TextBox^ Flop4Box;
private: System::Windows::Forms::TextBox^ Flop5box;





private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ WinBox;

private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Label^ label6;
private: System::Windows::Forms::TextBox^ TieBox;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button7;
private: System::Windows::Forms::Button^ button8;
private: System::Windows::Forms::Button^ button9;
private: System::Windows::Forms::Button^ button10;
private: System::Windows::Forms::Button^ button11;
private: System::Windows::Forms::Button^ button12;
private: System::Windows::Forms::Button^ button13;
private: System::Windows::Forms::Button^ button14;
private: System::Windows::Forms::Button^ button15;
private: System::Windows::Forms::Button^ button16;
private: System::Windows::Forms::Button^ button17;
private: System::Windows::Forms::Button^ button18;
private: System::Windows::Forms::Button^ button19;
private: System::Windows::Forms::Button^ button20;
private: System::Windows::Forms::Button^ button21;
private: System::Windows::Forms::Button^ button22;
private: System::Windows::Forms::Button^ button23;
private: System::Windows::Forms::Button^ button24;
private: System::Windows::Forms::Button^ button25;
private: System::Windows::Forms::Button^ button26;
private: System::Windows::Forms::Button^ button27;
private: System::Windows::Forms::Button^ button28;
private: System::Windows::Forms::Button^ button29;
private: System::Windows::Forms::Button^ button30;
private: System::Windows::Forms::Button^ button31;
private: System::Windows::Forms::Button^ button32;
private: System::Windows::Forms::Button^ button33;
private: System::Windows::Forms::Button^ button34;
private: System::Windows::Forms::Button^ button35;
private: System::Windows::Forms::Button^ button36;
private: System::Windows::Forms::Button^ button37;
private: System::Windows::Forms::Button^ button38;
private: System::Windows::Forms::Button^ button39;
private: System::Windows::Forms::Button^ button40;
private: System::Windows::Forms::Button^ button41;
private: System::Windows::Forms::Button^ button42;
private: System::Windows::Forms::Button^ button43;
private: System::Windows::Forms::Button^ button44;
private: System::Windows::Forms::Button^ button45;
private: System::Windows::Forms::Button^ button46;
private: System::Windows::Forms::Button^ button47;
private: System::Windows::Forms::Button^ button48;
private: System::Windows::Forms::Button^ button49;
private: System::Windows::Forms::Button^ button50;
private: System::Windows::Forms::Button^ button51;
private: System::Windows::Forms::Button^ button52;
private: System::Windows::Forms::Button^ button53;
private: System::Windows::Forms::Button^ button54;



protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code


..............

LOTS MORE CODE
..................

Last edited on
That Form1.h file of yours is in C#, not C++...
Apparently I just did not know about ref classes that Microsoft's compiler allows.

My examples were for static variables.
You are trying to access non-static variables as if they were static
&arr::Form1::Card1Box->Text = "BOOOOZA!";

You need an instance of Form1, then you could access non-static members such as Card1Box.

For example if I had a rectangle class some likely code could be:
1
2
3
4
5
    Rectangle r;  // r is an instance of class Rectangle
    r.height = 7.0;  // instances can reference non-static public members
    r.width = 5.0;
    double area = r.calcArea();
    ...


Do not go haphazardly creating an instance to Form1 though. There should only be one instance of the GUI window. You will have to pass around a reference to that one instance. Or you could just change the Card1Box to static and try out what I said earlier... But I still do not see why you are trying to do this. Typically in my GUIs I would call a function external to the GUI code and use return value(s) to modify what the GUI displays.
Last edited on
Thank you for your input.

How would I do this? I'm not that familiar with code. I'm still learning.
Also I tried this
arr.cpp

1
2
3
int calky2() {
	Form1 something;
	something.activewindow->Text="test";


I get this error. (I obviously tried Card1box first, that didn't work either. I was hoping redeclaring it would work). GOD THIS IS SO OVERCOMPLICATED.

1>arr.cpp(26): error C2039: 'activewindow' : is not a member of 'arr::Form1'

EVEN THOUGH IT IS PART OF arr::Form1 :( :( :( :( :( :(

Form1.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
namespace arr {

	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
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			TextBox ^ activewindow = Card1Box;
		    //activewindow->Text = "testing variable";
			//activewindow->BackColor = System::Drawing::Color::PaleTurquoise;

			//
		}

.............. some code.........

private: System::Windows::Forms::TextBox^  Card1Box;


I made a little bit of progress.

arr.cpp

1
2
3
4
5
6
using namespace arr;


int calky2() {
	Form1 something;
	something.Card1Box->Text="test";


This was a big change here.

Form1.h

public: System::Windows::Forms::TextBox^ Card1Box;

But I got a new problem. Apparently arr.cpp was NEVER added to Form1.h. And when I do that. Because ultimately I want to add the functions in arr.cpp to "On click" functions of the windows. That's the whole point of doing this. It just throws a ton of crazy errors at me.

Now it's importnat I DO NOT GET THESE ERORRS. If I don't include arr.cpp on Form1.h

1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(13): error C2871: 'arr' : a namespace with this name does not exist
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(17): error C2065: 'Form1' : undeclared identifier
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(17): error C2146: syntax error : missing ';' before identifier 'something'
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(17): error C2065: 'something' : undeclared identifier
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(18): error C2065: 'something' : undeclared identifier
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(18): error C2228: left of '.Card1Box' must have class/struct/union
activewindow is a local variable that exists only in the scope of Form1's constructor...

I suggest you read about scope, and static members
Some reading that could help with you understanding c++:
http://en.cppreference.com/w/cpp/language/scope
http://en.cppreference.com/w/cpp/language/static
http://en.cppreference.com/w/cpp/language/member_functions

I warned you against this:
Form1 something;
That is bad, because there should only be one instance of your GUI window class.

The namespace arr and class Form1 is probably not being found because Form1.h has not been included to arr.cpp yet

Since you will be calling functions in arr.cpp from Form1.h button_clicks, you could just pass the Text by reference or pointer to the functions that you call.
Last edited on
"you could just pass the Text by reference or pointer to the functions that you call."

How would I do that? Everytime I try to reference anything it always fails.

Honestly even if it is bad it's the only way it is half working right now. Eveyrthing else is completely broken.

I have 3 files
retard.cpp (named it this out of frustration)
which has
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
#include <limits>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm> 
#include "Form1.h" 


This one acts as if namespace "arr" is not of this planet. I can't reference it AT ALL.

arr.cpp

1
2
3
4
5
6
7
8
9
 #include "stdafx.h"
#include "Form1.h"
#include <time.h>
#include <stdlib.h>
#include <limits>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>  


This one can do the above. But adding it to Form1.h breaks everything.

And

Form1.h
1
2
3
4
#pragma once
#include "retard.cpp"
//#include "arr.cpp"
#include <sstream>  


No matter what adding arr.cpp completely breaks it. arr.cpp "Form1 something;" only works if I DO NOT add arr.cpp to Form1.h.

All this makes very little sense to me.

I guess I'll go read the links you gave me. Although I have went over stuff like this before and it didn't really maek much sense to me then either. It's overcomplicated if you ask me to be honest.
Last edited on
activewindow is a local variable that exists only in the scope of Form1's constructor...

I suggest you read about scope, and static members
Some reading that could help with you understanding c++:
http://en.cppreference.com/w/cpp/language/scope
http://en.cppreference.com/w/cpp/language/static
http://en.cppreference.com/w/cpp/language/member_functions

I warned you against this:
Form1 something;
That is bad, because there should only be one instance of your GUI window class.

The namespace arr and class Form1 is probably not being found because Form1.h has not been included to arr.cpp yet

Since you will be calling functions in arr.cpp from Form1.h button_clicks, you could just pass the Text by reference or pointer to the functions that you call.


I just read the 3 articles. Honestly it may be a bit over my "experience" because some of the stuff they were talking about was completely greek to me.

But what from I read I should be able to

namespare arr {
do whatever
}

Or just make it static.

Neither of which works of course. I tried it all before I even read that.

The only thing that remotely works is if

1) I DO NOT #include arr.cpp in Form1.h

2) I do this in arr.cpp

1
2
3
4
5
using namespace arr;

int calky2() {
	Form1 something;
	something.Card1Box->Text="test";


But it's all for not because I can't use calky2() in Form1.h because I can't #include it. The second I do it immediately starts throwing those errors (acting like it doesn't exist) even though it clearly does.

I'm obviously completely missing something.

But I have no idea what.

This is the errors I get btw if I do #include arr.cpp to Form1.h. Makes absolutely NO SENSE AT ALL to me.

1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(13): error C2871: 'arr' : a namespace with this name does not exist
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(17): error C2065: 'Form1' : undeclared identifier
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(17): error C2146: syntax error : missing ';' before identifier 'something'
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(17): error C2065: 'something' : undeclared identifier
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(18): error C2065: 'something' : undeclared identifier
1>c:\users\nmatveev\documents\visual studio 2010\projects\arr\arr\arr.cpp(18): error C2228: left of '.Card1Box' must have class/struct/union
Last edited on
I do not think you should be including .cpp files at the top of Form1.h, but as long as Visual Studio is not compiling those and linking them a second time it should be fine. But you have more serious problems of circular includes. You have to realize that something is wrong when you have a file A that includes file B, but file B also includes file A. What you can do is make a file C that has function prototypes of file B, and then instead of including file B in file A, you would include file C. This allows you to call functions in A that are defined in B.

How would I do that? Everytime I try to reference anything it always fails.

You add a parameter to a function in arr.cpp that is of type System::Text^ or whatever that string type is exactlySystem::String^. Then in your button_click you can simply pass Card1Box->Text as one of the arguments. You will need to pass this parameter by reference or pointer. This is so that the function can modify the actual source string and not a copy that would be created locally for the function.

I really do think you need to read more about scope, until it is not greek to you. Scope is something they will teach in any introductory programming class.
I tried to find some simpler reading for you. That cppreference site is a better reference, but only useful after you learn more.
Scope:
http://www.drdobbs.com/cpp/scope-regions-in-c/240002006
http://msdn.microsoft.com/en-us/library/b7kfh662.aspx
Pointers:
http://www.cplusplus.com/doc/tutorial/pointers/
Passing by reference:
http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic=%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fcplr233.html
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
While looking this stuff up for you I found that I quite like this IBM website. Lots of useful c++ stuff there.
Last edited on
In case anyone runs across this problem. I found the solution here. kevinkjt2000 pretty much had the right idea as well.

http://stackoverflow.com/questions/20980056/change-label-text-from-different-header-file-visual-c-2010

Form1.h
calcc(WinBox, TieBox);

somewhere.cpp

void calcc(System::Windows::Forms::TextBox^ WinBox, System::Windows::Forms::TextBox^ TieBox)
{
WinBox->Text = "Wins";
TieBox->Text = "Ties";
}

So the key was passing them as System::Windows:Forms inside the function variables.

THANK YOU FOR EVERYONE WHO HELPED ME! I wouldn't have known where to look if I didn't look at the material you sent me.

Thank you!
Topic archived. No new replies allowed.