简体   繁体   中英

Creating custom Label in C# and passing data in events

I had been playing around with an idea for a game, and implementation was going fairly well, but I have hit a stumbling block.

Basically, I have a form, which will show talent trees. I am just going to use labels to display the relevant details, and I want to create them programmatically. The display part is working fine, the part I am having trouble with is adding an event handler to the labels.

I want to be able to pass data during the event handling, so that I can identify which specific label was clicked, but I am hitting a brick wall. So when a particular label is clicked, the name of its associated skill (just passing a string) will be sent to the event handler. Any help would be appreciated. Here is the relevant code that I have:

        public void DisplayTree()
    {
        int i=0;
        startPoint.X = 40;
        startPoint.Y = 125;

        foreach(SkillNode s in tree.tier1)
        {
            for (i=0; i < s.labels.Count;i++ )
            {
                //Displays a label for each available rank for a skill
                s.labels.ElementAt(i).Text = (i+1).ToString()+"/"+s.maxRank.ToString();
                s.labels.ElementAt(i).Location = startPoint;
                startPoint.Y += s.labels.ElementAt(i).Height + 2;
                s.labels.ElementAt(i).Name = "lbl"+s.name+i.ToString();

                //Only enable it to be clicked if the user is at the correct rank
                if (s.rank == i)
                {
                    s.labels.ElementAt(i).Enabled = true;
                }
                else
                {
                    s.labels.ElementAt(i).Enabled = false;
                }
                //Add Event here
                //I want to pass the name of the skill with the event

                this.Controls.Add(s.labels.ElementAt(i));
            }
            startPoint.X += s.title.Width + 5;
            startPoint.Y = 125;
        }
    }
    public void LabelClick()
    {
        //Code here to pick out the name of the label


    }

Try this:

    public void LabelClick()
    {
       Console.WriteLine(((Control)sender).Name);
    }

When you create an event and want to follow the official C# styleguide, you follow the following pattern:

public delegate void {YourName}EventHandler(object sender, {YourName}EventArgs args);
public event {YourName}EventHandler EventName;

Every information about what happened in the event or can be manipulated by the subscriber is stored in a class that inherits EventArgs . The delegate also contains a reference to the sender, which is the object that fires the event.

When you fire an event you do the following, regularly in a protected method that has the same name as the Event with an "On" as prefix:

EventName?.Invoke(this, new {YourName}EventArgs() { Initialize Stuff });

As you can see, you can work with the sender and identify the object. In your case you could also change object sender to UIElement sender (or similar) to make it easier to identify stuff without a cast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM