1

我是这个社区的新手。我有一个问题:我遵循了这个如何保存在 Windows 窗体中运行时创建的控件并且代码运行良好,但是当我想从 stringCollection 中删除一个字符串时我遇到了问题。我使用方法 stringcollection.Remove("string") 插入了一个刚刚存储的有效字符串,并且我还使用 settings.default.save() 保存了所有内容,但该字符串并未从字符串集合中删除。为什么它不起作用?请有人帮助我!:)

这是我的代码:

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]);
            }
        }
    }
}
4

2 回答 2

0

我现在没有时间对此进行测试,但乍一看,您似乎正在previousLocation从. 这将是鼠标位置,而不是控件的位置?e.Locationbook1_MouseDown

在我看来,您将控件的位置存储在您的 中,并且我认为它具有一些尺寸,因此在触发StringCollection时鼠标可能不在控件的左上角。MouseDown

我建议查看从中获取位置,sender而不是e跟踪控件的先前位置。

于 2013-03-19T17:11:04.057 回答
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(有点困惑,事实并非如此,而且 GetEnumerator 调用 String集合不返回 IEnumerator 所以你还必须包装它 - 多么奇怪的类!)

于 2013-05-15T10:10:31.523 回答