내가 가지고 ListForm
있는 C#
목록에서 특정 학생에 사용자가 클릭 나는 그것이 평균 점수 수와 합계를 찾으려 할 때 질문입니다.
아래 코드 :
private void listForm1_SelectedIndexChanged(object sender, EventArgs e)
{
txtTotal.Text = listForm1.SelectedItem.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
listForm1.Items.Add("Hamish overtonne" + "|" + 39 + "|" + 12 + "|" + 85);
listForm1.Items.Add("Claudia Dye" + "|" + 44 + "|" + 56 + "|" + 85);
listForm1.Items.Add("Mike Layne" + "|" + 12 + "|" + 47+ "|" + 28);
}
그게 내가 가진 전부 야
이를 위해 사용할 수있는 코드는 다음과 같습니다. SelectedIndexChanged
목록에 이벤트를 등록하는 것을 명심 하십시오.
또한 내가 한 것처럼 정수를 사용하면 소수점없이 정수가 생성된다는 점을 명심하십시오.
프로세스를 설명하기 위해 코드 주석을 추가했습니다.
private void listForm1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the value from the selected item
string val = listForm1.GetItemText(listForm1.SelectedItem);
// Split the item's value to a string array according to the pipe char
string[] valArray = val.Split('|');
int sum = 0;
int scores = 0;
// Iterate through all possible values and sum it up,
// while keeping count to how many numbers there are:
for (int i = 1; i < valArray.Length; i++)
{
int num = Convert.ToInt32(valArray[i]);
sum += num;
scores++;
}
// Calculate the average.
// Keep in mind using an integer will create a whole number, without decimal points.
int average = sum / scores;
// Place the average and the sum in textboxes
txtAverage.Text = average.ToString();
txtTotal.Text = sum.ToString();
txtCount.Text = scores.ToString();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다