0

I'm making a program and I have a lot of labels that when you click on them they do the same thing and instead of writing them around 40 times is there a way to reduce it

private: System::Void lblHB15_Click(System::Object^  sender,  System::EventArgs^  e) {
    if (HBColumn1 == true) {
        drop (1);
    }
    if (row1 == 6) {
        btnRow1->Enabled = false;
        HBColumn1 = false;
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

1 Answers1

0

So, your (Matthew's) situation was:

  • You were working on a GUI C++ coding.
  • You have so many visible and clickable "Label" objects (a basic concept of showing a squared area painted by a background color, with a text inside the area painted by a foreground color) on the screen with their own id(name):
    lblHB1, lblHB2, lblHB3, lblHB4, ... , lblHB15, and so on.
  • Each of them has its own linked event (void functions):
    lblHB1_Click(...), lblHB2_Click(...), lblHB3_Click(...), lblHB4_Click(...), ans so on.
  • And you have trouble, because it is not an efficient way to write every individual label's own click event.

The solution is:

  • You have to merge the objects first, to make things a lot easier.
  • Name the labels by exactly the same string. Yes, overlap the name of objects of the same kind like those labels.
  • Then, in your screen / IDE / developer tool set each to a unique "index number" of the objects with the same id (name), in any specific order that you wish.
  • As a result, you will have access to those labels with their names like: lblTitle[0], lblTitle[1], lblTitle[2], lblTitle[3], and so on. Yes! The labels are now declared as an array of label objects.
  • Having set the names and index numbers like this, the event will forward the only one function: lblTitle_Click(...). In this case, the parameter of click function must contain the index of the object. Your developing tool and API must have a specific way to deliver this expression.

Find and study the "indexing" and "event-handling the same kind of objects with the same name".

James Risner
  • 5,451
  • 11
  • 25
  • 47
Steven H
  • 1
  • 1