1

I have a User Control which contains 4 labels. I populate a panel dynamically with multiple copies of this user control (one for each entry in a database). I want to capture a click event on any one of the user controls and grab the id associated with it.

The is the code contained within my user control:

public partial class TeamVsTeam: UserControl
{
    public TeamVsTeam()
    {
        InitializeComponent();
    }

    public int eventID { get; set; }
    public int idHomeTeam { get; set; }
    public int idAwayTeam { get; set; }
}

Within my application, I populate a panel with multiple controls like this:

TeamVsTeam.TeamVsTeam[] teamLabel = new TeamVsTeam.TeamVsTeam[n];

for (int i = 0; i < m_fixtures.lstTeams.Count; i++)
{
    teamLabel[i] = new TeamVsTeam.TeamVsTeam();

    teamLabel[i].eventID = m_fixtures.lstTeams[i].idEvent;

    teamLabel[i].lblHome.Text = m_fixtures.lstTeams[i].homeTeam.ToString();
    teamLabel[i].lblAway.Text = m_fixtures.lstTeams[i].awayTeam.ToString();

    teamLabel[i].Clicked += new EventHandler(Fixture_Click);

   //Location
    teamLabel[i].Top = 80 * i;
    teamLabel[i].Parent = panel1;
}

Is anyone able to suggest some code which I can use to capture the click event (anywhere on the user control) and obtain the eventID, homeTeam and awayTeam from the control which was clicked?

    public void Fixture_Click(object sender, EventArgs e)
    {
        var ctrl = ((TeamVsTeam.TeamVsTeam)sender);
        int ID = ctrl.eventID;
        MessageBox.Show(ID.ToString());
    }

EDIT I've updated the above code to show what I'm now working with, however, it's not completely solved things for me.

Here's an example of a fully populated user control (3 labels and 2 picture boxes)

https://www.dropbox.com/s/0tidlarzfoy6j2u/2015-09-30%2015_48_35-Super-6.jpg?dl=0

If I click on any of the light grey background, the click event is triggered and I can grab the fixture eventID. But the event is not triggered if I click on any of the labels or picture boxes. I still need to capture the fixture eventID, regardless of what part of the control is clicked. Any ideas?

0

1 Answer 1

1

you should be able to get data from sender after type cast

public void Fixture_Click(object sender, EventArgs e)
{
     var ctrl = ((TeamVsTeam.TeamVsTeam)sender);
     int ID = ctrl.eventID;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes! That's what I was after. Although the Fixture_Click event isn't actually being triggered when I click on a control. Have I missed something else out?
@NickRoberts, is Fixture_Click in TeamVsTeam triggered?
Ah, I just removed the public void Fixture_Click(object sender, EventArgs e) section from within the user control itself and has done the trick. Again, thanks for your help
Sorry, this has created one more question. I've updated the original question with my new code...
@NickRoberts, possible fix: change TeamVsTeam .ctor a bit to raise Click event after click on child controls: InitializeComponent(); label1.Click += (s, e) => OnClick(e); pictureBox1.Click += (s, e) => OnClick(e); /*etc*/; or InitializeComponent(); foreach (Control c in Controls) c.Click += (s, e) => OnClick(e);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.