示例#1
0
        /// <summary>
        /// Selects or clears the selection for the specified item in a System.Windows.Forms.ListBox.
        /// </summary>
        /// <param name="lstBox">The listbox parent.</param>
        /// <param name="index">The index of the item.</param>
        /// <param name="value">The value to set to selected property.</param>
        public static void SetSelected(System.Windows.Forms.ListBox lstBox, int index, bool value)
        {
            if ((index < -1) || (index >= lstBox.Items.Count))
                throw new Exception("Invalid property value");

            if (lstBox.GetSelected(index) != value)
            {
                if ((value) && ((lstBox.SelectionMode == SelectionMode.MultiSimple) || (lstBox.SelectionMode == SelectionMode.MultiExtended)))
                {
                    if (selectedIndexList.ContainsKey(lstBox))
                        selectedIndexList[lstBox] = index;
                    else
                        throw new Exception("SelectedIndex property not stored for a MultiSelect ListBox, "
                            + "please add a ListBoxHelper to the form and set the property SelectionMode again");
                }

                lstBox.SetSelected(index, value);
            }
        }
示例#2
0
		/// <summary>
		///     Reloads items in the <c>ListBox</c>. If items are not sorted,
		///     selections are kept.
		/// </summary>
		/// <param name="listBox">
		///     <c>ListBox</c> to localize.
		/// </param>
		/// <param name="resources">
		///     <c>ResourceManager</c> object.
		/// </param>
		private void ReloadListBoxItems(System.Windows.Forms.ListBox listBox, System.Resources.ResourceManager resources) {
			if (listBox.Items.Count > 0) {
				int[] selectedItems = new int[listBox.SelectedIndices.Count];
				listBox.SelectedIndices.CopyTo(selectedItems, 0);
				ReloadItems(listBox.Name, listBox.Items, listBox.Items.Count, resources);
				if (!listBox.Sorted) {
					for (int i = 0; i < selectedItems.Length; i++) {
						listBox.SetSelected(selectedItems[i], true);
					}
				}
			}
		}
示例#3
0
        /// <summary>
        /// Function to set the selected index of a ListBox. Its behavior depends on 
        /// the selection mode property.
        /// </summary>
        /// <param name="lstBox">The listbox to set the SelectedIndex.</param>
        /// <param name="SelectedIndex">The value to be set.</param>
        /// <returns>Returns the selectedIndex after the operation.</returns>
        public static int SetSelectedIndex(System.Windows.Forms.ListBox lstBox, int SelectedIndex)
        {
            int currSelectedIndex = 0;
            bool mustBeClean = false;

            if ((lstBox.SelectionMode == SelectionMode.MultiSimple) || (lstBox.SelectionMode == SelectionMode.MultiExtended))
            {
                if (selectedIndexList.ContainsKey(lstBox))
                    selectedIndexList[lstBox] = SelectedIndex;
                else
                    throw new Exception("SelectedIndex property not stored for a MultiSelect ListBox, "
                        + "please add a ListBoxHelper to the form and set the property SelectionMode again");

                currSelectedIndex = lstBox.SelectedIndex;
                if ((SelectedIndex > -1) && (SelectedIndex < lstBox.Items.Count))
                {
                    mustBeClean = !lstBox.SelectedIndices.Contains(SelectedIndex);

                    lstBox.SetSelected(SelectedIndex, true);
                    if (mustBeClean)
                    {
                        ControlHelper.DisableControlEvents(lstBox, "SelectedIndexChanged");
                        lstBox.SetSelected(SelectedIndex, false);
                        ControlHelper.EnableControlEvents(lstBox, "SelectedIndexChanged");
                    }
                }
                else
                {
                    if (lstBox.Items.Count > 0)
                        lstBox.SelectedIndex = 0;
                    lstBox.SelectedIndex = -1;
                }

                lstBox.SelectedIndex = currSelectedIndex;
            }
            else
            {
                if ((SelectedIndex < -1) || (SelectedIndex >= lstBox.Items.Count))
                    throw new Exception("Invalid property value");

                lstBox.SelectedIndex = SelectedIndex;
            }

            return GetSelectedIndex(lstBox);
        }