2

我有一个提示屏幕,在程序的请求中弹出并要求用户选择他们想要更新的项目。清单框中有 5 个项目。我想默认选择数据库和 CGM 选项。我现在拥有它的方式是检查 checlistbox 中的所有项目,然后将它们设置为未选中。我该如何解决这个问题,以便默认选择 CGM 和数据库?

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

1 回答 1

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);
}
于 2014-01-29T21:14:55.663 回答