private void AddGroupToVisibilityDLL(GroupComboItem cb)
 {
     if (visibilityDDL.InvokeRequired)
         this.Invoke(new Action(() => AddGroupToVisibilityDLLAction(cb)));
     else
         AddGroupToVisibilityDLLAction(cb);
 }
 private void AddGroupToVisibilityDLLAction(GroupComboItem cb)
 {
     visibilityDDL.Items.Add(cb);
     if (cb.IsSelected)
         visibilityDDL.SelectedIndex = (visibilityDDL.Items.Count - 1);
 }
        //---------------------------------------------------------------
        #region PROPERTIES and GROUPS

        /// <summary>
        /// Populate visibility section with groups belonging to current user
        /// <param name="selectedGroupID">Default selected group.
        /// 0 :                                 Everyone
        /// -1 or CurrentUser.PersonalGroupID : Only Me
        /// GroupID :                           a specific group
        /// </param>
        /// <param name="ownerGroupName">Owner group name of current snippet.
        /// Empty if snippet is to be added.
        /// The name of the owner in case we want to display a specific snippet
        /// </param>
        /// </summary>
        public void PopulateGroups(int selectedGroupID, string ownerGroupName = "")
        {
            visibilityDDL.Items.Clear();
            string selectedGroupName = "";

            if (string.IsNullOrWhiteSpace(ownerGroupName))
            {
                // check for login error 
                if (!WebConnector.Current.IsLogged || (BaseWS.CurrentUser == null))
                {
                    bgw = new BackgroundWorker();
                    bgw.DoWork += new DoWorkEventHandler(bgw_CheckLoginAndConnection);
                    bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_CheckLoginAndConnectionCompleted);
                    bgw.WorkerReportsProgress = true;
                    if (!bgw.IsBusy)
                        bgw.RunWorkerAsync();

                    // add only Everyone group (selected), we cannot add other groups
                    GroupComboItem cbeveryone = new GroupComboItem();
                    cbeveryone.IsSelected = true;
                    selectedGroupName = Commons.GroupNames.Public; // "Everyone";
                    cbeveryone.GroupID = 0;
                    cbeveryone.Text = Commons.GroupNames.Public; // "Everyone";
                    AddGroupToVisibilityDLL(cbeveryone);
                }
                else
                {
                    // add OnlyMe group
                    GroupComboItem cbonlyme = new GroupComboItem();
                    if ((selectedGroupID == BaseWS.CurrentUser.PersonalGroupID) || (selectedGroupID < 0))
                    {
                        cbonlyme.IsSelected = true;
                        selectedGroupName = Commons.GroupNames.MyOwn; // "Only Me";
                    }
                    cbonlyme.GroupID = BaseWS.CurrentUser.PersonalGroupID;
                    cbonlyme.Text = Commons.GroupNames.MyOwn; // "Only Me";
                    AddGroupToVisibilityDLL(cbonlyme);

                    ICollection<Snip2Code.Model.Entities.Group> groupsList = BaseWS.CurrentUser.Groups;

                    foreach (Snip2Code.Model.Entities.Group g in groupsList)
                    {
                        if (g.ID == BaseWS.CurrentUser.PersonalGroupID)
                            continue;   // this is already pre-loaded !

                        GroupComboItem cb = new GroupComboItem();
                        if (g.ID == selectedGroupID)
                        {
                            cb.IsSelected = true;
                            selectedGroupName = g.Name;
                        }
                        cb.GroupID = g.ID;
                        cb.Text = g.Name;
                        AddGroupToVisibilityDLL(cb);
                    }

                    // add Everyone group
                    GroupComboItem cbeveryone = new GroupComboItem();
                    if (selectedGroupID == 0)
                    {
                        cbeveryone.IsSelected = true;
                        selectedGroupName = Commons.GroupNames.Public; // "Everyone";
                    }
                    cbeveryone.GroupID = 0;
                    cbeveryone.Text = Commons.GroupNames.Public; // "Everyone";
                    AddGroupToVisibilityDLL(cbeveryone);
                }
            }
            else
            {
                GroupComboItem cb = new GroupComboItem();
                cb.GroupID = 0;
                cb.IsSelected = true;
                cb.Text = ownerGroupName;
                AddGroupToVisibilityDLL(cb);
            }
        }