簡體   English   中英

如何為 CheckedListBox 中的所有項目創建循環?

[英]How I can create a loop for all items from a CheckedListBox?

我有一個復雜的問題,我發現的一切都無濟於事。 所以我有一個WindowsFormApp ,在Form2我有一個CheckedListBox ,它是這樣的:

|_|Simulink
|_|Aerospace Blockset
|_|Bioinformatics

和許多其他行。

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}

我想,當我檢查此checkBoxes以獲取名稱時: Simulink ,例如,如果選中SimulinkAer...並在文本文件instaler.ini驗證誰在這里:

string installerfilename = Path + "installer.ini";
var link = (Path + "installer.ini").ToString();
var lines = File.ReadAllLines(link);

我有那個名字: #product=之后的Simulink ,像這樣: #product=Simulink並刪除# ,並在我有product=all => #product=all地方添加一個# (這適用於所有情況)。 您可以幫助我為所有checkboxes創建一個循環,因為我所嘗試的只是為checkboxes創建一個事件但是太復雜了,在這種模式下我有很多代碼?

使用 Regex.Replace 和 string.replace 更新您的產品文本文件。

    private void chklbproduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        string installerfilename = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Installer.txt");
        IEnumerable<string> inilines = File.ReadAllLines(installerfilename).AsEnumerable();

        string selectedItem = chklbproduct.SelectedItem.ToString();
        bool IsChecked = chklbproduct.CheckedItems.Contains(selectedItem);

        if (IsChecked)
            inilines = inilines.Select(line => line == string.Format("#product={0}", selectedItem) 
                                               ? line.Replace(line, string.Format("#product={0}", selectedItem), string.Format(@"product={0}", selectedItem))
                                               : line);

        else
            inilines = inilines.Select(line => (line == string.Format("#product={0}", selectedItem) || line == string.Format(@"product={0}", selectedItem)) 
                                               ? line.Replace(line, string.Format(@".*product={0}", selectedItem), string.Format(@"#product={0}", selectedItem)) 
                                               : line);


        if (chklbproduct.CheckedItems.Count == 0)
            inilines = inilines.Select(line => Regex.Replace(line, @".*product=all", @"product=all"));
        else
            inilines = inilines.Select(line => Regex.Replace(line, @".*product=all", @"#product=all"));


        string strWrite = string.Join(Environment.NewLine, inilines.ToArray());
        File.WriteAllText(installerfilename, strWrite);

    }

通過在設計器中單擊每個復選框為其分配一個名稱,然后在屬性窗格中填寫名稱屬性。 您現在可以輕松引用后面代碼中的復選框

突出顯示名稱的屬性窗格

現在在后面的代碼中,您可以自己從所有復選框創建一個列表。 請務必調用構造函數中的InitializeComponent方法執行此操作。

public class MyForm
{
    List<CheckBox> checkboxes;

    public MyForm()
    {
        // Call InitializeComponent first
        // or all the checkboxes will be null
        InitializeComponent();

        checkboxes = new List<CheckBox>();
        checkboxes.add(myCheckBox1);
        checkboxes.add(myCheckBox2); 
        // etc...
    }
}

您現在可以遍歷復選框並執行必要的操作,例如單擊按鈕時:

public void Button1_Clicked(...)
{

    // Loop over all checkboxes that are checked
    foreach(var checkbox in checkboxes.Where(x => x.Checked))
    {
        switch(checkbox.Name)
        {
            case "myCheckBox1":
                // MyCheckBox1 is checked, do something
                break;

            case "myCheckBox2":
               // MyCheckBox2 is checked, do something
               break;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM