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?