在列表框中选择项目后,需要更新带有小数的3个文本框。
指向文件的链接:https://www.dropbox.com/s/xj2efe5sxsolswk/midterm.zip
列表框中的格式:“名称| XX | | XX | | XX |”例如“马特| 100 | | 90 | | 80 |”
如何从列表框中调出与所选索引关联的3个值,以计算3个值并更新与该列表框选择关联的3个文本框?
我创建了3个列表来尝试存储我需要的内容。我将得分输入保存到scoreList2,但在将它与ListBox关联时不确定如何链接它们。
public static List<string> scoreList = new List<string>();
public static List<decimal> scoreList2 = new List<decimal>();
public static List<object> scoreList3 = new List<object>();
代码:
private void Form1_Load(object sender, EventArgs e)
{
(all hard coded cuz I'm a noob)
//txtScoreTotal.Text = tempNum1.ToString();
//txtScoreTotal.text =
//txtScoreCount.Text = tempNum2.ToString();
//txtAverage.Text = tempNum3.ToString();
lbStudents.Items.Add(tempInfo1 + " " + tempNum1 + " " + tempNum2 + " " + tempNum3);
}
private void lbStudents_SelectedIndexChanged(object sender, EventArgs e)
{
/*
txtScoreTotal.Text = tempNum1.ToString();
//txtScoreTotal.Text = selected listbox scoretotal
txtScoreCount.Text = tempNum2.ToString();
//txtScoreTotal.Text = selected listbox scorecount
txtAverage.Text = tempNum3.ToString();
//txtSCoreTotal.Text = selected listbox average
*/
txtScoreTotal.Text = lbStudents.SelectedItem.ToString();
}
答案 0 :(得分:1)
您可以使用String.Split
方法和LINQ
这样:
private void lbStudents_SelectedIndexChanged(object sender, EventArgs e)
{
if(lbStudents.SelectedItem != null)
{
decimal result;
var numbers = lbStudents.SelectedItem.ToString()
.Split(new [] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => decimal.TryParse(x, out result))
.ToList();
txtBox1.Text = numbers[0];
txtBox2.Text = numbers[1];
txtBox3.Text = numbers[2];
}
}