微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# - 我的项目没有从 listbox1 移动到 listbox2

如何解决c# - 我的项目没有从 listbox1 移动到 listbox2

这就是我的 GUI 的样子

enter image description here

我正在为 uni 做一个项目

它的作用是读取文本文件

放入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");


            }

        }


    }
}

解决方法

读取文件时,您有 if (!this.userForm.valid) { return; } 。这只是向 ListBox 添加一个字符串。您应该在 while 循环内创建 listBox1.Items.Add(StdScore.content); 的实例并将这些实例直接添加到 ListBox。将变量命名为与类本身完全相同的名称是一种非常糟糕的做法。我希望看到更像 StdScore 的东西。现在您可以使用 StdScore curScore = new StdScore(); 并且很明显这是类的实例而不是类本身,并且您没有尝试访问静态成员。对于 curScore 类,您可以覆盖 StdScore 方法来控制 ListBox 中显示的内容。

所以这里是带有 ToString() 覆盖的 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 内所有存储的 StdScore 实例:

listBox1

请注意,从面向对象编程的角度来看,这些代码都不正确。代码可以大大改进。我只是把你现有的代码改成“让它工作”……至少我认为它会;如果没有,它应该会给您一些关于在哪里进行更改的好主意。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。