vendredi 5 juin 2015

Clear the 'All' checkbox when some of the sub-checkboxes have been un-clicked?

I have a form that has several check-boxes, and an 'All' check-box that, of course, fills or empties the rest. The issue that I'm running into is that I need it to un-check the 'All' box once one of the sub-boxes has been unchecked. The logic of how to do this seems to be escaping me at the moment, so any tips would be greatly appreciated.

Here's the code from that portion of the project:

public partial class SrsSelectForm : Form
{

/// Property to return the full SRS names for checked items

public List<string> SelectedSrs
{
  get
  {
    return (from cb in checkBoxes
            where cb.Checked
            select cb.Tag.ToString()).ToList();
  }
}

/// <summary>
/// Unique Srs full names for all setting nodes on the current device
/// </summary>
private HashSet<string> allSrs;

/// <summary>
/// Tracks the checkboxes generated for the available SRS
/// </summary>
private List<CheckBox> checkBoxes;

/// <summary>
/// Constructor
/// </summary>
public SrsSelectForm()
{
  InitializeComponent();

  allSrs = new HashSet<string>();
  checkBoxes = new List<CheckBox>();
}

/// <summary>
/// Event handler
/// </summary>
/// <param name="sender">this form</param>
/// <param name="e">Event args</param>
private void FormLoad(object sender, EventArgs e)
{
  // center on parent
  Utilities.SetFormLocation(this);
}

/// <summary>
/// Retrieves available SRS for the setting nodes and creates check boxes
/// for selection 
/// </summary>
/// <param name="device">currently loaded device</param>
/// <param name="folder">the user's selected output folder</param>
/// <param name="node">starting settings node</param>
public void Populate(IDevice device, string folder, TreeNode node)
{
  // find all Srs output file names
  GetSrsNames(device, folder, node);

  // generate controls for them
  CreateSrsControls();
}

/// <summary>
/// Generates checkboxes for available SRS
/// </summary>
private void CreateSrsControls()
{
  int xPos = 8;
  int yPos = 55;
  int height = 24;

  foreach (string srsPath in allSrs)
  {
    CheckBox cb = new CheckBox();

    cb.AutoSize = true;
    cb.Location = new Point(xPos, yPos);

    // friendly name
    cb.Text = Utilities.GetSrsGroupName(srsPath);

    // store full name for returning to caller
    cb.Tag = srsPath;

    cb.Checked = true;

    grpSrs.Controls.Add(cb);
    checkBoxes.Add(cb);

    yPos += height;
  }
}

/// <summary>
/// Finds all Srs for the settings tree
/// </summary>
/// <param name="device">the currently loaded device</param>
/// <param name="folder">the selected export folder</param>
/// <param name="node">a tree node</param>
private void GetSrsNames(IDevice device, string folder, TreeNode node)
{
  if (node.Tag is DeviceDefinitionNodeValue)
  {
    var nodeValue = (DeviceDefinitionNodeValue)node.Tag;

    if (nodeValue.IsRelayGroup)
    {
      string fileName = string.Empty;
      if (Utilities.GetUsableSrsFileName(device, nodeValue, folder, ref fileName))
      {
        allSrs.Add(fileName);
      }
    }
  }

  // recurse
  foreach (TreeNode subNode in node.Nodes)
  {
    GetSrsNames(device, folder, subNode);
  }
}

/// <summary>
/// Toggles the SRS checkboxes in response to the All c/b changing
/// </summary>
/// <param name="sender">checkbox control</param>
/// <param name="e">event args</param>
private void CheckedOptionChanged(object sender, EventArgs e)
{
  if (sender == cbAll)
  {
    foreach (var cb in checkBoxes)
    {
      cb.Checked = cbAll.Checked;
    }
  }
}




Aucun commentaire:

Enregistrer un commentaire