Recieving infomration from sender

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);
Last edited on
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.
Last edited on
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
1
2
3
4
5
6
7
8
this->button3->Location = System::Drawing::Point(471, 457);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(410, 70);
			this->button3->TabIndex = 4;
			this->button3->Tag = L"3";
			this->button3->Text = L"button3";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &MyForm::checkAnswar);
and this is how i guess buttons are link
1
2
3
void InitializeComponent(void){
	this->button1 = (gcnew System::Windows::Forms::Button());
}
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?
i think it doesn't download the Tag or dosen't conert it to int. if i use code like that no message box pop out
1
2
3
4
5
6
7
8
9
private: System::Void checkAnswar(System::Object^ sender, System::EventArgs^ e) {

		Button^ button = safe_cast<Button^>(sender);
		button->Tag;
		CorrectQuestion = 1;
		if (Tag == CorrectQuestion)
		{
			MessageBox::Show("it works");
		}
Okay, so a tag is a managed pointer if I'm reading the documentation at https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.tag correctly [with C++ selected in the right-hand menu]

What happens if you do

1
2
3
4
5
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());
Last edited on
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 finally got around to actually creating my own CLR project instead of just guessing.

This works for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	void InitializeComponent(void)
	{
		this->button1 = (gcnew System::Windows::Forms::Button());
		this->SuspendLayout();
		// 
		// button1
		// 
		this->button1->Location = System::Drawing::Point(57, 45);
		this->button1->Name = L"button1";
		this->button1->Size = System::Drawing::Size(75, 23);
		this->button1->TabIndex = 0;
		this->button1->Tag = L"42";
		this->button1->Text = L"button1";
		this->button1->UseVisualStyleBackColor = true;
		this->button1->Click += gcnew System::EventHandler(this, &MyForm::checkAnswer);
		// 
		// MyForm
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(284, 261);
		this->Controls->Add(this->button1);
		this->Name = L"MyForm";
		this->Text = L"MyForm";
		this->ResumeLayout(false);
	}

	private: System::Void checkAnswer(System::Object^ sender, System::EventArgs^ e)
	{
		Button^ button = safe_cast<Button^>(sender);
		int tagNumber = Convert::ToInt32(button->Tag);

		MessageBox::Show(tagNumber.ToString());
	}


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.
Last edited on
Topic archived. No new replies allowed.