Solved

How to reference an object on a Windows form ?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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