即使在 C# 中关闭表单后仍保留 ListBox 项

Retaining ListBox Items even after form is closed in C#

我有一个列表框,我通过单击一个按钮从文本框中添加项目。我想要的是,即使我每次打开表单时都关闭表单,也应该显示列表中存在或保存在列表框中的列表。

当表单为 closed.The 列表框是另一种表单并且在第一个表单中单击按钮打开第二个表单时,我无法在列表框中显示列表。请帮助我如何在列表框中显示项目或保留保存的值,即使表单是 closed.The 代码附在下面:-

第二表格代码:-

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

    }      

第一种形式代码:-

  private void bn_ManageProfile_Click(object sender, EventArgs e)
    {
        ProfileManager ProfileManager = new ProfileManager(cmb_drive.GetItemText(cmb_drive.SelectedItem) + @"FilePath");
        ProfileManager.ShowDialog();



    }

如果你想永久存储这些项目,你应该使用数据库。 或者您可以做的只是保留一个列表来保存您添加的所有项目,并在打开表单时将这些项目添加到您的列表框中。

当表单关闭时,将列表框中的所有项目添加到新创建的集合中,比如 MyList。

打开表单时,将 MyList 中的每个项目添加到列表框项目中。

您可以使用 Properties 在表单之间传递列表值。

我没有编译以下代码,所以请小心,但它应该为您指明了正确的方向。

假设 Form1 是 parent:

在 Form1 中,创建一个静态 object 来保存值

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

Form2中,设置Form1可以访问的一些属性

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

您的 Form2 方法应更改为使用您刚刚创建的 Field

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

Form2 中,当您更改 ListBox 中的值时,请务必更新 theListBoxItems 列表。也许你可以在 Form2.

Form_Closing 事件中做点什么
theListBoxItems.Add("My Value");

Form1 中,通过将列表项传递给它来调用您的 Form2

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;
    }
}

现在列表自动在表单之间传递。

我希望这是有道理的。

Form 1:- 
             private void bn_ManageProfile_Click(object sender, EventArgs e)
            {
        ProfileManager ProfileManager = new ProfileManager(cmb_drive.GetItemText(cmb_drive.SelectedItem) +@"FolderPath");

           ProfileManager.ShowDialog();
          }

表格 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]);


        } 
     }

我创建了一个函数来帮助我加载列表框的值,即使在关闭表单后也是如此,这正好解决了我的问题。