2008-12-18 6 views

Odpowiedz

4

Jeśli mówimy o ustawieniach użytkownika aplikacji, ja bym pętlę przez combobox i zapisać wartości w wyznaczonym wyrażenie:

StringBuilder sb = new StringBuilder(); 
foreach(var item in combo.Items){ 
    sb.Append(item.ToString() + ";"); 
} 
Properties.Settings.MyListSetting = sb.ToString(); 

Wybacz jeśli powyższy kod nie jest doskonały, to tylko przykład.

Nadzieję, że pomaga!

8

Jedynym rodzajem zastosowania, którego można użyć w Projektancie ustawień, jest System.Collections.ArrayList. Jeśli używasz ArrayList, wszystkie typy jego elementów muszą być przekształcane do postaci szeregowej (atrybut Serializowalny lub implementacja System.Runtime.Serialization.ISerializable).

Oto kod pobierający dane z tablicy ArrayList (o nazwie cboCollection) w Ustawieniach do pola kombi iz powrotem.

private void Form1_Load(object sender, EventArgs e) 
    { 
     if (Settings.Default.cboCollection != null) 
      this.comboBox1.Items.AddRange(Settings.Default.cboCollection.ToArray()); 
    } 


    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     ArrayList arraylist = new ArrayList(this.comboBox1.Items); 
     Settings.Default.cboCollection = arraylist; 
     Settings.Default.Save(); 
    } 

    //A button to add items to the ComboBox 
    private int i; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     this.comboBox1.Items.Add(i++); 
    } 
1

Obiekty Windows Forms nie podlegają przekształcaniu do postaci szeregowej. Dlatego nie można serializować i przechowywać ich w pliku z binarnym formatowaniem. Musisz przechowywać wartości combobox ręcznie w pliku.

string comboboxFileName = @"c:\workDir\settings.settings"; 

private void saveComboboxInFile (String comboboxFileName) 
{ 
    //-------------------------------------------------------- 
    //- Store the combobox values in a file. 1 value = 1 line 
    //-------------------------------------------------------- 
    try 
    { 
     using (StreamWriter comboboxsw = new StreamWriter(comboboxFileName)) 
     { 
      foreach (var cfgitem in comboBox.Items) 
      { 
       comboboxsw.WriteLine(cfgitem); 
      } 
     } // End Using` 
    } 
    catch (Exception e) 
    { 
     //process exception 
    } 
} 



private void reloadCombboxFromFile (string comboboxFileName) 
    { 
    //------------------------------------------------- 
    //- Read the values back into the combobox 
    //------------------------------------------------- 
     try 
     { 
      using (StreamReader comboboxsr = new StreamReader(comboboxFileName)) 
      { 
       while (!comboboxsr.EndOfStream) 
       { 
         string itemread = comboboxsr.ReadLine(); 
         comboBox.Items.Add(itemread); 
       } 
      } // End Using 
     } 
     catch (DirectoryNotFoundException dnf) 
     { 
     // Exception Processing 
     } 
     catch (FileNotFoundException fnf) 
     { 
     // Exception Processing 
     } 
     catch (Exception e) 
     { 
     // Exception Processing 
     } 
    } 
Powiązane problemy