2013-03-19 64 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,因此您还必须包装它以及一个奇怪的类!)