C ++ 9 :: Casting "System :: Object ^ sender" for control type

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

This is specifically in the TextBox_TextChanged event function.

I know this works fine in C #, but I get errors when I try to use it in C ++, and I cannot find the equivalent for C ++.

C ++ code that gives me errors.,.

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

And the error that occurs.,.

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.

Thank!

+3
source share
2 answers

I think you can try this:

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

++. ++ ""; ++, .

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

Source: https://habr.com/ru/post/1777718/


All Articles