vendredi 3 avril 2015

saving arraylist in application settings winforms

I am writing a simple application - a kind of saper game in winforms in C#. My application already saves some information in application settings like the size or colour of buttons , but when i try to save an arraylist of my own structure I get a problem. The are no errors but the information is not saved for the next program execution.Ustawienia is a public static class including wyniki which is another form , and Properties.Settings.Default.scores is an ArrayList added in application settigs. I would be grateful if you have any idea what i am doing wrong and how to store the arraylist in app settings.


Here is the code:


public partial class Form3 : Form {



public Form3()
{
InitializeComponent();
if (Properties.Settings.Default.scores == null)
Properties.Settings.Default.scores = new System.Collections.ArrayList();

}

private void ok_click(object sender, EventArgs e)
{

Highscore higscore = new Highscore(Properties.Settings.Default.ktory, textBox1.Text, ustawienia.ile_wierszy, ustawienia.ile_kolumn, ustawienia.elapsed.Seconds);
Properties.Settings.Default.scores.Add( higscore);

Properties.Settings.Default.scores.Sort(new myComparer());

Properties.Settings.Default.ktory++;
Properties.Settings.Default.Save();
Highscore.show();
this.Close();

}

}
public class Highscore
{
public int nubmer;//w properties ktory=+1;
public string name;
int rows;
int columns;
public int time;
public Highscore(int _number, string _name, int _rows, int _columns, int _time)
{
number = _number;
name = _name;
rows = _rows;
columns = _columns;
time = _time;
}
public static void show()
{
ListView list = (ListView)ustawienia.wyniki.Controls.Find("listView1", true)[0];
list.Items.Clear();
foreach (Highscore e in Properties.Settings.Default.scores)
{

ListViewItem newItem = new ListViewItem(new[] { e.name, e.time.ToString(), e.rows.ToString()+"x"+e.columns.ToString() });
lista.Items.Add(newItem);
}

ustawienia.wyniki.Show();
}
}
public class myComparer:IComparer
{

int IComparer.Compare(Object x, Object y)
{
if (((Highscore)x).time < ((Highscore)y).time)
return 1;
else if (((Highscore)x).time > ((Highscore)y).time)
return -1;
else
{

return String.Compare(((Highscore)x).name,((Highscore)y).name);
}
}
}


}


Aucun commentaire:

Enregistrer un commentaire