Search code examples
c#listboxitem

c# - my items are not moving from listbox1 to listbox2


this is how my GUI looks like

enter image description here

I was working on a project for uni

what it does is reading a text-file

put it in listbox1

then the second button should take the students who succeed to listbox2

but whenever I press the button I get an error

I tried rly hard to search everywhere, but couldn't find the problem

it's just not writing it in listbox2 for no reason

anyone know what should I do?

what do I do to make it work???

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


            }

        }


    }
}

Solution

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

    On to button2 where you want to move students who succeed over to listBox2. Here you need to ITERATE over all the stored StdScore instances that are within 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);
            }
        }                  
    }
    

    Note that from an Object Oriented Programming perspective, none of this code is correct. The code can be much improved. I simply took your existing code and changed it to "make it work"...at least I think it will; if not, it should give you some good ideas on where to make changes.