Beispiel #1
0
        /// <summary>
        /// Swap places of two items
        /// </summary>
        /// <param name="lstBox">The calling CheckedListBox</param>
        /// <param name="indexA">Index of forst item</param>
        /// <param name="indexB">Index of second item</param>
        /// <returns>CheckedListBox with new item order</returns>
        public static System.Windows.Forms.CheckedListBox SwapItems(this System.Windows.Forms.CheckedListBox lstBox, int indexA, int indexB)
        {
            if (indexB > -1 && indexB < lstBox.Items.Count)
            {
                object tmpItem = lstBox.Items[indexA];
                lstBox.Items[indexA] = lstBox.Items[indexB];
                lstBox.Items[indexB] = tmpItem;

                var stateA = lstBox.GetItemCheckState(indexA);
                var stateB = lstBox.GetItemCheckState(indexB);
                lstBox.SetItemCheckState(indexB, stateA);
                lstBox.SetItemCheckState(indexA, stateB);
            }
            return(lstBox);
        }