Passing a parameter though an event handler

I want to pass List < int > ScoresList through -----------------------------------V

    private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
    {
        int target = lstStudents.SelectedIndex;
        if (target != -1)
        {
            Student student = (Student)students[target];
            txtScoreCount.Text = Convert.ToString(ScoresList.Count);
        }
    }

To access------------------------------------------------------------------------------^ the # of scores in my list.

When I do, I have access to my ScoresList, however, I get a "

No overload for 'lstStudents_SelectedIndexChanged' matches delegate 'System.EventHandler'.

The error is more specifically located here in my Designer.

this.lstStudents.SelectedIndexChanged += new System.EventHandler(this.lstStudents_SelectedIndexChanged);

I do not know what to put here that will resolve the issue. Since I'm here I assume I shouldn't pass a parameter through my event? How do I resolve this?

Answers


Adding parameter to event handler doesn't makes sense. Think of who responsible of passing the parameter value? The event handler get invoked by system upon occurrence of respective event. Also event handler method required to follow specific predefined signature, and adding parameter changes method signature, hence you got that error message.

Possible solution is to declare ScoresList as global variable so it will be accessible accross methods in the same scope, including your event handler method.

private List<int> ScoresList = new List<int>();

private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
    int target = lstStudents.SelectedIndex;
    if (target != -1)
    {
        Student student = (Student)students[target];
        txtScoreCount.Text = Convert.ToString(ScoresList.Count);
    }
}

Need Your Help

Silent installation of MySql as Service

mysql windows service silent-installer

i need to install MySql Server in silent mode on Windows.

Get selected items from explorer using C# .NET 3.5

c# .net-3.5 pinvoke explorer

I am writing a .NET 3.5 WPF application in C#. This application needs to be able to get the selected items out of Windows explorer when it is in the foreground.