Forms 静态和非静态方法或全局类对象 我在VS2012 C++中制作了Windows窗体应用程序。 例如,实际项目更为复杂: 我有一个包含文本框、按钮和计时器的表单。 按钮只是触发计时器。计时器只调用增加某个变量的函数。 我需要在文本框中显示递增的函数变量
在表1.h中,我添加了代码:Forms 静态和非静态方法或全局类对象 我在VS2012 C++中制作了Windows窗体应用程序。 例如,实际项目更为复杂: 我有一个包含文本框、按钮和计时器的表单。 按钮只是触发计时器。计时器只调用增加某个变量的函数。 我需要在文本框中显示递增的函数变量,forms,static,global,non-static,Forms,Static,Global,Non Static,在表1.h中,我添加了代码: public: void Timer_Function(); //function activated by timer Tick void Set_Text(String ^str); //function to set TextBox Text private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
public: void Timer_Function(); //function activated by timer Tick
void Set_Text(String ^str); //function to set TextBox Text
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (timer1->Enabled == false) timer1->Enabled = true;
else timer1->Enabled = false;
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
Timer_Function();
}
在My_app.cpp代码中,如下所示:
#include "stdafx.h"
#include "Form1.h"
#include "resource.h"
using namespace test_staticfunc;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
void Form1::Timer_Function()
{
Timer_Func();
}
void Form1::Set_Text(String ^str)
{
textBox1->Text = str;
}
void Timer_Func()
{
static int I=0;
I++;
Form1::Set_Text(I.ToString());
}
void Timer_Func();
也就是说,我试图通过将Timer_Func()的内部变量I传递给Form1公共方法集_Text()来显示它的当前状态。
所以这里的错误是Set_Text()不是静态方法。
我试图将其设置为静态,但出现了一个错误“С2227:“->Text”左侧的操作数不是指向类、结构或联合的指针。”?在这种情况下,静态方法试图实现非静态方法,对吗
或者另一种方法:创建Form1的实例-而不是
Application::Run(gcnew Form1());
插入代码
Form1 ^My_form = gcnew Form1();
Application::Run(My_form);
并将Set_Text用作类实例My_表单的非静态方法。
但是我的表格只在main()中可用!我在别的地方都做不到。有没有办法让它全球化或者什么的
有没有其他方法可以解决这个问题
救命啊!我已经在几个论坛上搜索了答案,但没有找到答案。更确切地说,他们中的任何一个都不适合。
另外,对不起,我的英语不好^_^