[英]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
的實例,並將這些實例直接添加到 ListBox。 將變量命名為與 class 本身完全相同的名稱是一種非常糟糕的做法。 我希望看到更像StdScore curScore = new StdScore();
的東西。 . 現在您可以使用curScore
,很明顯這是 class 的一個實例,而不是 class 本身,您並不是要訪問 static 成員。 對於 class 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);
}
在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);
}
}
}
請注意,從Object 面向編程的角度來看,這段代碼都不正確。 代碼可以大大改進。 我只是拿了你現有的代碼並將其更改為“讓它工作”......至少我認為它會; 如果沒有,它應該會給你一些關於在哪里進行更改的好主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.