简体   繁体   English

如何从保存在设置中的stringCollection中删除字符串

[英]How delete string from stringCollection saved in settings

I'm new in this community. 我是这个社区的新手。 I've a problem: I follewed this How I can save controls created in run time in Windows Forms and the code work very well, but I've a problem when I want to delete a string from the stringCollection. 我有一个问题:我对如何在Windows窗体中保存在运行时创建的控件和代码的工作方式非常满意,但是当我想从stringCollection中删除字符串时遇到了问题。 I used the method stringcollection.Remove("string") inserting a valid string just stored and I've also save all with settings.default.save() but the string is not delete from string collection. 我使用了方法stringcollection.Remove(“ string”)插入刚刚存储的有效字符串,并且还使用settings.default.save()保存了所有字符串,但是该字符串并未从字符串集合中删除。 Why dont it work? 为什么不起作用? Please someone help me! 请有人帮我! :) :)

THIS IS MY CODE: 这是我的代码:

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

I don't have time to test this at the minute, but at a quick glance it looks like you're getting your previousLocation from e.Location in your book1_MouseDown . 我暂时没有时间进行测试,但快速浏览一下,就好像您是从e.Location中的book1_MouseDown获取previousLocatione.Location book1_MouseDown This would be the mouse location, not the location of the control? 这将是鼠标的位置,而不是控件的位置?

It looks to me like you're storing the location of the control in your StringCollection , and I assume it has some dimensions, so the mouse may not be at the top left corner of the control when MouseDown is fired. 在我看来,您好像将控件的位置存储在StringCollection ,并且我假设它具有某些尺寸,因此在激发MouseDown时,鼠标可能不在控件的左上角。

I'd suggest looking at getting the location from sender instead of e to keep track of the control's previous location. 我建议您考虑从sender那里获取位置,而不是从e处获取该控件的先前位置。

There's a lot of slightly odd code there! 那里有很多有点奇怪的代码! I think what you're trying to get at with the settings thing is that there is some slightly wierd behaviour when saving StringCollections to settings. 我认为您要通过设置来解决的问题是,将StringCollections保存到设置时会有一些稍微有些奇怪的行为。 Despite the fact that you can add/remove things to the collection and then call save, that wont actually do anything. 尽管您可以在集合中添加/删除内容,然后调用save,但实际上并不会做任何事情。

You can see that the settings object has the correct elements in the property but it doesnt do work. 您可以看到settings对象在属性中具有正确的元素,但是它不起作用。 You also have to re-set the Property eg: 您还必须重新设置属性,例如:

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();
    }
}

There's some slight complications to the code that I've omitted - ACollectionOfStrings is a little wrapper class that basically passes through calls to the StringCollection but is also an IEnumerable (slightly baffled that this isn't the case and also that the GetEnumerator call on String collection doesnt return an IEnumerator so you also have to wrap that aswell - what a wierd class!) 我略过的代码有些复杂-ACollectionOfStrings是一个小的包装器类,基本上通过对StringCollection的调用传递,但也是IEnumerable(有点困惑,事实并非如此,而且对String的GetEnumerator调用集合不会返回IEnumerator,因此您还必须将其包装-多么奇怪的类!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM