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

少し変わったコードがたくさんあります!設定で取得しようとしているのは、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でもあります.コレクションは IEnumerator を返さないので、それもラップする必要があります - なんて奇妙なクラスでしょう!)

于 2013-05-15T10:10:31.523 に答える
0

すぐにこれをテストする時間はありませんが、一見するpreviousLocatione.Locationbook1_MouseDown. これは、コントロールの位置ではなく、マウスの位置でしょうか?

コントロールの場所を に保存しているように見えますが、StringCollectionいくつかの寸法があると思われるため、 が起動されたときにマウスがコントロールの左上隅にない場合がありMouseDownます。

コントロールの以前の位置を追跡するのではsenderなく、位置を取得することを検討することをお勧めします。e

于 2013-03-19T17:11:04.057 に答える