正在访问->文本属性问题 [英] Accessing ->Text property problem
问题描述
我在Form11.h中具有名称空间TestC和类Form1-它还具有一个设为私有的button_Click函数:
我想在Form11.cpp中引用textBox1-> Text
但我不能使用此->
我尝试了TestC :: Form1 :: textBox1,并且可以很好地使用智能,但是->不起作用(智能引发错误)和.也不起作用.
我收到一个错误消息,指出类必须是->
左侧的并集/结构
那么,如何在VC ++ 2008中引用Text属性呢?
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 />
推荐答案
我怀疑您没有访问Form1的实例.但是,除非您发布代码,否则我无法确定.
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?
它不能让我发布整个Form11.h
吗?
以上就是我所担心的全部.
但是,没什么特别的-只是一个标准的VC ++生成的文件.
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.
好.尝试在要访问文本框的方法中键入
OK. Try typing
TextBox1->Text = "Hello!";
int.然后,构建项目.如果没有收到任何警告,则表示您很好.
有时,智能感知可能会在托管C ++中对您不利.
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++.
这篇关于正在访问->文本属性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!