hi, i am making a quiz in Visual Studio C++ .NET (Frame wokr) windows Forms and i need to let know the program which from 4 buttons was Clicked. then convert it to the int. the buttons have tags and i found that in c# it can be done this way. dose someone have an idea how to do it in C++ ? ( the event checkAnswar is triggered by clicking one of 4 Buttons )
1 2 3 4
private: System::Void checkAnswar(System::Object^ sender, System::EventArgs^ e) {
auto senderObject = (Button)sender;
int buttonTag = Convert.ToInt32(senderObject.Tag);
First, given a System::Object^ sender parameter, I believe you need to do: Button^ button = safe_cast<Button^>(sender); to cast it to what you expect. (Assuming Button is a known type)
Then, I guess you can access the sender, button->Tag. I don't know what type you placed in the Tag object, so I don't know how you want to cast it. I imagine you can call Convert::ToInt32(tag) in the same way.
it don't work. this is how it looking for a button 3. i guess that the tag is in string.
sorry if i am not helpfull but i don't know how to explain what is going on there
You can help us help you by telling us specifically what is going wrong. Does it not compile? If so, what is the error message given. Does it run, but the behavior is incorrect? If so, what is the expected behavior vs. the actual behavior?
Button^ button = safe_cast<Button^>(sender);
//std::cout << (*button->Tag) << '\n'; // (idk if you are using a console)
int number = Convert::ToInt32(*button->Tag);
MessageBox::Show(number.ToString());
this error pop out;
Validity Code Description Project File Line Skip status
Error (active) E0304 no instance of the item "System :: Convert :: ToInt32" overloaded does not match QuizProjectV2 argument list C: \ Users \ kujaw \ source \ repos \ QuizProjectV2 \ MyForm.h 168
and it is about this part of code
Convert::ToInt32
if i run it anyway nothing is happening when i press buttons
I think the key difference is that I was previously mistaken about what to pass to Convert::ToInt32. Tag is a pointer to string based on your initialization, but ToInt32 expects it to be a pointer, not dereferenced already.