如果选中了复选框,如何在产品前编写#如果没有,则删除?

时间:2015-10-07 10:19:50

标签: c#

我有一个文本文件Rows ... product.name1 product.name2 ... product.nameN ... Rows ,就像这样:

 private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
 {
     string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
     var lin = (path + "config.ini").ToString();
     var lines = File.ReadAllLines(lin);
     string InstallerFile = lines.Where(txt => txt.Contains("IstallerFile="))
                                 .Select(txt => txt.Split('=')[1].Replace("\"", ""))
                                 .FirstOrDefault();
     string pathTemp = @"C:\temp\";
     string[] pathArr = InstallerFile.Split('\\');
     string[] fileArr = pathArr.Last().Split('\\');
     string fileArr1 = String.Join(" ", fileArr);

     string installerfilename = pathTemp + fileArr1;
     string installertext = File.ReadAllText(installerfilename);
     var linInst = File.ReadLines(pathTemp + fileArr1).ToArray();

     if (this.ActiveControl != sender)
         return;

     CheckBox cb = sender as CheckBox;
     if ((cb.Checked) && (checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count))
     {
         for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
         {
             this.checkedListBox2.SetItemChecked(i, true);        
         }
     }
     else if ((!cb.Checked) && ((checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count) || (checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)))
     {                  
         //checkBox1.Checked = false;
         for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
         {
             this.checkedListBox2.SetItemChecked(i, false);                            
         }
     }
}

在表单应用程序中,我有一个CheckBox1来选择/取消选择ChecedListBox2中的所有项目,为此我使用下一个代码:

#

当选择checkBox1时,我想在product.之前写#,如果取消选中checkBox1,则在product之前删除AppDelegate.m。 到目前为止,我尝试了很多变种,但不能正常工作。那我该怎么做呢?

我知道已经存在类似的问题,但我已经尝试过并且在我的情况下不起作用。

1 个答案:

答案 0 :(得分:0)

如下所示修改循环并尝试

for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
             {
                 this.checkedListBox2.SetItemChecked(i, true);   
                 checkedListBox2.Items[i]=checkedListBox2.Items[i].ToString().Replace("product","#product");
             }

    for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
             {
                 this.checkedListBox2.SetItemChecked(i, false);       
    checkedListBox2.Items[i]=checkedListBox2.Items[i].ToString().Replace("#product","product");                     
             }
相关问题