2013-03-19 35 views
1

我是這個社區中的新成員。 我遇到了一個問題:我將這個How I can save controls created in run time in Windows Forms和代碼工作得很好,但是我想要從stringCollection中刪除一個字符串時遇到問題。 我用方法stringcollection.Remove(「字符串」)插入一個有效的字符串剛剛存儲,我也保存所有settings.default.save()但字符串不是從字符串集合中刪除。 爲什麼不工作?請有人幫助我! :)如何從stringCollection中刪除字符串保存在設置中

這是我的代碼:

public Form1() 
{ 
    InitializeComponent(); 


    if (Properties.Settings.Default.StringCollection == null) 
     Properties.Settings.Default.StringCollection = new System.Collections.Specialized.StringCollection(); 

} 


private void make_BookButtonAndStore(int x, int y, string name) 
{ 
    make_Book(x, y, name); 
    Properties.Settings.Default.StringCollection.Add(String.Format("{0};{1};{2}", book1.Location.X, book1.Location.Y, book1.Name)); 
    Properties.Settings.Default.Save(); 
} 

private void make_Book(int x, int y, string name) 
{ 
    // this code is initializing the book(button) 
    book1 = new Button(); 
    //Image img = button1.Image; 
    //book1.Image = img; 
    book1.Name = name; 
    //book1.Height = img.Height; 
    //book1.Width = img.Width; 
    book1.Location = new Point(44 + x, 19 + y); 
    book1.MouseDown += new MouseEventHandler(book1_MouseDown); 
    book1.MouseMove += new MouseEventHandler(book1_MouseMove); 
    book1.MouseUp += new MouseEventHandler(book1_MouseUp); 
    groupBox1.Controls.Add(book1); 
} 



void book1_MouseDown(object sender, MouseEventArgs e) 
{ 
    activeControl = sender as Control; 
    previousLocation = e.Location; 
    Cursor = Cursors.Hand; 
} 

void book1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (activeControl == null || activeControl != sender) 
     return; 

    var location = activeControl.Location; 
    location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y); 
    activeControl.Location = location; 


} 

void book1_MouseUp(object sender, MouseEventArgs e) 
{ 
    activeControl = null; 
    Cursor = Cursors.Default; 

    Button btnPremuto = (Button)sender; 
       Properties.Settings.Default.StringCollection.Remove(previousLocation.X+";"+previousLocation.Y+";"+btnPremuto.Name); 
    Properties.Settings.Default.StringCollection.Add(String.Format("{0};{1};{2}", btnPremuto.Location.X, btnPremuto.Location.Y, btnPremuto.Name)); 
    Properties.Settings.Default.Save(); 

} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    foreach (string line in Properties.Settings.Default.StringCollection) 
    { 
     if (!String.IsNullOrWhiteSpace(line)) 
     { 
      // The line will be in format x;y;name 
      string[] parts = line.Split(';'); 
      if (parts.Length >= 3) 
      { 
       int x = Convert.ToInt32(parts[0]); 
       int y = Convert.ToInt32(parts[1]); 

       make_Book(x, y, parts[2]); 
      } 
     } 
    } 
} 
+0

我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-03-19 16:58:47

+0

你在Designer中創建了什麼類型..?在設計器中創建它,然後將值保留爲stringCollection並避免執行此操作'Properties.Settings.Default.StringCollection = new System.Collections.Specialized.StringCollection();'創建'new'實例不會保存值 – MethodMan 2013-03-19 17:01:06

回答

0

我沒有時間在一分鐘來測試這一點,但在快速瀏覽,它看起來像你得到你的previousLocatione.Locationbook1_MouseDown 。這將是鼠標位置,而不是控件的位置?

它在我看來像你存儲控件的位置在你的StringCollection,我認爲它有一些尺寸,所以當MouseDown被觸發時,鼠標可能不在控件的左上角。

我建議看看從sender獲取位置,而不是e以跟蹤控件的先前位置。

0

那裏有很多有點奇怪的代碼!我認爲你想要設置的東西是,在將StringCollections保存到設置時存在一些稍微更奇怪的行爲。儘管事實上你可以添加/刪除集合中的東西,然後調用保存,那實際上不會做任何事情。

你可以看到設置對象在屬性中有正確的元素,但它不起作用。您也可以重新設置的屬性如:

public class Config 
{ 
    private readonly Settings _settings; 
    private readonly ACollectionOfStrings _selectedCategories; 

    internal Config(Settings settings) 
    { 
     _settings = settings; 
     if(_settings.SelectedCategories == null) 
      _settings.SelectedCategories = new StringCollection(); 

     _selectedCategories = new ACollectionOfStrings(_settings.SelectedCategories); 
    } 

    public IEnumerable<string> SelectedCategories 
    { 
     get { return _selectedCategories; } 
    } 

    private void ModifySettings(Action<Settings> modification) 
    { 
     modification(_settings); 
     Save(); 
    } 

    private void Save() 
    { 
     _settings.Save(); 
    } 

    public void AddCategory(string category) 
    { 
     _selectedCategories.Add(category); 
     SaveList(); 
    } 

    private void SaveList() 
    { 
     ModifySettings(s => s.SelectedCategories = _selectedCategories.List); 
    } 

    public void Remove(string category) 
    { 
     _selectedCategories.Remove(category); 
     SaveList(); 
    } 
} 

有一些輕微的併發症,我已經省略了碼 - ACollectionOfStrings是,基本上通過電話向StringCollection,但也是一個IEnumerable一個小包裝類(有點困惑,情況並非如此,並且String集合上的GetEnumerator調用不會返回IEnumerator,因此您還必須包裝它以及一個奇怪的類!)