2014-01-29 64 views
2

我有一個提示屏幕,它在程序的乞求中彈出並要求用戶選擇他們想要更新的項目。在清單框中有5個項目。我想要默認選擇數據庫和CGM選項。我現在擁有的方式是檢查checlistbox中的所有項目,然後將它們設置爲未選中狀態。我如何解決這個問題,以便默認選擇CGM和數據庫?如何在默認情況下勾選兩個項目在C#清單框中

public partial class PromptScreen : Form 
{   
    public PromptScreen() 
    { 
     InitializeComponent(); 
     this.Icon = Properties.Resources.TDXm; 
     for (int i = 0; i < cLbFiles.Items.Count; i++) 
      dictionary.Add(cLbFiles.Items[i].ToString(), CheckState.Unchecked); 
    } 
    private void clbFiles_ItemCheck(object sender, ItemCheckEventArgs e) 
    { 
     foreach (KeyValuePair<string, CheckState> kvp in dictionary) 
     { 
      if (kvp.Key == cLbFiles.Items[e.Index].ToString()) 
      { 
       dictionary[kvp.Key] = e.NewValue; 
       if (kvp.Key == "Component Views") 
       { 
        if (kvp.Value == CheckState.Unchecked) 
         MessageBox.Show("Updating Component Views! This might take up to 5 minutes", "Wait Warning", 
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       } 
       break; 
      } 
     } 
    } 

    private void btnCGMDB_Click(object sender, EventArgs e) 
    { 
     for (int i = 0; i < cLbFiles.Items.Count; i++) 
     { 
      if (cLbFiles.Items[i].ToString() == "CGM's" || cLbFiles.Items[i].ToString() == "Database") 
       cLbFiles.SetItemChecked(i, true); 
     } 
     btnUpdate.PerformClick(); 
    } 
} 

回答

1

好像你只是做它在構造函數中:

public PromptScreen() 
{ 
    InitializeComponent(); 
    this.Icon = Properties.Resources.TDXm; 

    string[] checkByDefault = new[] { "CGM's", "Database" }; 
    for (int i = 0; i < cLbFiles.Items.Count; i++) 
    { 
     string itemString = cLbFiles.Items[i].ToString(); 
     dictionary.Add(itemString, checkByDefault.Contains(itemString) ? CheckState.Checked : CheckState.Unchecked); 
} 
+0

這個方法我試過,但默認情況下不檢查。 –

相關問題