c# - 我的项目没有从 listbox1 移动到 listbox2
c# - my items are not moving from listbox1 to listbox2
这是我的 GUI 的样子
我正在为大学做一个项目
它的作用是读取文本文件
放入listbox1
那么第二个按钮应该把成功的同学带到listbox2
但是每当我按下按钮时我都会得到一个错误
我很努力的到处找,也没找到问题所在
就是无缘无故不写在listbox2里
有人知道我该怎么办吗?
我该怎么做才能让它发挥作用???
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);
。这只是将一个字符串添加到 ListBox。您应该在 while 循环内创建 StdScore
的实例,并将这些实例直接添加到列表框。将您的变量命名为与 class 本身完全相同的名称是一种非常糟糕的做法。我希望看到更像 StdScore curScore = new StdScore();
的东西。现在您可以使用 curScore
并且很明显这是 class 的实例而不是 class 本身,并且您不会尝试访问静态成员。对于 class StdScore
,您可以重写 ToString()
方法来控制在 ListBox 中显示的内容。
所以这里是 StdScore
和 ToString()
覆盖:
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);
}
转到 button2
,您想将成功的学生转移到 listBox2
。在这里,您需要迭代 listBox1
:
内所有已存储的 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);
}
}
}
请注意,从 面向对象编程 的角度来看,此代码的 none 是正确的。代码可以大大改进。我只是拿了你现有的代码并将其更改为“让它工作”......至少我认为它会;如果没有,它应该会给你一些关于在哪里进行更改的好主意。
这是我的 GUI 的样子
我正在为大学做一个项目
它的作用是读取文本文件
放入listbox1
那么第二个按钮应该把成功的同学带到listbox2
但是每当我按下按钮时我都会得到一个错误
我很努力的到处找,也没找到问题所在
就是无缘无故不写在listbox2里
有人知道我该怎么办吗?
我该怎么做才能让它发挥作用???
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);
。这只是将一个字符串添加到 ListBox。您应该在 while 循环内创建 StdScore
的实例,并将这些实例直接添加到列表框。将您的变量命名为与 class 本身完全相同的名称是一种非常糟糕的做法。我希望看到更像 StdScore curScore = new StdScore();
的东西。现在您可以使用 curScore
并且很明显这是 class 的实例而不是 class 本身,并且您不会尝试访问静态成员。对于 class StdScore
,您可以重写 ToString()
方法来控制在 ListBox 中显示的内容。
所以这里是 StdScore
和 ToString()
覆盖:
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);
}
转到 button2
,您想将成功的学生转移到 listBox2
。在这里,您需要迭代 listBox1
:
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);
}
}
}
请注意,从 面向对象编程 的角度来看,此代码的 none 是正确的。代码可以大大改进。我只是拿了你现有的代码并将其更改为“让它工作”......至少我认为它会;如果没有,它应该会给你一些关于在哪里进行更改的好主意。