Obtaining an Index When You Click a Control Stored in a Two-Dimensional Array

Asked 4 months ago, Updated 4 months ago, 13 views

C# stores Label controls in a two-dimensional array as follows:

this.labels=newLabel [8,4];
        for (inti=0; i<labels.GetLength(0); i++)
            for (int j=0; j<labels.GetLength(1);j++)
                This.labels[i,j] = new Label();

                // Property Settings

                This.panel1.Controls.Add (this.labels[i,j]);

Is it possible to generate a click event when I click this label and get the index of the clicked label (although the above code sets the drag drop event...)?
For example, if labels[1,1] are clicked, I would like to get an index called [1,1].
As far as the contents of the Sender are concerned, there is information set in the properties, but there is no index information.

c# visual-studio

2022-09-30 14:06

1 Answers

If you have not already used the Tag property, you may want to write index information to that property.
Any type of data derived from Object can be set and used by the user.
If you don't have the right one, why don't you add a string and convert it each time, or create your own local definition object?

Label class


Retrieves or configures an object that stores data about the Tag control.(From Control)

Control.Tag Properties

Gets or sets the object that stores data about the control.

Types derived from the Object class can be assigned to this property. If the Tag property is set in the Windows form designer, you can only assign text.
A common use of Tag properties is to store data closely associated with controls. For example, if you have a control that displays information about a customer, you can store DataSet containing the customer's order history in the Tag properties of that control for quick access to data.

Here is an example of a similar application.
C#:8 Try creating puzzles using the control array

The index as a one-dimensional array is stored in a tag as a string.

2022-09-30 14:06

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.