C++ 9 :: 转换“System::Object ^ sender”控制类型

发布于 2024-10-05 09:30:52 字数 594 浏览 1 评论 0原文

这次在 C++ 9 (VS2008) 中,我尝试将“System::Object ^ sender”强制转换为其表示的 Control 类型。

这具体是在 TextBox_TextChanged 事件函数中。

我知道这在 C# 中工作得很好,但是当我在 C++ 中尝试它时,我遇到了错误,而且我似乎找不到 C++ 的等效项。

给我错误的 C++ 代码。 。 。

System::Void txtEmplNum_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
    TextBox thisBox = sender as TextBox ;
}

以及由此产生的错误。 。 。

Error   1   error C2582: 'operator =' function is unavailable in 'System::Windows::Forms::TextBox'  c:\projects\nms\badgescan\frmMain.h 673 BadgeScan

欢迎任何想法。

谢谢!

This time in C++ 9 (VS2008) I am attempting to cast a "System::Object ^ sender" to the Control type that it represents.

This is specifically in a TextBox_TextChanged event function.

I know this works fine in C# but I'm getting errors when I try it in C++ and I can't seem to find the equivalent for C++.

C++ Code that is giving me errors . . .

System::Void txtEmplNum_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
    TextBox thisBox = sender as TextBox ;
}

And the error that results . . .

Error   1   error C2582: 'operator =' function is unavailable in 'System::Windows::Forms::TextBox'  c:\projects\nms\badgescan\frmMain.h 673 BadgeScan

Any ideas are welcome.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-10-12 09:30:52

我想你可能想尝试这个:

System::Void txtEmplNum_TextChanged(System::Object^  sender, System::EventArgs^  e) 
{ 
    TextBox^ thisBox = safe_cast<TextBox^>(sender); 
} 

I think you might want to try this:

System::Void txtEmplNum_TextChanged(System::Object^  sender, System::EventArgs^  e) 
{ 
    TextBox^ thisBox = safe_cast<TextBox^>(sender); 
} 
你怎么敢 2024-10-12 09:30:52

您上面提供的代码不是 C++。 C++没有“as”关键字;对于 C++,该方法编写正确,但代码块错误。

System::Void txtEmplNum_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
    // This is not C++.
    TextBox thisBox = sender as TextBox;

    // This is C++ as already stated above.
    TextBox^ tb = safe_cast<TextBox^>(sender);

    // Or you can just do this if you don't need a handle beyond
    // this line of code and just want to access a property or a method.
    safe_cast<TextBox^>(sender)->Text = "Some Text";
}

The code you provided above is not C++. C++ has no "as" keyword; The method is written correctly for c++ but the code block is wrong.

System::Void txtEmplNum_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
    // This is not C++.
    TextBox thisBox = sender as TextBox;

    // This is C++ as already stated above.
    TextBox^ tb = safe_cast<TextBox^>(sender);

    // Or you can just do this if you don't need a handle beyond
    // this line of code and just want to access a property or a method.
    safe_cast<TextBox^>(sender)->Text = "Some Text";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文