2017-05-05 40 views
0

我正在使用Visual C++ 2010 Express。我有一个窗体(Form1.h),它包含一个文本框(textBox1)。从另一个cpp文件访问Form1的文本框,Visual C++ 2010

我想另一个test.cpp能够访问textBox1并显示消息。

我有类似:

在Form1.h

... standard form code generated by Visual Studio 

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

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) 
{ 
    textBox1->Text = "Connecting to server ..."; 
} 

而且在TEST.CPP包含有类似

.... 

void write (const unsigned char *data, int length) 
{ 
    System::Windows::Forms::textBox1->Text = "Send failed"; 
} 
.... 

编译后,我有以下错误:

  • 'textBox1':不是成员“系统:视窗:形式”
  • 'textBox1的:未声明的标识符
  • 左“ - >正文”必须指向类/结构/联合/通用型
  • 语法错误:缺少“ ;” 'string'之前

我是Visual C++新手,不知道如何正确访问类/对象。 在此先感谢您的帮助。

回答

0

我觉得有些编译器问题可以通过改变来解决:

系统:视窗:形式:textBox1的只是textBox1的

思维的另一种方式是textBox1的是一个管理(^) System :: Windows :: Forms :: TextBox类型的指针。

其他拼写错误可能仍然存在。上面的代码片段似乎在C++/.NET中。这是与“普通”C++不同的野兽。

+0

感谢您的意见。我看到另一个试图做类似的事情,但使用test.cpp中定义的函数并在Form1中调用它。对我来说,我想在test.cpp中使用它。链接是: http://stackoverflow.com/questions/20980056/change-label-text-from-different-header-file-visual-c-2010 –

+0

啊......那么这样的答案似乎是合理的。正如答案的作者所述,文本框对于Form1类是私有的,因此需要访问者函数。 –

+0

如何在test.cpp中调用它? –