Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 更改来自不同头文件的标签文本,Visual C++;2010? 我使用Visual C++ 2010 Express。我有一个表单(Form1.h),它包含一个按钮(btn1)和一个标签(label1)_Winforms_Visual Studio 2010_C++ Cli_Header Files - Fatal编程技术网

Winforms 更改来自不同头文件的标签文本,Visual C++;2010? 我使用Visual C++ 2010 Express。我有一个表单(Form1.h),它包含一个按钮(btn1)和一个标签(label1)

Winforms 更改来自不同头文件的标签文本,Visual C++;2010? 我使用Visual C++ 2010 Express。我有一个表单(Form1.h),它包含一个按钮(btn1)和一个标签(label1),winforms,visual-studio-2010,c++-cli,header-files,Winforms,Visual Studio 2010,C++ Cli,Header Files,单击按钮时,我想从不同的头文件(testing.h)调用一个函数,然后继续更改标签中的文本 我所拥有的是这样的东西 表格1.h #include "testing.h" ... standard form code generated by Visual Studio private: System::Windows::Forms::Label^ label1; ... private: System::Void btn1_Click(System::Object^ sender,

单击按钮时,我想从不同的头文件(
testing.h
)调用一个函数,然后继续更改标签中的文本

我所拥有的是这样的东西

表格1.h

#include "testing.h"

... standard form code generated by Visual Studio

private: System::Windows::Forms::Label^  label1;

...

private: System::Void btn1_Click(System::Object^  sender, System::EventArgs^  e) {
        testfunc1();
    }
};
在这里,h有点像

#ifndef _TESTING_FUNCS
#define _TESTING_FUNCS

void testfunc1(){
    label1->Text = "Text has been changed from outside.";
}

#endif
当我试图编译并运行它时,我会收到错误,说
'label1'是一个未声明的标识符
(在testing.h中),并且引用“->Text”左侧的
错误必须指向class/struct/…

<>我是C++新手,通常使用java,所以这里有一些新的东西给我。对我来说,有两个明显的选择:

1) 将标签作为参数传递给函数

2) 以某种方式从
testing.h
头文件访问标签,sans参考


但是我也不知道该怎么做。

标签是一个类的私有变量,就像在Java中一样,不能从外部访问,尤其是在静态上下文中。您可以传递标签,也可以在表单中创建访问器函数并传递整个表单

传递标签的示例:

void testfunc1(System::Windows::Forms::Label^ someLabel)
{
    someLabel->Text = "Text has been changed from outside.";
}
称之为:

System::Void btn1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    testfunc1(label1);
}