How does ^ (handle to object operator) works?

时间:2017-01-03 10:23:10

标签: winforms c++-cli

I can remove last char from textBox by

textBox1->Text = textBox1->Text->Remove(textBox1->Text->Length - 1);

And I want to do it creating a reference or a pointer to the Text. But I can't do it like this

String^ t = textBox1->Text;
t = t->Remove(t->Length - 1); // Text doesn't change

P.S. Any of this doesn't compile:

String* t = textBox1->Text; // error: an ordinary pointer to a
                            // C++/CLI ref class or interface class
                            // is not allowed

String& t = textBox1->Text; // error: an ordinary reference to a
                            // C++/CLI ref class or interface class
                            // is not allowed

String% t = textBox1->Text // error: a tracking reference to a
                           // System::String is not allowed

1 个答案:

答案 0 :(得分:2)

t = t->Remove(t->Length - 1);

只需更改本地引用(pointer-thingy,handle,...)t,以引用Remove返回的字符串。

文本框字符串不会因此而改变。

当你这样做时

textBox1->Text = t;

...您正在为属性 Text调用 setter函数,此setter函数不仅会更改文本框字符串数据,还会更新或更新至少使文本框显示无效。