Solved

How to reference an object on a Windows form ?

Posted on 2011-03-09
4
197 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 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
Don't forget to use the * syntax rather than the ^ syntax.
0
 

Author Comment

by:toyboy61
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

743 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

17 Experts available now in Live!

Get 1:1 Help Now