Bassicaly我一直坚持通过列表框按降序显示高分(如500比1)。这是我的代码,请记住,label1是游戏中的得分,所以如果有人可以帮我吗?
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
label1.Text = Form2.passingText;
StreamWriter q = new StreamWriter("C:\\Users\\BS\\Desktop\\tex.txt", true);
q.WriteLine(label1.Text);
q.Close();
StreamReader sr = new StreamReader("C:\\Users\\BS\\Desktop\\tex.txt");
string g = sr.ReadLine();
while (g != null)
{
listBox1.Items.Add(g);
g = sr.ReadLine();
}
sr.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
答案 0 :(得分:0)
您可以将文件作为行列表读取,然后使用Linq对其进行排序 因此,请尝试以下操作:
,而不是使用SteamReader
using System.Linq;
//....
List<string> hiscores = File.ReadAllLines("C:\\Users\\BS\\Desktop\\tex.txt").ToList();
hiscores.Sort();
foreach (string s in hiscores)
listBox1.Items.Add(s);
编辑: 既然你必须使用StreamReader,这就是这种方法(但原理是相同的):
List<string> hiscores = new List<string>();
StreamReader sr = new StreamReader("C:\\Users\\BS\\Desktop\\tex.txt");
string g = sr.ReadLine();
while (g != null)
{
hiscores.Add(g);
g = sr.ReadLine();
}
sr.Close();
hiscores.Sort();
hiscores.Reverse();
//alternatively, instead of Sort and then reverse, you can do
//hiscores.OrderByDescending(x => x);
foreach(string s in hiscores)
{
listBox1.Items.Add(s);
}