C# 如何从保存在“设置”中的stringCollection中删除字符串
我是这个社区的新手。 我有一个问题:我遵循了这一点,代码工作得很好,但是当我想从stringCollection中删除字符串时,我有一个问题。 我使用了stringcollection.Remove(“string”)方法插入刚存储的有效字符串,并使用settings.default.save()保存了所有内容,但该字符串不是从字符串集合中删除的。 为什么它不起作用?请有人帮帮我!:) 这是我的代码:C# 如何从保存在“设置”中的stringCollection中删除字符串,c#,winforms,settings,draggable,stringcollection,C#,Winforms,Settings,Draggable,Stringcollection,我是这个社区的新手。 我有一个问题:我遵循了这一点,代码工作得很好,但是当我想从stringCollection中删除字符串时,我有一个问题。 我使用了stringcollection.Remove(“string”)方法插入刚存储的有效字符串,并使用settings.default.save()保存了所有内容,但该字符串不是从字符串集合中删除的。 为什么它不起作用?请有人帮帮我!:) 这是我的代码: public Form1() { InitializeComponent();
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]);
}
}
}
}
我现在没有时间测试这一点,但快速一看,您似乎从
book1\u MouseDown
中的e.Location
获取了您的以前的位置。这将是鼠标位置,而不是控件的位置
在我看来,您正在将控件的位置存储在StringCollection
中,并且我假设它有一些维度,因此当触发MouseDown
时,鼠标可能不在控件的左上角
我建议从sender
获取位置,而不是e
,以跟踪控件以前的位置。那里有很多稍微奇怪的代码!我认为您试图通过设置了解的是,在将StringCollections保存到设置时,会出现一些稍微古怪的行为。尽管您可以在集合中添加/删除内容,然后调用save,但这实际上不会起任何作用
您可以看到设置对象在属性中有正确的元素,但它不起作用。您还必须重新设置属性,例如:
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();
}
}
公共类配置
{
私人只读设置\u设置;
私有只读字符串集合(U选择的类别);
内部配置(设置)
{
_设置=设置;
如果(_settings.SelectedCategories==null)
_settings.SelectedCategories=新建StringCollection();
_selectedCategories=新的字符串集合(_settings.selectedCategories);
}
公共IEnumerable SelectedCategories
{
获取{return\u selectedCategories;}
}
私有无效修改设置(操作修改)
{
修改(_设置);
Save();
}
私有void Save()
{
_设置。保存();
}
公共无效添加类别(字符串类别)
{
_选择类别。添加(类别);
存储列表();
}
私有void存储列表()
{
修改设置(s=>s.SelectedCategories=\U SelectedCategories.List);
}
删除公共void(字符串类别)
{
_所选类别。删除(类别);
存储列表();
}
}
我省略的代码有一些细微的复杂之处-acollectionOfstring是一个小包装类,基本上通过对StringCollection的调用传递,但也是一个IEnumerable(有点困惑的是,事实并非如此,而且字符串集合上的GetEnumerator调用没有返回IEnumerator,因此您也必须将其包装起来-这是一个多么复杂的类!)我编辑了您的标题。请参见“”,其中一致意见是“不,他们不应该”。您在设计器中创建的是什么类型的..?在设计器中创建它,然后清空将该类型保留为stringCollection的值。也请避免执行此操作Properties.Settings.Default.stringCollection=new System.Collections.Specialized.stringCollection();
创建一个新的
实例不会保存值。。