Check with the below code.
private void listBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
int n = listBox2.Items.Count;
double sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + double.Parse(listBox2.Items[i].ToString());
}
double result = sum / n;
if (listBox2.Items.Count > 0)
{
listBox3.Items.Clear();
listBox4.Items.Clear();
label8.Text = "0";
listBox4.Items.Add(result);
listBox3.Items.Add(sum);
label8.Text = sum.ToString();
}
}
}
If it will not resolve your issue please provide complete details like how you are inserting number to listbox.