C# 在C中关闭表单后仍保留列表框项#

C# 在C中关闭表单后仍保留列表框项#,c#,winforms,listbox,C#,Winforms,Listbox,我有一个列表框,通过单击按钮从文本框中添加项目。我想要的是,即使每次打开表单时都关闭表单,列表框中也会显示之前存在或保存的列表 当表单关闭时,我无法在列表框中显示列表。列表框位于另一个表单中,单击第一个表单中的按钮即可打开第二个表单。请帮助我如何在列表框中显示项目或在表单关闭后保留保存的值。代码附在下面:- 第二表格代码:- private void bn_CreateProfile_Click(object sender, EventArgs e) {

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

当表单关闭时,我无法在列表框中显示列表。列表框位于另一个表单中,单击第一个表单中的按钮即可打开第二个表单。请帮助我如何在列表框中显示项目或在表单关闭后保留保存的值。代码附在下面:-

第二表格代码:-

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

    }      

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

表单关闭后,将listbox中的所有项添加到新创建的集合(例如MyList)


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

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

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

假设
Form1
是父项:

在Form1中,创建一个静态对象来保存这些值

private static List<string> MyListItems = new List<string>();
您的
Form2
方法应该更改为使用您刚刚创建的
字段

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


        } 
     }

我已经做了一个函数来帮助我加载Listbox值,即使在表单关闭之后也是如此,这正好解决了我的问题

您每次都在创建一个新窗口,创建一个实例并在需要时显示它,这样其中的数据将保留下来。不要使用
ListBox
来保存数据,进行持久数据存储,同时在创建窗口时从中重新填充数据。如何使用上面的代码来帮助我意味着我应该在上面的代码中更改什么@doctctorI没有使用Listbox保存数据Listbox中的数据存储在文件中,当我关闭第二个表单并尝试稍后再次打开它时,Listbox项将被删除。我希望当我们再次打开表单时它保持原样@sina添加关闭和加载事件处理程序&在窗体关闭时保存数据,并在窗体加载时重新加载数据。(虽然我更喜欢dotctor的解决方案)我不需要数据库,但一定要围绕您所说的创建列表来尝试第二件事。@tdesai做得好吗?只是尝试一下而已。
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();
          }
    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]);


        } 
     }