Sebachtian
Sebachtian

Reputation: 37

Read Label Array Information in Event Handler

i have an array label name title[i], and link[i].

Label[] title = new Label[100];
Label[] link = new Label[100];
for (int i = 0; i < 10; i++)
{
title[i] = new Label();
link[i] = new Label();
}

when i click the label title, i can get the link label information too.

title[i].MouseClick += new EventHandler(hover_title);

i try this code doesnt work.

public void hover_title(object sender, EventArgs e)
            {
                title[i].text=link[i].text;
            }

how i can get the label link text when i click the title label.

Upvotes: 0

Views: 100

Answers (2)

Enigmativity
Enigmativity

Reputation: 117175

You can do this:

Label[] title = new Label[100];
Label[] link = new Label[100];
for (int i = 0; i < 10; i++)
{
    var j = i;
    title[j] = new Label();
    link[j] = new Label();
    title[j].MouseClick += (s, e) => title[j].Text = link[j].Text;
}

Upvotes: 1

DotNet Developer
DotNet Developer

Reputation: 3018

Something like following should solve your problem.

    public void hover_title(object sender, EventArgs e)
    {
        var label = sender as Label;
        int i = (title as IList).IndexOf(label);
        label.Text = link[i].Text;
    }

And remember, after you create a control you must give it a new location, in case of Label set a text, a new size (in case of Label you can set AutoSize property to true), and add it to parent control's Controls collection.

Upvotes: 1

Related Questions