0

How can I convert teile[4] into a Color in this code?

       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);
            }
        }
    }

its always white if i load something

Darren
  • 68,902
  • 24
  • 138
  • 144
  • This question needs a lot of simplifying. It sounds like it should really be along the lines of "how do I create a `Color` from a name in a `string`?" – Cory Nelson Sep 07 '13 at 23:04
  • y well ... i ve a libary ,,book,, and books,, i create 1 book and add it to the arraylist in books ... one book is: public book(i. isdn, s. title, s. autor, Color col) so i save it as i wrote it in the thread ... and i wanna load it and add it to the arraylist so i need to create a book with the isdn autor and title + color and theres the point how can i get the color in it – Ramon Hämmerle Sep 07 '13 at 23:19
  • Have you looked in the file to check that `b.Farbe` is being written as the name of the colour? – Andrew Morton Sep 07 '13 at 23:34
  • jep it is :) its also giving me ,,black" if im doing it with pause + f11 but showing it at the end with ,,white" – Ramon Hämmerle Sep 07 '13 at 23:55

2 Answers2

2

You can use Color.FromName.

string color = teile[4];
Color col = Color.FromName(color);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Darren
  • 68,902
  • 24
  • 138
  • 144
  • @RamonHämmerle - `teile[4]` must be "White". You'll have to change the string in the file to a different colour. – Darren Sep 07 '13 at 23:32
  • Well i safed it with ,,black" so ,,teile[3]" (its actuelly) it Black ... and if im doing it with F11 (Pause) .. its allready writing in col black ... idk whats the problem – Ramon Hämmerle Sep 07 '13 at 23:40
0

You can use some of the answers in this question: Best solution for XmlSerializer and System.Drawing.Color

They are using XmlSerializer, but the idea is the same convert a System.Drawing.Color to and from a System.String.

What I would do in that case:

//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);
Community
  • 1
  • 1
Jason
  • 1,385
  • 9
  • 11
  • i think its the same which Darren davies wrote isn t it ? – Ramon Hämmerle Sep 07 '13 at 23:33
  • Not exactly the same. That function (Color.FromName) assumes the colors are in a specific language / format. I didn't know if we could assume that. It is possible you have to create a dictionary custom to your situation. For example, your strings might say "blk" to represent black, there is nothing wrong with that and no need to change the file, but the Color.FromName doesn't know the BLK means KnownColor.Black. – Jason Sep 07 '13 at 23:47
  • I added a better example. Does that help explain what I mean? – Jason Sep 07 '13 at 23:59
  • "Color [Red]" hes writing that in the variable ... if im doing it with pause .. F11... – Ramon Hämmerle Sep 08 '13 at 00:08
  • do you might have skype ? would be a lot easier ... and i could show you my screen .. – Ramon Hämmerle Sep 08 '13 at 00:09
  • I understand, the Save function is saving the contents of ToString on the Color class which varies depending on if the color is known / not known, etc. You could just save the color with b.Farbe.ToArgb() and then restore it with Color.FromArgb() this way the whole named color mess it gone and you are just writing a number to the file. Will that work? – Jason Sep 08 '13 at 00:17