I have a listbox in which i am adding items from the textbox by clicking a button. What i want is even when i close the form every time when i open the form the list should be displayed which is present or saved earlier in the listbox.
I am unable to display the list in the listbox when the form is closed.The listbox is in another form and the 2nd form is opened on a click of a button in the 1st form. Please, help me how can i display items in the listbox or retain the saved value even after form is closed.The code is attached below:-
2nd Form Code :-
private void bn_CreateProfile_Click(object sender, EventArgs e)
{
txt_ProfileName.Enabled = true;
bn_CreateProfile.Text = "Add Profile";
if (txt_ProfileName.Text == "")
{
lb_ProfileList.Items.Clear();
}
else
{
lb_ProfileList.Items.Add(txt_ProfileName.Text);
}
}
private void lb_ProfileList_SelectedIndexChanged_1(object sender, EventArgs e)
{
String[] items = lb_AllProjects.CheckedItems.Cast<String>().ToArray<String>();
foreach (var item in items)
{
for (int d = 0; d < lb_AllProjects.SelectedItems.Count; d++)
{
lb_SelectedProjects.Items.Add(item);
lb_AllProjects.Items.Remove(item);
}
}
}
private void bn_SaveProfile_Click(object sender, EventArgs e)
{
const string spath = "ProfileList.txt";
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(spath,true);
foreach (var profileitem in lb_ProfileList.Items)
{
SaveFile.Write(profileitem + " ");
foreach(var selecteditems in lb_SelectedProjects.Items)
{
SaveFile.Write("#" + " " + selecteditems);
}
SaveFile.WriteLine("\n");
}
SaveFile.Close();
MessageBox.Show("Profile Saved");
}
1st Form Code:-
private void bn_ManageProfile_Click(object sender, EventArgs e)
{
ProfileManager ProfileManager = new ProfileManager(cmb_drive.GetItemText(cmb_drive.SelectedItem) + @"FilePath");
ProfileManager.ShowDialog();
}