Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to reference an object on a Windows form ?

Posted on 2011-03-09
4
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

721 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