2015-06-25 22 views
0

我有一個列表框,我在其中通過單擊按鈕添加項目。我想要的是甚至當我每次打開窗體時關閉表單,應該顯示列表中顯示的列表,或者在列表框中保存。即使在C#中關閉窗體後保留列表框項目

當窗體關閉時,我無法在列表框中顯示列表。列表框以另一種形式顯示,並且在第一個窗體中單擊按鈕時打開第二窗體。請幫助我,我怎麼能顯示在列表框中的項目或保留保存的值即使形式是閉上的代碼附加如下: -

第二表格代號: -

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

    }  

1表格代號: -

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



    } 
+0

您每次創建一個新窗口,創建一個實例並在需要時顯示它,以便其中的數據保持不變。 – dotctor

+0

不要使用'ListBox'來保存數據,擁有持久的數據存儲,同時在創建窗口時從數據中重新填充數據。 – Sinatr

+0

如何,你可以幫我用上面的代碼意味着我應該改變上面的代碼? @doctctor – tdesai

回答

0
Form 1:- 
      private void bn_ManageProfile_Click(object sender, EventArgs e) 
      { 
     ProfileManager ProfileManager = new ProfileManager(cmb_drive.GetItemText(cmb_drive.SelectedItem) [email protected]"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]); 


     } 
    } 

我做了一個函數來幫我加載列表框值的形式被關閉後與剛剛解決了我的問題。

0

如果要永久存儲這些項目,應該使用數據庫。 或者你可以做的只是保持一個列表來保存你添加的所有項目,並在窗體打開時將這些項目添加到你的列表框中。

當窗體關閉時,將所有項目從列表框添加到新創建的集合中,比如MyList。

打開窗體時,將MyList中的每個項目添加到列表框項目中。

+0

我不需要數據庫,但肯定會嘗試圍繞你所說的有關創建列表的第二件事。 – tdesai

+0

@ tdesai是不是覺得? – Joseph

+0

只是嘗試這一點。 – tdesai

0

您可以使用Properties在表單之間傳遞列表值。

我沒有編譯下面的代碼,所以要小心,但它應該指向正確的方向。

假設Form1是父:

在Form1,創建一個靜態對象來保存值

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列表。也許你可以在Form_Closing事件Form2中做點事情。

theListBoxItems.Add("My Value"); 

Form1,通過將列表中的項目,以它

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

現在列表形式之間自動傳遞打電話給你Form2這樣。

我希望這是有道理的。