更改标签文本从不同的头文件,Visual C ++ 2010? [英] Change label text from different header file, Visual C++ 2010?

查看:85
本文介绍了更改标签文本从不同的头文件,Visual C ++ 2010?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual C ++ 2010 Express。我有一个表单( Form1.h ),其中包含一个按钮( btn1 )和一个标签( label1 )。

I am using Visual C++ 2010 Express. I have a form (Form1.h) that contains a button (btn1) and a label (label1).

当我单击按钮时,我想从其他头文件( testing.h ),然后将继续更改标签中的文本。

When I click the button, I would like to call a function from a different header file (testing.h) that will then proceed to change the text in the label.

我所拥有的是这样的...

What I have is something like this...

Form1.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();
    }
};

其中testing.h类似于...

Where testing.h is something like...

#ifndef _TESTING_FUNCS
#define _TESTING_FUNCS

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

#endif

当我尝试编译和运行它,我得到一个错误,指出'label1'是一个未声明的标识符(在testing.h内),以及一个错误消息,引用了 '->文本'必须指向class / struct /...

When I try to compile and run it, I get errors saying that 'label1' is an undeclared identifier (within testing.h), and an error referring to the "left of '->Text' must point to class/struct/..."

我是C ++的新手,通常使用Java,因此我这里有一些新东西。对我来说,有两个明显的选择:

I am new to C++ and usually use Java, so there are a few new things for me here. To me, there are two obvious options:

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

1) Pass the label to the function as an argument

2)以某种方式从 testing.h 头文件访问标签,没有引用

2) Somehow access the label from the testing.h header file, sans reference

但是我不是真的

推荐答案

标签是类的私有变量,就像在Java中无法从外部,尤其是在静态环境中。您可以传递标签,也可以在表单中创建访问器函数并传递整个表单。

The label is a private variable of a class and just like in Java is not accessible from the outside, especially not in static context. You can pass the label, or you can create an accessor function in your Form and pass the whole form.

传递标签的示例:

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);
}

这篇关于更改标签文本从不同的头文件,Visual C ++ 2010?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆