Solved

How to reference an object on a Windows form ?

Posted on 2011-03-09
4
201 Views
Last Modified: 2012-05-11
I have the attached code snippet. On Form2 I have many buttons, but only two of the functions are given here.

When I click on Button1 (or Button2..) I want the control to transfer to AllButtons_Click, and update the Text-field of Button1 or Button2 with the letter "X".

Button1.Text = "X";
Button2.Text = "X";

Question 1: Is this a good way to do it ?
Question 2: How do I reference a "generic" Button-object in the AllButtons_Click function ?

private: System::Void AllButtons_Click(System::Object *  sender, System::EventArgs *  e) 
{
// When this event occurs I want to update the Text-field in // the object with i.e. the letter "X".
}

private: System::Void Button1_Click(System::Object *  sender, System::EventArgs *  e)
{
  this->Button1->Click += new System::EventHandler(this, &Form2::AllButtons_Click);
}

private: System::Void Button2_Click(System::Object *  sender, System::EventArgs *  e)
{
  this->Button2->Click += new System::EventHandler(this, &Form2::AllButtons_Click);
}

Open in new window

0
Comment
Question by:toyboy61
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 35088615
You have two routes you can take: you can declare the general handler and point both buttons' click events to it (without needing a separate handler for each); you can create a separate function that you call from each button's specific handler. An example of both follows.

Example 1
Common Handler
// Cast the "sender" to a Button and you can refer to the button that was clicked
private: System::Void AllButtons_Click(System::Object^  sender, System::EventArgs^  e) {
     ((System::Windows::Forms::Button^)sender)->Text = "X";
}

Open in new window


Set the handler
 Set each button's click handler to be the common handler

Example 2
Separate Handlers with Common Function
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    SetButtonText(this->button1);  // Call common function
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    SetButtonText(this->button2);  // Call common function
}

private: System::Void SetButtonText(System::Windows::Forms::Button^ clickedButton) {
     clickedButton->Text = "X";
}

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35092030
Don't forget to use the * syntax rather than the ^ syntax.
0
 

Author Comment

by:toyboy61
ID: 35093570
AndyAinscow: I'm aware of that.. :-))).
kaufmed: I'm using Visual Studio .NET 2003, and the syntax has apparently been changed since then.
So I'm not able to use expressions like "System:Object^ ", but must use "System:Object* ".  

I'm may be a newbie on C++-programming, but I have been programming in Basic (sic!), Pascal, Fortran and Cobol in the 80's. Later on I have been doing some simple programming i Visual Basic 6.0, and I have also tried a bit on Java. Now I'm focusing on (Visual) C++.. Object-oriented programming is not that hard to learn (or understand), but I'm overwhelmed when I see all the libraries, classes and so on you find in the Microsoft/Windows-world. Seems to take a lifetime to learn all of it.. .:-)))
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35094573
Sorry about that. I was using VS 2008 for the example, and I don't code in C++ (except for answering questions here!). The syntax may very well be different--I don't have 2003 to confirm.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now