2008-12-18 18 views

cevap

4

, combobox yoluyla döngü olur ve bir sınırlandırılmış dizede değerleri kaydetmek: Yukarıdaki kod mükemmel değilse

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

Lütfen bağışlayın, Bu sadece Bir örnek.

Bu yardımcı olur umarım!

8

Ayarlar Tasarımcısı'nın kullanmanıza izin verdiği tek koleksiyon türü System.Collections.ArrayList. Bir ArrayList kullanırsanız, tüm öğelerinin türleri serileştirilebilir olmalıdır ([Serializable] özniteliğine sahip olmalı veya System.Runtime.Serialization.ISisitable'ı uygulamalıdır.)

ArrayList'den veri almak için bazı kodlar (cboCollection olarak adlandırılır) Ayarlar'da bir açılan kutuya ve geri.

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

Windows Forms nesneleri, seri hale getirilemez. Bu nedenle bunları bir binaryformatter ile bir dosyaya serileştiremez ve depolayamazsınız. Combobox değerlerini manuel olarak bir dosyada saklamanız gerekir.

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 
     } 
    } 
İlgili konular