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