开发者

C++ 9 :: Casting "System::Object ^ sender" to Control type

开发者 https://www.devze.com 2023-01-27 10:27 出处:网络
This time in C++ 9 (VS2008) I am attempting to cast a \"System::Object ^ sender\" to the Control type that it represents.

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 =开发者_JAVA技巧 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!


I think you might want to try this:

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


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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消