MALIK GHAFRI

MALIK GHAFRI

  • NA
  • 26
  • 1.1k

Set ToErase property to true for checked checkboxes (checkedlistbox)

Feb 12 2023 12:40 PM

I am creating software for searching for some patterns from bin files and if it is available, It will be represented as checkedlistbox1 when clicking the open file button. This part is working successfully

but Now I want when the user clicks check for example on P0420 and then clicks save the file, the pattern of the corresponding name, (2004940101000078), will be replaced with zero, How I can trigger the checked checkbox in checkedlistbox1,

public class PatternMatch
{
   public string HexPattern {get;set;} // the pattern to search for
   public int HexIndex {get;set;} = -2; // the index where it was found, -1=not found, -2=not searched yet
   public string Name {get;set;} // the button name, like P0420, P0430
   public bool ToErase {get;set;} // whether to erase the matched pattern from the input
}

 

In the read file button::

// the source to search in
string hex2 = BitConverter.ToString(byteArray).Replace("-", string.Empty);

// your patterns and button names
List<PatternMatch> patterns = new List<PatternMatch>();
patterns.Add(new PatternMatch { HexPattern = "2004940101000078", Name = "P0420" });
patterns.Add(new PatternMatch { HexPattern = "3004940101000078", Name = "P0430" });
patterns.Add(new PatternMatch { HexPattern = "3E04940101000028", Name = "P043E" });
// etc

foreach(var pattern in patterns)
{
   // initialise search position
   pattern.HexIndex = -1;
   do
   {
      // try and find next match
      pattern.HexIndex = hex2.IndexOf(pattern.HexPattern, pattern.HexIndex+1);

     // repeat while there was something found, but at an odd index (false positive)
   } while (pattern.HexIndex != -1 && HexIndex % 2 == 1);
   // NB: in the original byte[] use half of pattern.HexIndex

   if (pattern.HexIndex == -1)
   {
       // Debug.WriteLine($"pattern {pattern.HexPattern} not found");
      // MessageBox.Show($"pattern {pattern.HexPattern} not found");

   }
   else
   {
       // Debug.WriteLine($"pattern {pattern.HexPattern} found at byte index {pattern.HexIndex / 2}");
      // MessageBox.Show($"pattern {pattern.HexPattern} found at byte index {pattern.HexIndex / 2}");

       checkedListBox1.Items.Add(pattern.Name);

   }

In the save file button:

List<PatternMatch> patterns = new List<PatternMatch>(); 
patterns.Add(new PatternMatch { HexPattern = "2004940101000078", Name = "P0420" });
patterns.Add(new PatternMatch { HexPattern = "3004940101000078", Name = "P0430" });
patterns.Add(new PatternMatch { HexPattern = "3E04940101000028", Name = "P043E" });

foreach (var toErase in patterns.Where(p => p.ToErase))
{
   // first remove the old pattern, then insert 0's (Replace would replace all occurances, not just the one at the index you found)
   hex2 = hex2.Remove(toErase.HexIndex, toErase.HexPattern.Length)
              .Insert(toErase.HexIndex, new string('0', toErase.HexPattern.Length));
}

My question is how to Set ToErase property to true for checked checkboxes?

What should I write in the checkedListBox1?

Thanks,

Malik


Answers (1)