Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have namespace TestC and class Form1 in Form11.h - it also has a button_Click function made private:

I want to reference the textBox1->Text in Form11.cpp

But I cannot use this->

I tried TestC::Form1::textBox1 and that worked fine with intellisence but then -> does not work (intellisence throws an error) and . does not work either.

I get an error stating class must be a union/struct on the left of ->

So, how do I go about referincing Text properties in VC++ 2008?

<br />
#pragma once<br />
#include <cstring><br />
#include <iostream><br />
#include <string><br />
<br />
<br />
using namespace System;<br />
using namespace System::ComponentModel;<br />
using namespace System::Collections;<br />
using namespace System::Windows::Forms;<br />
using namespace System::Data;<br />
using namespace System::Drawing;<br />
using namespace std;<br />
<br />
namespace TestC {<br />
<br />
	/// <summary><br />
	/// Summary for Form1<br />
	///<br />
	/// WARNING: If you change the name of this class, you will need to change the<br />
	///          'Resource File Name' property for the managed resource compiler tool<br />
	///          associated with all .resx files this class depends on.  Otherwise,<br />
	///          the designers will not be able to interact properly with localized<br />
	///          resources associated with this form.<br />
	/// </summary><br />
	public ref class Form1 : public System::Windows::Forms::Form<br />
	{<br />
	public:<br />
		Form1(void)<br />
		{<br />
			InitializeComponent();<br />
			//<br />
			//TODO: Add the constructor code here<br />
			//<br />
		}<br />
<br />
	protected:<br />
		/// <summary><br />
		/// Clean up any resources being used.<br />
		/// </summary><br />
		~Form1()<br />
		{<br />
			if (components)<br />
			{<br />
				delete components;<br />
			}<br />
		}<br />
	private: System::Windows::Forms::TextBox^  textBox1;<br />
	private: System::Windows::Forms::Button^  button1;<br />
	private: System::Windows::Forms::TextBox^  textBox2;<br />
	protected: <br />
<br />
	private:<br />
		/// <summary><br />
		/// Required designer variable.<br />
		/// </summary><br />
		System::ComponentModel::Container ^components;<br />
<br />
#pragma region Windows Form Designer generated code<br />
		/// <summary><br />
		/// Required method for Designer support - do not modify<br />
		/// the contents of this method with the code editor.<br />
		/// </summary><br />
		void InitializeComponent(void)<br />
		{<br />
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());<br />
			this->button1 = (gcnew System::Windows::Forms::Button());<br />
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());<br />
			this->SuspendLayout();<br />
			// <br />
			// textBox1<br />
			// <br />
			this->textBox1->Location = System::Drawing::Point(12, 12);<br />
			this->textBox1->Name = L"textBox1";<br />
			this->textBox1->Size = System::Drawing::Size(235, 20);<br />
			this->textBox1->TabIndex = 0;<br />
			// <br />
			// button1<br />
			// <br />
			this->button1->Location = System::Drawing::Point(185, 50);<br />
			this->button1->Name = L"button1";<br />
			this->button1->Size = System::Drawing::Size(61, 28);<br />
			this->button1->TabIndex = 1;<br />
			this->button1->Text = L"button1";<br />
			this->button1->UseVisualStyleBackColor = true;<br />
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);<br />
			// <br />
			// textBox2<br />
			// <br />
			this->textBox2->Location = System::Drawing::Point(17, 54);<br />
			this->textBox2->Multiline = true;<br />
			this->textBox2->Name = L"textBox2";<br />
			this->textBox2->Size = System::Drawing::Size(159, 178);<br />
			this->textBox2->TabIndex = 2;<br />
			// <br />
			// Form1<br />
			// <br />
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);<br />
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;<br />
			this->ClientSize = System::Drawing::Size(284, 262);<br />
			this->Controls->Add(this->textBox2);<br />
			this->Controls->Add(this->button1);<br />
			this->Controls->Add(this->textBox1);<br />
			this->Name = L"Form1";<br />
			this->Text = L"Form1";<br />
			this->ResumeLayout(false);<br />
			this->PerformLayout();<br />
<br />
		}<br />
#pragma endregion <br />
		// Function to convert from System::String to std::string<br />
	private: System::Void  MarshalString ( String ^ s, string& os ) {<br />
   }<br />
<br />
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {<br />
			<br />
<br />
			 }<br />
<br />
	};<br />
}<br />
<br />
Posted
Updated 17-Dec-09 6:10am
v3

I suspect you are not accessing an instance of Form1. However, I can't tell for sure unless you post your code. Could you please post the code for Form11.h?
 
Share this answer
 
It will not let me post the entire Form11.h

The above is as much as it will take I'm afraid.

How ever, It's nothing special - just a bog standard VC++ generated file.
 
Share this answer
 
OK. Try typing
TextBox1->Text = "Hello!";
int the method you want to access the textbox. Then, build the project. If you don't get any warnings, you're good.

Sometimes intellesense can work against you in managed C++.
 
Share this answer
 
Form12.cpp
.\Form12.cpp(9) : error C2065: 'textBox1' : undeclared identifier
.\Form12.cpp(9) : error C2227: left of '->Text' must point to class/struct/union/generic type
type is ''unknown-type''

(I have another basic Form in VC++ 2005 too just for testing purposes).

Basically, I tried TextBox1->Text (which was not how it is declared but thought, why not, eh?) and also tried the declared version and neither work!

<br />
// Form12.cpp : main project file.<br />
<br />
#include "stdafx.h"<br />
#include "Form1.h"<br />
<br />
using namespace Form12;<br />
System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {<br />
<br />
	textBox1->Text = "hello";<br />
<br />
}<br />


If I reference the textBox1->Text using;
this->textBox1->Text = "hleoo!";

in the Form12.h header file method it works. But, writing code in header files is stupid. I want it write in Main.cpp
 
Share this answer
 
v3
You should put your code in the button click handler in Form1.h . The method body you posted is not the the same method as Form1::button1_Click.
 
Share this answer
 
Are you sure I should put the code in the header file? Is that 'done' nowadays? Because, I find that weird! I'm an old C programmer who thinks all the code should go in the cpp files and header files just hold declarations.

Seriously, you sure that is acceptable?
 
Share this answer
 
It's the default behavior, and it's fine. I agree with you that it is better form to put it in a cpp file. However, i would never put class methods in Main.cpp. If you want, just create a file called Form1.cpp and put the definition of the method there (along with the correct #includes and using directives). Make sure you prepend "Form1::" to the method definition. You will want to retain a declaration of the method in the header file.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900