c# - How to deselect just the button `Select all` not all items? -


i have checkbox select all/deselect items checkedlistbox. fore have next code works:

 private void checkedlistbox2_selectedindexchanged(object sender, eventargs e)         {  if (checkedlistbox2.checkeditems.count == checkedlistbox2.items.count)                  checkbox1.checked = true;             else if (checkedlistbox2.checkeditems.count != checkedlistbox2.items.count)                 checkbox1.checked = false; } 

but problem is, if have items checked (the button select all checked) , if make click on 1 item deselected (and button select all uncheck ). want when make click on 1 item deselect button select all not items?

edit: here code :

private void checkedlistbox2_selectedindexchanged(object sender, eventargs e) {              string installerfilename = string.format("{0}{1}", appdomain.currentdomain.basedirectory, "installer.ini");             ienumerable<string> inilines = file.readalllines(installerfilename).asenumerable();             /* string selecteditem = checkedlistbox2.selecteditem.tostring();             bool ischecked = checkedlistbox2.checkeditems.contains(selecteditem);*/             bool ischecked =       checkedlistbox2.checkeditems.contains(checkedlistbox2.selecteditem);                  if (ischecked)                     inilines = inilines.select(line => line == string.format("#product={0}", checkedlistbox2.selecteditem)                                                        ? regex.replace(line, string.format("#product={0}", checkedlistbox2.selecteditem), string.format(@"product={0}", checkedlistbox2.selecteditem))                                                        : line);                  else                     inilines = inilines.select(line => (line == string.format("product={0}", checkedlistbox2.selecteditem))                                                        ? regex.replace(line, string.format(@".*product={0}", checkedlistbox2.selecteditem), string.format(@"#product={0}", checkedlistbox2.selecteditem))                                                        : line);                  if (checkedlistbox2.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"));                   if (checkedlistbox2.checkeditems.count == checkedlistbox2.items.count)                                checkbox1.checked = true;                  else if (checkedlistbox2.checkeditems.count != checkedlistbox2.items.count)                     checkbox1.checked = false;                   string strwrite = string.join(environment.newline, inilines.toarray());                 file.writealltext(installerfilename, strwrite);              }       private void checkbox1_checkedchanged_1(object sender, eventargs e)             {                  string installerfilename = path + "installer.ini";                 string installertext = file.readalltext(installerfilename);                 var lin = file.readlines(path + "installer.ini").toarray();                  checkbox cb = sender checkbox;                 if ((cb.checked) && (checkedlistbox2.checkeditems.count != checkedlistbox2.items.count))                 {                                  //  checkbox1.checked = false;                     (int = 0; < this.checkedlistbox2.items.count; i++)                         {                             this.checkedlistbox2.setitemchecked(i, true)                                             }                     foreach (var txt in lin)                     {                         if (txt.contains("#product="))                         {                             // var name = txt.split('=')[1];                             installertext = installertext.replace("#product=", "product=");                         }                         file.writealltext(installerfilename, installertext);                     }                 }                     else if ((!cb.checked) && ((checkedlistbox2.checkeditems.count != checkedlistbox2.items.count) || (checkedlistbox2.checkeditems.count == checkedlistbox2.items.count)))                     {                          //checkbox1.checked = false;                         (int = 0; < this.checkedlistbox2.items.count; i++)                         {                             this.checkedlistbox2.setitemchecked(i, false);                                                     }                         foreach (var txt in lin)                         {                              if (txt.contains("product=") && (!txt.startswith("#")))                             {                                 // var name1 = txt.split('=')[1];                                 installertext = installertext.replace(txt, "#" +txt);                             }                             file.writealltext(installerfilename, installertext);                         }                     }              } 

you should define boolean field determine whether user manually checked checkbox or gets checked checkedlistbox2_selectedindexchanged method. check/uncheck items when field true. following code should work:

    bool _checkedmanually = true;      private void checkbox1_checkedchanged_1(object sender, eventargs e)     {         if (!_checkedmanually)         {             _checkedmanually = true;             return;         }         (int = 0; < checkedlistbox2.items.count; i++)         {             if (checkbox1.checked)                 checkedlistbox2.setitemchecked(i, true);             else                 checkedlistbox2.setitemchecked(i, false);         }     }      private void checkedlistbox2_selectedindexchanged(object sender, eventargs e)     {         _checkedmanually = false;          if (checkedlistbox2.checkeditems.count == checkedlistbox2.items.count)             checkbox1.checked = true;         else if (checkedlistbox2.checkeditems.count != checkedlistbox2.items.count)             checkbox1.checked = false;     } 

update:

as rakesh mentioned in comments, add line if(this.activecontrol != sender ) return; checkbox1_checkedchanged_1 method:

private void checkbox1_checkedchanged_1(object sender, eventargs e)             {                  string installerfilename = path + "installer.ini";                 string installertext = file.readalltext(installerfilename);                 var lin = file.readlines(path + "installer.ini").toarray();                  if(this.activecontrol != sender )                     return;                  checkbox cb = sender checkbox;                 if ((cb.checked)                 {                                  //  checkbox1.checked = false;                     (int = 0; < this.checkedlistbox2.items.count; i++)                         {                             this.checkedlistbox2.setitemchecked(i, true)                                             }                     foreach (var txt in lin)                     {                         if (txt.contains("#product="))                         {                             // var name = txt.split('=')[1];                             installertext = installertext.replace("#product=", "product=");                         }                         file.writealltext(installerfilename, installertext);                     }                 }                 else                  {                      //checkbox1.checked = false;                     (int = 0; < this.checkedlistbox2.items.count; i++)                     {                         this.checkedlistbox2.setitemchecked(i, false);                                                 }                     foreach (var txt in lin)                     {                          if (txt.contains("product=") && (!txt.startswith("#")))                         {                             // var name1 = txt.split('=')[1];                             installertext = installertext.replace(txt, "#" +txt);                         }                         file.writealltext(installerfilename, installertext);                     }                 }              } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -