/// <summary>
        /// Fill a checked list box with all the tags from a category, and set their check state
        /// A tag will be checked if there is a row in aVew whose aTagColumnName column has the tag value
        /// \note Its expected that aView has already been filtered for a specific item
        /// </summary>

        /*public static void FillCheckedList(CheckedListBox aCheckedList, string aCategoryName,
         *                                 DataView aView, string aTagColumnName)
         * {
         *  aCheckedList.BeginUpdate();
         *  aCheckedList.Items.Clear();
         *  aCheckedList.CheckOnClick = true;
         *
         *  var tagSet = SystemTags.GetTagSet(aCategoryName);
         *  foreach (var tag in tagSet.Tags.Values)
         *  {
         *      int index = aCheckedList.Items.Add(tag, false);
         *      foreach (DataRowView dbRow in aView)
         *      {
         *          if ((int)dbRow[aTagColumnName] == tag.Value)
         *          {
         *              aCheckedList.SetItemChecked(index, true);
         *              break;
         *          }
         *      }
         *  }
         *
         *  aCheckedList.EndUpdate();
         * }*/

        public static void Fill(this CheckedListBox value, string categoryName, int tagSetId)
        {
            Validation.IsNotNull(value, "value");
            Validation.IsNotNullOrEmpty(categoryName, "categoryName");

            value.BeginUpdate();
            value.Items.Clear();
            value.CheckOnClick = true;

            var dbContext         = Program.NinjectKernel.Get <IRealmDbContext>();
            var checkedTagSetList = dbContext.TagSets.Include(x => x.Tags).Where(x => x.Id == tagSetId);

            if (!checkedTagSetList.Any() || checkedTagSetList.Count() > 1)
            {
                value.EndUpdate();
                return;
            }
            var tagList = checkedTagSetList.First().Tags.ToList();

            var tagSet = SystemTags.GetTagSet(categoryName);

            foreach (var tag in tagSet.Tags.Values.Where(tag => tagList.Exists(x => x.Id == tag.Id)))
            {
                value.SetItemChecked(value.Items.Add(tag, false), true);
                break;
            }

            value.EndUpdate();
        }
        /// <summary>
        /// Fill a combobox with the tags for a category, and optionally select a specified tag
        /// If nullEntryName is non-empty, then a NULL tag will be added
        /// </summary>
        public static void Fill(this ComboBox value, string categoryName, int selectTag, string nullEntryName)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value), Resources.NullParameterErrorMessage);
            }
            if (string.IsNullOrEmpty(categoryName))
            {
                throw new ArgumentNullException(nameof(categoryName), Resources.NullParameterErrorMessage);
            }
            if (nullEntryName == null)
            {
                throw new ArgumentNullException(nameof(nullEntryName), Resources.NullParameterErrorMessage);
            }

            value.BeginUpdate();
            value.Items.Clear();

            if (nullEntryName.Length > 0)
            {
                var sysTag = new SystemTag(0, nullEntryName);
                var index  = value.Items.Add(sysTag);
                if (sysTag.Id == selectTag)
                {
                    value.SelectedIndex = index;
                }
            }

            var categoryTags = SystemTags.GetTagSet(categoryName);

            foreach (var sysTag in categoryTags.Tags.Values)
            {
                var index = value.Items.Add(sysTag);
                if (sysTag.Id == selectTag)
                {
                    value.SelectedIndex = index;
                }
            }

            if (value.SelectedIndex == -1 && value.Items.Count > 0)
            {
                value.SelectedIndex = 0;
            }

            value.EndUpdate();
        }
        public static void Fill(this CheckedListBox value, string categoryName, bool defaultCheckState)
        {
            Validation.IsNotNull(value, "value");
            Validation.IsNotNullOrEmpty(categoryName, "categoryName");

            value.BeginUpdate();
            value.Items.Clear();
            value.CheckOnClick = true;

            var tagSet = SystemTags.GetTagSet(categoryName);

            foreach (var tag in tagSet.Tags.Values)
            {
                value.Items.Add(tag, defaultCheckState);
            }

            value.EndUpdate();
        }