0

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



    }
tdesai
  • 1
  • 7
  • you are creating a new window every time, create an instance and show it when you want so the data in it will remain. – Hamid Pourjam Jun 25 '15 at 08:56
  • Do not use `ListBox` to hold data, have persistent data storage, while repopulating data from it upon window creation. – Sinatr Jun 25 '15 at 08:57
  • How, can you help me with the code above means what should i change in the above code ?? @doctctor – tdesai Jun 25 '15 at 08:58
  • I am not using Listbox to hold data the data from the listbox is stored in the file and when i close the 2nd form and i try to open it again later the listbox items is erased. I want it to stay as it is when we open the form again. @Sinatr – tdesai Jun 25 '15 at 09:01
  • add a closing & load event handler & save the data when the form closes & reload it when the form loads. (Though I prefer the solution by dotctor) – PaulF Jun 25 '15 at 09:01
  • What should i add in the ManageProfileClick Event button to display all populate the list every time the 2nd form is opened. @PaulF – tdesai Jun 25 '15 at 09:06
  • Here is an example of saving data on form closing http://stackoverflow.com/questions/24861533/how-do-i-save-data-on-form-closing-event-in-winforms. Then you would add a load event handler for the form to restore it. – PaulF Jun 25 '15 at 09:11

3 Answers3

0

If you want to store those items permanently, you should use database. Or else what you can do is just keep a list to hold all the items you added and add those items to your listbox when the form is opened.

when form closed,add all the items from listbox to the newly created collection say MyList.

When form opened,add each items from MyList to the list box items.

Joseph
  • 1,054
  • 1
  • 11
  • 25
0

You can pass the list values between forms by using Properties.

I haven't compiled the following code so be careful, but it should point you in the right direction.

Assuming Form1 is the parent:

In Form1, create a static object to hold the values

private static List<string> MyListItems = new List<string>();

In Form2, set up some Properties that will be accessible by Form1

private List<string> theListItems;
public List<string> TheListItems
{
    get { return theListItems; }
    set { theListItems = value; }
}

Your Form2 methods should be altered to use the Field you just created

private void lb_ProfileList_SelectedIndexChanged_1(object sender, EventArgs e)
{
    foreach (string item in theListItems)
    {
        for (int d = 0; d < lb_AllProjects.SelectedItems.Count; d++)
        {
            lb_SelectedProjects.Items.Add(item);
            lb_AllProjects.Items.Remove(item);
        }
    }
}

In Form2 when you change the values in the ListBox be sure to update theListBoxItems list. Maybe you could do something in Form_Closing event of Form2.

theListBoxItems.Add("My Value");

In Form1, call your Form2 like this by passing the list items to it

private void bn_ManageProfile_Click(object sender, EventArgs e)
{
    // Create instance of ProfileManager form
    using (ProfileManager MyProfileManager = new ProfileManager(cmb_drive.GetItemText(cmb_drive.SelectedItem) + @"FilePath"))
    {
        // Pass list to form
        MyProfileManager.TheListItems = MyListItems;
        // Show form
        MyProfileManager.ShowDialog();

        // Get value back from form
        MyListItems = MyProfileManager.TheListItems;
    }
}

Now the list is passed between forms automatically.

I hope this makes sense.

Equalsk
  • 7,954
  • 2
  • 41
  • 67
0
Form 1:- 
             private void bn_ManageProfile_Click(object sender, EventArgs e)
            {
        ProfileManager ProfileManager = new ProfileManager(cmb_drive.GetItemText(cmb_drive.SelectedItem) +@"FolderPath");

           ProfileManager.ShowDialog();
          }

Form 2:-

    public ProfileManager(String Path)
    {
        InitializeComponent();
        PopulateListBox(@"C:\Users\ProfileList.txt");

        string[] testedfiles = System.IO.Directory.GetFiles(Path,"*.vcxproj");       // Display the list of .vcxproj projects to Build
        foreach (string file in testedfiles)

            lb_AllProjects.Items.Add(System.IO.Path.GetFileName(file));

    }

     private void PopulateListBox(string path)
     {

        string[] lines = System.IO.File.ReadAllLines(path);

        foreach (string line in lines)
        {
            this.lb_ProfileList.Items.Add(line.Split(' ')[0]);


        } 
     }

I have made a function to help me load the Listbox values even after the form is closed and that just solves my problem.

tdesai
  • 1
  • 7