c#-我的项目没有从listbox1移动到listbox2
这就是我的GUI的样子 我在为大学做一个项目 它所做的是读取一个文本文件 把它放在列表框1中 然后,第二个按钮应显示成功访问listbox2的学生 但每当我按下按钮,我就会出错 我试着到处找,但找不到问题 它只是没有无缘无故地将其写入listbox2 有人知道我该怎么做吗 我该怎么做才能让它工作c#-我的项目没有从listbox1移动到listbox2,c#,listboxitem,C#,Listboxitem,这就是我的GUI的样子 我在为大学做一个项目 它所做的是读取一个文本文件 把它放在列表框1中 然后,第二个按钮应显示成功访问listbox2的学生 但每当我按下按钮,我就会出错 我试着到处找,但找不到问题 它只是没有无缘无故地将其写入listbox2 有人知道我该怎么做吗 我该怎么做才能让它工作 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace second_Project
{
public partial class FSS : Form
{
private void FSS_Load(object sender, EventArgs e)
{
}
public FSS()
{
InitializeComponent();
}
public class StdScore
{
public int id;
public string name;
public double xam, Score, Pract;
public string content;
public string[] xx;
}
OpenFileDialog ofd = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{
StdScore StdScore = new StdScore();
ofd.Filter = "TXT|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(ofd.FileName);
while (!sr.EndOfStream)
{
StdScore.content = sr.ReadLine();
string[] info = StdScore.content.Split(' ');
StdScore.xx = new string[info.Length];
listBox1.Items.Add(StdScore.content);
}
sr.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
StdScore StdScore = new StdScore();
StdScore.xam = 0;
StdScore.Pract = 0;
StdScore.Score = 0;
StdScore.xam += int.Parse(StdScore.xx[2]);
StdScore.Pract += int.Parse(StdScore.xx[3]);
StdScore.Score = StdScore.xam * 0.7 + StdScore.Pract * 0.3;
if (StdScore.xam >= 40 && StdScore.Pract >= 40 && StdScore.Score >= 60)
{
StdScore.xam = (StdScore.xam * 70) / 100;
StdScore.Pract = (StdScore.Pract * 30) / 100;
StdScore.Score = StdScore.xam + StdScore.Pract;
listBox2.Items.Add(StdScore.xx[0] + " " + StdScore.xx[1] + " " + StdScore.xx[2] + " " + StdScore.xx[3] + " " + StdScore.Score + " " + "Succes");
}
}
}
}
读取文件时,您有
listBox1.Items.Add(StdScore.content)代码>。这只是向列表框添加一个字符串。您应该在while循环中创建StdScore
的实例,并将这些实例直接添加到列表框中。将变量命名为与类本身完全相同的名称是一种非常糟糕的做法。我希望看到更像StdScore curScore=newstdscore()代码>。现在,您可以使用curScore
,很明显,这是类的实例,而不是类本身,并且您不会试图访问静态成员。对于类StdScore
,可以重写ToString()
方法来控制列表框中显示的内容
下面是带有ToString()
覆盖的StdScore
:
public class StdScore
{
public int id;
public string name;
public double xam, Score, Pract;
public string content;
public string[] xx;
public override string ToString()
{
return content;
}
}
接下来,读取文件:
while (!sr.EndOfStream)
{
StdScore curScore = new StdScore();
curScore.content = sr.ReadLine();
curScore.xx = curScore.content.Split(' ');
listBox1.Items.Add(curScore);
}
转到按钮2
,您希望将成功的学生移至列表框2
。在这里,您需要迭代列表框1
中存储的所有StdScore
实例:
private void button2_Click(object sender, EventArgs e)
{
foreach(StdScore curScore in listBox1.Items)
{
curScore.xam = int.Parse(curScore.xx[2]);
curScore.Pract = int.Parse(curScore.xx[3]);
curScore.Score = curScore.xam * 0.7 + curScore.Pract * 0.3;
if (curScore.xam >= 40 && curScore.Pract >= 40 && curScore.Score >= 60)
{
curScore.xam = (curScore.xam * 70) / 100;
curScore.Pract = (curScore.Pract * 30) / 100;
curScore.Score = curScore.xam + curScore.Pract;
string success = curScore.xx[0] + " " + curScore.xx[1] + " " +
curScore.xx[2] + " " + curScore.xx[3] + " " + curScore.Score + " Success";
listBox2.Items.Add(success);
}
}
}
请注意,从面向对象编程的角度来看,这些代码都不正确。代码可以大大改进。我只是把你现有的代码改成“让它工作”…至少我认为它会;如果没有,它应该会给你一些关于在哪里进行更改的好主意。错误信息是什么?@Terry Tyson太高了,我的朋友,我怎么能把它放在这里?编辑问题并只发布相关部分?这都是相关的,因为我肯定某个地方出了问题,如果你读了我在上面写的内容,你就会知道我试图让项目进入listbox2,我写了代码,但它不起作用,这就是我共享代码和GUI的原因