Metainformationen zur Seite
  •  

Dies ist eine alte Version des Dokuments!


Artikel Info

Artikel Info

Name:
Artikel Name
Beschreibung:
Artikel Beschreibung
Bereich:
anleitung
Kategorien:
programmieren
Version:
2014-11-13

CheckListBox mit nicht veränderbaren Items

Um in einer Listbox unveränderliche Items darzustellen habe ich in der Dialogklasse zwei Funktionen hinzugefügt:

    SetReadonlyItem (int index, CheckState value)
    addChkListBoxItem_ItemChanged(object sender, ItemCheckEventArgs e) 

Im Konstruktor wird die addChkListBoxItem_ItemChanged dem ItemCheck-Event des CheckedListBox Objekts angemeldet.

Die SetReadonlyItem wird verwendet um ReadOnlyItems zu setzen und ändern.

        public partial class SelectDialog : Form
        {
            private List<int> readOnlyItems = new List<int>();
 
            public SelectDialog()
            {
                InitializeComponent();
                this.add_chkListBox.ItemCheck += this.addChkListBoxItem_ItemChanged;
            }
 
 
            /// <summary>
            /// Set an CheckListBox item to an readonly item.
            /// CheckState of readonly items can be changed programmatically but not within dialogbox
            /// </summary>
            /// <param name="index">Index of Item</param>
            /// <param name="value">CheckState of item</param>
            public void SetReadonlyItem (int index, CheckState value)
            {
                this.add_chkListBox.ItemCheck -= this.addChkListBoxItem_ItemChanged;
                this.add_chkListBox.SetItemCheckState(index, value);
                this.add_chkListBox.ItemCheck += this.addChkListBoxItem_ItemChanged;
                if (!this.readOnlyItems.Contains(index))
                {
                    this.readOnlyItems.Add(index);
                }
            }
 
            private void addChkListBoxItem_ItemChanged(object sender, ItemCheckEventArgs e)
            {
                 if (readOnlyItems.Contains(e.Index))
                 {
                     e.NewValue = e.CurrentValue;
                 }
            }
        }

Diskussion

Geben Sie Ihren Kommentar ein. Wiki-Syntax ist zugelassen: