Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

How to reference an object on a Windows form ?

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
toyboy61
Asked:
toyboy61
  • 2
1 Solution
 
käµfm³d 👽Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
Don't forget to use the * syntax rather than the ^ syntax.
0
 
toyboy61Author Commented:
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
 
käµfm³d 👽Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now