0

如何在此代码中转换teile[4]为 a ?Color

       public void Save(StreamWriter sw)
    {
        for (int i = 0; i < liste.Count; i++)
        {
            Buch b = (Buch)liste[i];
            if (i == 0)
                sw.WriteLine("ISDN ; Autor ; Titel ; Farbe");
            sw.WriteLine(b.ISDN + ";" + b.Autor + ";" + b.Titel + ";" + b.Farbe);
        }
    }

    public void Load(StreamReader sr)
    {
        int isdnn;
        string autorr;
        string titell;

        Color col;

        sr.ReadLine();

        string line;

        while((line = sr.ReadLine()) != null)
        {
            if (line.Length > 0)
            {
                string[] teile = line.Split(';');
                try
                {
                    isdnn = Convert.ToInt32(teile[0]);
                    autorr = teile[1];
                    titell = teile[2];
                    string color = teile[3];
                    col = Color.FromName(color);
                }
                catch
                {
                    throw new Exception("Nicht geklappt");
                }
                Buch buch = new Buch(isdnn, autorr, titell, col);
                liste.Add(buch);
            }
        }
    }

如果我加载一些东西,它总是白色的

4

2 回答 2

2

您可以使用Color.FromName.

string color = teile[4];
Color col = Color.FromName(color);
于 2013-09-07T22:59:25.383 回答
0

您可以在这个问题中使用一些答案:XmlSerializer 和 System.Drawing.Color 的最佳解决方案

他们正在使用 XmlSerializer,但想法是相同的将 a 转换为 aSystem.Drawing.Color和从 a转换System.String

在这种情况下我会做什么:

//Change your line to this
sw.WriteLine(b.ISDN + ";" + b.Autor + ";" + b.Titel + ";" + System.Drawing.ColorTranslator.ToHtml(b.Farbe));

//And change this line
col = System.Drawing.ColorTranslator.FromHtml(color);
于 2013-09-07T23:14:34.943 回答