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

reko22

这就是我的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");


            }

        }


    }
}
闲置心智

When reading the file, you have listBox1.Items.Add(StdScore.content);. This simply adds a string to the ListBox. You should create instances of StdScore INSIDE the while loop and add those instances directly to the ListBox. It's a very bad practice to name your variable the exact same as the class itself. I would expect to see something more like StdScore curScore = new StdScore();. Now you can use curScore and it's clear this is an instance of the class and not the class itself, and you're not trying to access a static member. For class StdScore, you can override the ToString() method to control what is displayed in the ListBox.

So here is StdScore with the ToString() override:

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;
    }

}

Next, reading the file:

while (!sr.EndOfStream)
{
    StdScore curScore = new StdScore();
    curScore.content = sr.ReadLine();
    curScore.xx = curScore.content.Split(' ');
    listBox1.Items.Add(curScore);
}

button2如果您想将成功的学生转移哪里listBox2在这里,您需要对以下所有存储StdScore实例进行ITERATE listBox1

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);
        }
    }                  
}

请注意,从面向对象编程的角度来看,这些代码都不正确。该代码可以大大改善。我只是简单地采用了您现有的代码,然后将其更改为“使其运行”……至少我认为会这样做。如果没有,它应该给您一些在哪里进行更改的好主意。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C# 在 ListBox1 中搜索项目

将多个项目从 ListBox1 添加到绑定多个项目 ListBox2

如何检查 listbox2 是否包含一些 listbox1 项目 - VB.net

使用SQL查询从listbox1循环到listbox1到listbox2?可能吗?

为什么@UiFactory ListBox makeListBox1()方法会影响所有UiBinder ListBoxes(ListBox1,ListBox2,ListBox3等)?

项目没有出现在C#的ListBox中,但在XAML中是

将 ListBoxItems 从 ListBox1 拖放到 ListBox2 及其图像并避免重复 Delphi

如何使用listbox1添加的Listbox2 Texts文本项创建数组列表

比较listbox1项和listbox2项,并删除重复项

Excel VBA根据Listbox2选择填充Listbox1

如何将项目添加到选定的Listbox1项目中的另一个Listbox2项目中?

从 listBox c# 中获取随机项目

为每个项目XAML C#创建Listbox.ItemTemplate

如何在ListBox C#中检索每个项目

foreach 某些项目在 ListBox 中 - Winform,C#

设置ListBox项目数据格式c#

将 ListBox1 中的每个 String 转换为 Integer 并添加到 ListBox2:格式不正确的输入字符串

C# listBox selectedValue 保持为空

ListBox在C#中的单独文本

C#异步StreamReader并放入ListBox

c# Listbox_DrawItem 和 listbox_SelectedIndexChanged

c#项目参考

排序C#ListBox项目

C#从另一个类向MainForm ListBox添加项目

为什么我的 C# 项目属性没有目标框架和组合框选项?

为什么我的C#项目文件没有针对源文件的Compile Inlcude元素

C# 项目没有被添加到 List<String>

CMake没有为C#项目设置LangVersion

如何将2D角色(带有动画)移动到C#中的鼠标单击位置?