【问题标题】:accessing text box of Form1 from another cpp file, Visual C++ 2010从另一个 cpp 文件 Visual C++ 2010 访问 Form1 的文本框
【发布时间】:2017-05-05 09:54:16
【问题描述】:

我正在使用 Visual C++ 2010 Express。我有一个包含文本框 (textBox1) 的表单 (Form1.h)。

我希望另一个 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' : 不是'System::Windows::Forms'的成员
  • 'textBox1':未声明的标识符
  • '->Text' 的左边必须指向类/结构/联合/泛型类型
  • 语法错误:缺少';'在“字符串”之前

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

【问题讨论】:

    标签: c++ visual-studio-2010


    【解决方案1】:

    我认为一些编译器问题可以通过更改来解决:

    System::Windows::Forms::textBox1 到 textBox1

    另一种思路是 textBox1 是 System::Windows::Forms::TextBox 类型的托管 (^) 指针。

    其他错别字可能仍然存在。上面的 sn-ps 似乎在 C++/.NET 中。这是与“常规”C++ 不同的野兽。

    【讨论】:

    • 感谢您的 cmets。我看到另一个链接试图做类似的事情,但使用 test.cpp 中定义的函数并在 Form1 中调用它。对我来说,我想在 test.cpp 中调用它。链接为:stackoverflow.com/questions/20980056/…
    • 啊...那么答案似乎很合理。正如答案的作者所说,文本框是 Form1 类私有的,因此需要一个访问器函数。
    • 如何在 test.cpp 中调用它?
    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多