Alan Avery

Alan Avery

  • NA
  • 6
  • 521

Address Form checkbox methods programatically?

Jul 28 2019 3:02 PM
Hello.

That's the name of the program I probably should have started C# with.  Instead, I've built a small application that identifies musical keys from individual notes through the application of falsification offsets from identified notes.

The program works, but I've got a dozen tickbox methods that all do exactly the same thing save for 

1.  A single numerical value which informs the offset falsifier method
2.  The name of the checkbox control, which is used to link the boolean value of the truthtable to the checkbox value, and provide forward and backwards functionality (i.e., it sets true on check, and false on uncheck).

There has to be a better way to do this.  Ideally, I'd be able to parameterize the control name, and the passed num value.  Even more ideally, I would be able to handle all of the  tickboxes with a single method, with the linked values bound to the control state rather than repeating myself a dozen times.

Or, just as likely, there is some solution that simply escapes my imagination.

The values in question are highlighted below.

Your input is appreciated.

  1. //Is there a way to handle all of the checkboxes with a single entry?    
  2. //Perhaps a way to bind a numerical value to each tickbox,     
  3. //which would be passed to a handler method?    
  4. private void CTick_CheckedChanged(object sender, EventArgs e)    
  5. {    
  6.     byte num = 0;    
  7.     Roots[num] = CTick.Checked//C is Checked, Roots[0] = True, else False    
  8.     KillMaj();  //Eliminate impossible keys    
  9.     DisplayKeys(); //Update display                
  10. }    
  11.    
  12. //Same thing 11 more times  
  13. private void CSharpTick_CheckedChanged(object sender, EventArgs e)    
  14. {    
  15.     byte num = 1;    
  16.     Roots[num] = CSharpTick.Checked;    
  17.     KillMaj();    
  18.     DisplayKeys();    
  19. }    
  20.   
  21. private void DTick_CheckedChanged(object sender, EventArgs e)    
  22. {    
  23.     byte num = 2;    
  24.     Roots[num] = DTick.Checked;    
  25.     KillMaj();    
  26.     DisplayKeys();    
  27. }    
  28.   
  29. [... you get the idea]  

Answers (1)