【问题标题】:sorting a text file and inputting into a listbox c#对文本文件进行排序并输入到列表框 c#
【发布时间】:2021-08-01 18:26:26
【问题描述】:

我正在尝试制作一个排行榜,按每个参与者获得的分数对其进行排序。 每个用户都会说出他们是输了还是输了,然后代码会给出输赢的数字。 然后将其添加到列表框并保存在 .txt 文件中。 我的问题是我想按照他们的得分对排行榜进行排序。 以下是文件中的所有代码,以便您知道我在使用什么

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.IO;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
 public partial class BadForm : Form
 {
    public BadForm()
    {
        InitializeComponent();
    }

    private void BackBut_Click(object sender, EventArgs e)
    {
        this.Hide();
        TeaEveSel f4 = new TeaEveSel();
        f4.Show();
        // This shows the team event selection page when the button is pressed
    }

    private void HomeBut_Click(object sender, EventArgs e)
    {
        this.Hide();
        StartForm f4 = new StartForm();
        f4.Show();
        // This shows the start up page when the button is pressed
    }

    private void TeaSave_Click(object sender, EventArgs e)
    {
        if (ResultsBox.Text == "Win")
        {
            ResultsBox.Text = 5.ToString(); // Whenever win is selected it changes to the number 5 so that calculations can be done
        }
        else if (ResultsBox.Text == "Lose")
        {
            ResultsBox.Text = 1.ToString(); // Whenever lose is selected it changes to the number 1 so that calculations can be done
        }
        else
        {

        }

        listBox1.Items.Add(string.Format("{0} | {1} | {2} | {3}", IndNamBox.Text, TeaNamBox.Text, EveNamBox.Text, ResultsBox.Text));
        listBox1.Items.Add("");
        //This adds the text in the combo boxes and text boxes to the list box



    }

    private void TeaTable_Click(object sender, EventArgs e)
    {
        const string fpath = "G:\\IT\\unit 4\\assignment 2\\WindowsFormsApp1\\Badminton.txt";

        var SaveFile = new System.IO.StreamWriter(fpath);

        foreach (var item in listBox1.Items)
        {
            SaveFile.WriteLine(item.ToString());
        }
        SaveFile.ToString();
        SaveFile.Close();
        //When this button is pressed it updates the txt file with the text in the list box

        BigServer frm2 = new BigServer();
        frm2.Show();
        frm2.Hide();
        //This opens and closes the BigServer form so that the listbox on the BigServer loads the txt file 
    }

    private void BadForm_Load(object sender, EventArgs e)
    {
        var lines = File.ReadAllLines("G:\\IT\\unit 4\\assignment 2\\WindowsFormsApp1\\Badminton.txt");
        listBox1.Items.AddRange(lines);
        //This fills the listbox with the txt file so that all the information is in the file and nothing gets overwritten.
    }

 }
}

我知道有很多,但上下文非常重要。任何帮助都会很棒。如果我可以在保存到 .txt 文件之前对列表框进行排序,那也可以。

【问题讨论】:

  • 排序前需要将文本编号解析为数字。
  • 我在什么时候这样做,你能告诉我排序的代码

标签: c# sorting listbox leaderboard


【解决方案1】:

我认为你需要一门课

    public class User
    {
        public User()
        {

        }

        public User(string yourString)
        {
            var result = yourString.Split('|').ToList();
            if (result.Count == 4)
            {
                Name = result[0];
                Team = result[1];
                Event = result[2];
                Result = int.Parse(result[3]);
            }
        }

        public string Name { get; set; }
        public string Team { get; set; }
        public string Event { get; set; }
        public int Result { get; set; }

        public override string ToString()
        {
            return $"{Name}|{Team}|{Event}|{Result}";
        }

    }

例如

    static void Main(string[] args)
    {
        var testList = new List<string>()
        {
            "a|b|c|5",
            "a1|b1|c1|4",
            "a2|b2|c2|3",
            "a3|b4|c5|1",
            "a5|b5|c5|7",
        };

        var users = testList.Select(m => new User(m));

        users = users.OrderBy(m => m.Result).ToList();
        //or
        //users = users.OrderByDescending(m => m.Result).ToList();

        foreach (var user in users)
        {
            Console.WriteLine(user.ToString());
        }


        Console.ReadLine();

    }

【讨论】:

  • 是否会对我的列表框或文本文件进行排序,因为我不知道将代码放在哪里而不会出错
  • 另外我如何以这种排序顺序将此数据添加到列表框中
【解决方案2】:

试试这个:

            //fill listbox with test data
            var temp = Enumerable.Range(1,100).Select(x => (object)string.Join(" | ", new object[] { "A","B","C",x.ToString()})).ToArray();
            listBox1.Items.AddRange(temp);  

            var sortedRows = listBox1.Items.Cast<string>().OrderBy(x => int.Parse(x.Split(new char[] {'|'}).Last())).ToArray();
            listBox1.Items.Clear();
            listBox1.Items.AddRange(sortedRows);

【讨论】:

  • 我无法清除列表框,因为它需要所有预先存在的数据以及新信息
  • 轻微问题您的代码只会使列表框充满 A¦B¦C¦1、A¦B¦C¦2 等等
  • 代码只是用排序的相同项目替换当前项目。添加新项目后,您必须再次排序。我刚刚创建了数据来填充列表框,只是为了测试。
  • 那么我将如何调整代码以便它输入我的文本文件
  • 你的代码很好。我用你的代码填充列表框,然后用我的新代码排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多