如何从函数内部更改控件属性. [英] How to change control properties from within a function.

查看:121
本文介绍了如何从函数内部更改控件属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在从控制台应用程序过渡,并且有一个小问题.我知道可以通过"this-> label1-> Text = L" Some New Text;"来更改诸如标签文本的值.作为对返回值的响应,单击按钮等.

我正在尝试学习如何在函数返回之前从函数中更改控件的值.例如,假设我有:

Hello,
I am transitioning from console applications and have a small question. I know that values such as the text of a label can be changed by "this->label1->Text = L"Some New Text";" as a response to a return value, button click etc.

I am trying to learn how I can change the values of controls from within a function before the function returns. As an example suppose I have:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             System::Void myfunc();
         }



反过来,在main之后会导致以下代码:



This in turn leads to the following code just after main:

void myfunc()
{
    //We are doing something
    //lets change Label1.Text = L"New Text";
    //now we will do some more stuff

}



我尝试将标签指定为"System :: Windows :: Forms :: Label ....",但一直无法成功使用.请问这里是警告,还是我只是没有正确识别班级成员.

Cheers,Sawyer1



I have tried specifying the label as "System::Windows::Forms::Label...." but have been unsuccessful in getting it to work. Is there a caveat here or am I just not identifying the member of the class correctly.

Cheers,Sawyer1

推荐答案

假定myfunc()button1_Click()事件处理程序属于同一类,则可以在函数中使用相同的代码就像在事件处理程序中一样:

Assuming that myfunc() is a member of the same class as the button1_Click() event handler, you can just use the same code in your function as you would in your event handler:

void myfunc()
{
    this->label1->Text = L"Some New Text";
}



我希望我已经正确理解了您的问题:)

-------------------

嗯,看来我确实误会了这个问题,对不起:)

您想从不在同一类中的函数访问特定类中的控件.在这种情况下,外部函数(myfunc)需要接收对标签的引用,以便可以访问它(否则,函数很难找到 实例). label1)

不幸的是,我的C ++非常生锈,因此我不确定传递引用的语法是什么,但是在C#中,我将执行以下操作:



I''m hoping that I''ve understood your question correctly :)

-------------------

Ah, it seems I did misunderstand the question, sorry :)

You want to access a control in a specific class from a function that''s not in the same class. In that case, the outside function (myfunc) needs to receive a reference to the label, so that it can access it (otherwise it would be very hard for the myfunc function to find an instance of label1)

Unfortunately my C++ is extremely rusty, so I''m not sure what the syntax is for passing references, but in C# I''d do the following:

void myfunc(Label lbl)
{
  lbl.Text = "Some New Text"; // In C++: lbl->Text = L"Some New Text";
}



然后从button1_Click事件处理程序(调用方")内部这样调用:



And then calling it like this from inside the button1_Click event handler (the "caller"):

myfunc(this.label1);



因此,为了使其正常工作,您需要在myfunc函数和调用方中找出label1参数的正确语法.

我希望这有更多用处...



So in order to get this to work, you''ll need to figure out the correct syntax for the label1 argument in both the myfunc function and the caller.

I hope this is of more use...


这篇关于如何从函数内部更改控件属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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