首页 > 解决方案 > 如何将数据存储在数组的单行上?

问题描述

我需要能够将数据存储在数组中以显示在列表框中,到目前为止我只能获取文本文件的第一个单词(名称)。

我一直在使用 Line Split 方法,虽然这会将所有部分拆分为自己的行,但执行此方法也会带来“索引超出数组边界”。请帮忙,我已经被困在这整整两天了。

// - - - - - - - Selection Screen - - - - - - -
public SelectionScreen()
{
    InitializeComponent();

    //Loading from text file.
    string[] PrimaryArray = new string[12];
    PrimaryArray = System.IO.File.ReadAllLines(@"Test1.txt");

    foreach (string line in PrimaryArray)
    {
        string[] Parts = line.Split(',');

        CharStats temp = new CharStats();
        temp.Name = Parts[0];
        temp.Description = Parts[1];
        temp.Notes = Parts[2];
        temp.MaxHP = Convert.ToInt16(Parts[3]);
        temp.MaxMP = Convert.ToInt32(Parts[4]);
        temp.Atk = Convert.ToInt32(Parts[5]);
        temp.Def = Convert.ToInt32(Parts[6]);
        temp.Mat = Convert.ToInt32(Parts[7]);
        temp.Mdf = Convert.ToInt32(Parts[8]);
        temp.Agi = Convert.ToInt16(Parts[9]);
        temp.Luk = Convert.ToInt16(Parts[10]);
        temp.Lvl = Convert.ToInt16(Parts[11]);

        ListboxCharacterList.Items.Add(temp.Name);
    }
}

// - - - - - - - Writing stats - - - - - - -
public string WriteStats()
{
    return Environment.NewLine + Name + "," + Description + "," + Notes + "," + 
        MaxHP + "," + MaxMP + "," + Atk + "," + Def + "," + Mat + "," + 
        Mdf + "," + Agi + "," + Luk + "," + Lvl;
}

// - - - - - - - TextFile - - - - - - -
//Mike,Desc.,Notes,1000,500,6,7,3,2,6,9

标签: c#arraystext

解决方案


AListBox可以包含 any 的任何实例class。你可以添加你CharStats的。您只需覆盖ToString()执行哪个以显示框中的值。如果你想选择SelectedItem(s) 你必须投它CharStats

CsvHelper使用( NuGet-package)可以轻松读取 Csv 文件。这里是一个例子:

public class CharStats
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return this.FirstName + " " + this.LastName;
    }
}

public Form1()
{
    InitializeComponent();

    // From File:
    // FirstName;LastName;Age
    // d;D;4
    // e;E;5
    using (StreamReader sr = new StreamReader(@"C:\Temp\myfile.csv"))
    {
        using (CsvHelper.CsvReader csvReader = new CsvHelper.CsvReader(sr))
        {
            IEnumerable<CharStats> statList = csvReader.GetRecords(typeof(CharStats)).Cast<CharStats>();

            foreach (CharStats cs in statList)
            {
                this.listBox1.Items.Add(cs);
            }
        }
    }

    this.listBox1.Items.Add(new CharStats() { FirstName = "a", LastName = "A", Age = 1 });
    this.listBox1.Items.Add(new CharStats() { FirstName = "b", LastName = "B", Age = 2 });
    this.listBox1.Items.Add(new CharStats() { FirstName = "c", LastName = "C", Age = 3 });

    this.listBox1.SelectedIndex = 1;

    var item = this.listBox1.SelectedItem;

    CharStats selected = item as CharStats;

    if (selected != null)
        MessageBox.Show("Worked. We selected: " + selected.FirstName + " " +     selected.LastName + " (" + selected.Age + ")");
}

这将显示一个这样的 ListBox:

一个

乙乙

c C

并且选择将是正确的对象。将MessageBox显示

b B (2)


推荐阅读