protected virtual void OnItemUnSelected(SelectableItem item)
 {
     if (ItemUnSelected != null)
     {
         ItemUnSelected(this, item);
     }
 }
        public void LoadItems <T>(IList <T> collection)
        {
            int maxdispcount = 0;

            if (collection == null || this.ItemIDField.Trim().Length == 0)
            {
                return;
            }

            m_dataSource = SelectableItem.Load <T>(collection, this.ItemIDField, this.ItemParentIDField, this.ItemTextField, this.ItemImageField, this.ItemActionField, this.ItemChildField, this.ItemArgsField);
            m_level      = 0;
            if (m_dataSource.ContainsKey(m_level))
            {
                m_levelSource = m_dataSource[m_level];

                m_pageindex  = 0;
                maxdispcount = (this.RowCount * this.ColumnCount);
                m_pagecount  = (collection.Count / maxdispcount) + (collection.Count % maxdispcount > 0 ? 1 : 0);
                PopulateItems(false);
                btnUp.Enabled   = false;
                btnDown.Enabled = (m_pagecount > 1);
            }
            else
            {
                ClearItems();
                btnUp.Enabled = btnDown.Enabled = false;
                lblPager.Text = string.Format("{0}/{1}", m_pagecount == 0 ? 0 : m_pageindex + 1, m_pagecount);
            }
        }
        private static void PrepareCollection <T>(int level, ref Dictionary <int, List <SelectableItem> > dcoll, string parent, List <T> collection, string idField, string parentField, string textField, string imageField, string actionField, string childField, string argsField)
        {
            Type                  iType = typeof(T);
            PropertyInfo          iProp;
            List <SelectableItem> litem;

            for (int index = 0; index < collection.Count; index++)
            {
                Image iImage = null;

                if (imageField.Length > 0)
                {
                    iProp = iType.GetProperty(imageField);

                    if (iProp.PropertyType.Equals(typeof(string)))
                    {
                        iImage = File.Exists(iProp.GetValue(collection[index], null).ToString()) ? Image.FromFile(iProp.GetValue(collection[index], null).ToString()) : null;
                    }
                    else if (iProp.PropertyType.Equals(typeof(IntPtr)))
                    {
                        iImage = iProp.GetValue(collection[index], null) != null?Image.FromHbitmap((IntPtr)iProp.GetValue(collection[index], null)) : null;
                    }
                    else if (iProp.PropertyType.Equals(typeof(Stream)))
                    {
                        iImage = iProp.GetValue(collection[index], null) != null?Image.FromStream(iProp.GetValue(collection[index], null) as Stream) : null;
                    }
                }

                SelectableItem item = new SelectableItem(iType.GetProperty(idField).GetValue(collection[index], null).ToString(),
                                                         parent,
                                                         textField.Length > 0 ? iType.GetProperty(textField).GetValue(collection[index], null).ToString() : string.Empty,
                                                         iImage,
                                                         actionField.Length > 0 ? iType.GetMethod(actionField) : null,
                                                         collection[index],
                                                         argsField.Length > 0 ? iType.GetProperty(argsField).GetValue(collection[index], null) : null);
                if (childField.Length > 0)
                {
                    PrepareCollection(level + 1, ref dcoll, item.ID,
                                      new List <T>((iType.GetProperty(childField).GetValue(collection[index], null) as IEnumerable <T>)),
                                      idField, parentField, textField, imageField, actionField, childField, argsField);
                }

                if (dcoll.ContainsKey(level))
                {
                    dcoll[level].Add(item);
                }
                else
                {
                    litem = new List <SelectableItem>();
                    litem.Add(item);
                    dcoll.Add(level, litem);
                }
            }
        }
        private void ItemButton_Click(object sender, EventArgs e)
        {
            int    maxdispcount;
            Button isender;

            isender = ((Button)sender);
            SelectableItem item = m_levelSource[(int)isender.Tag];

            if (m_dataSource.ContainsKey(m_level + 1) && m_dataSource[m_level + 1].Exists(delegate(SelectableItem child) { return(child.ParentID.CompareTo(item.ID) == 0); }))
            {
                m_level++;
                m_levelSource   = m_dataSource[m_level].FindAll(delegate(SelectableItem child) { return(child.ParentID.CompareTo(item.ID) == 0); });
                m_pageindex     = 0;
                maxdispcount    = (this.RowCount * this.ColumnCount) - 1;
                m_pagecount     = (m_levelSource.Count / maxdispcount) + (m_levelSource.Count % maxdispcount > 0 ? 1 : 0);
                btnUp.Enabled   = (m_pageindex > 0);
                btnDown.Enabled = (m_pageindex < m_pagecount - 1);
                PopulateItems(false);
            }
            else if (item.Action != null)
            {
                item.Action.Invoke(item.DataInstance, item.Args);
            }
            else
            {
                if (!this.MultiSelect)
                {
                    for (int row = 0; row <= m_rowendex; row++)
                    {
                        for (int cell = 0; cell <= (row == m_rowendex ? m_colendex : tlpItemCont.ColumnCount - 2); cell++)
                        {
                            tlpItemCont.Controls["btn_" + m_pageindex.ToString() + row.ToString() + cell.ToString()].BackColor = (m_dataSource.ContainsKey(m_level + 1) && m_dataSource[m_level + 1].Exists(delegate(SelectableItem child) { return(child.ParentID.CompareTo(m_levelSource[(int)tlpItemCont.Controls["btn_" + m_pageindex.ToString() + row.ToString() + cell.ToString()].Tag].ID) == 0); })) ? this.ParentControlColor : this.ItemControlColor;
                        }
                    }
                    m_lselectedButton.Clear();
                }

                if (m_lselectedButton.Exists(delegate(string bname) { return(bname.CompareTo(item.ID) == 0); }))
                {
                    isender.BackColor = (m_dataSource.ContainsKey(m_level + 1) && m_dataSource[m_level + 1].Exists(delegate(SelectableItem child) { return(child.ParentID.CompareTo(m_levelSource[(int)isender.Tag].ID) == 0); })) ? this.ParentControlColor : this.ItemControlColor;
                    m_lselectedButton.RemoveAt(m_lselectedButton.FindIndex(delegate(string bname) { return(bname.CompareTo(item.ID) == 0); }));
                    OnItemUnSelected(item);
                }
                else
                {
                    isender.BackColor = this.SelectedControlColor;
                    m_lselectedButton.Add(item.ID);
                    OnItemSelected(item);
                }
            }
        }
        public void ReloadItems <T>(IList <T> collection, bool isFirstOrder)
        {
            int maxdispcount = 0;

            if (collection == null || this.ItemIDField.Trim().Length == 0)
            {
                return;
            }

            if (isFirstOrder)
            {
                int count = 0;
                T   objToRemove;
                foreach (T item in collection)
                {
                    Type type = collection[count].GetType();
                    // object obj = collection[count];

                    System.Reflection.PropertyInfo pi = type.GetProperty("Code");   //("KIT-PRODUC")
                    object codeVal = pi.GetValue(collection[count], null);

                    if (codeVal.ToString() == "KIT-PRODUC")
                    {
                        objToRemove = item;
                        break;
                    }
                    count++;
                }
                collection.Remove(collection[count]);
            }

            m_dataSource = SelectableItem.Load <T>(collection, this.ItemIDField, this.ItemParentIDField, this.ItemTextField, this.ItemImageField, this.ItemActionField, this.ItemChildField, this.ItemArgsField);
            m_level      = 0;
            if (m_dataSource.ContainsKey(m_level))
            {
                m_levelSource = m_dataSource[m_level];

                m_pageindex  = 0;
                maxdispcount = (this.RowCount * this.ColumnCount);
                m_pagecount  = (collection.Count / maxdispcount) + (collection.Count % maxdispcount > 0 ? 1 : 0);
                PopulateItems(false);
                btnUp.Enabled   = false;
                btnDown.Enabled = (m_pagecount > 1);
            }
            else
            {
                ClearItems();
                btnUp.Enabled = btnDown.Enabled = false;
                lblPager.Text = string.Format("{0}/{1}", m_pagecount == 0 ? 0 : m_pageindex + 1, m_pagecount);
            }
        }
        public static Dictionary <int, List <SelectableItem> > Load <T>(IEnumerable <T> selectableitems, string idField, string parentField, string textField, string imageField, string actionField, string childField, string argsField)
        {
            Type                  iType = typeof(T);
            PropertyInfo          iProp;
            List <SelectableItem> litem;
            Dictionary <int, List <SelectableItem> > dcoll = new Dictionary <int, List <SelectableItem> >();

            if (selectableitems == null)
            {
                return(dcoll);
            }

            IList <T> collection = new List <T>(selectableitems);

            idField     = idField.Trim();
            parentField = parentField.Trim();
            textField   = textField.Trim();
            childField  = childField.Trim();
            imageField  = imageField.Trim();
            actionField = actionField.Trim();
            argsField   = argsField.Trim();

            for (int index = 0; index < collection.Count; index++)
            {
                Image iImage = null;

                if (imageField.Length > 0)
                {
                    iProp = iType.GetProperty(imageField);

                    if (iProp.PropertyType.Equals(typeof(string)))
                    {
                        iImage = File.Exists(iProp.GetValue(collection[index], null).ToString()) ? Image.FromFile(iProp.GetValue(collection[index], null).ToString()) : null;
                    }
                    else if (iProp.PropertyType.Equals(typeof(IntPtr)))
                    {
                        iImage = iProp.GetValue(collection[index], null) != null?Image.FromHbitmap((IntPtr)iProp.GetValue(collection[index], null)) : null;
                    }
                    else if (iProp.PropertyType.Equals(typeof(Stream)))
                    {
                        iImage = iProp.GetValue(collection[index], null) != null?Image.FromStream(iProp.GetValue(collection[index], null) as Stream) : null;
                    }
                }

                SelectableItem item = new SelectableItem(iType.GetProperty(idField).GetValue(collection[index], null).ToString(),
                                                         string.Empty,
                                                         textField.Length > 0 ? iType.GetProperty(textField).GetValue(collection[index], null).ToString() : string.Empty,
                                                         iImage,
                                                         actionField.Length > 0 ? iType.GetMethod(actionField) : null,
                                                         collection[index],
                                                         argsField.Length > 0 ? iType.GetProperty(argsField).GetValue(collection[index], null) : null);
                if (childField.Length > 0)
                {
                    PrepareCollection <T>(1, ref dcoll, item.ID,
                                          new List <T>((iType.GetProperty(childField).GetValue(collection[index], null) as IEnumerable <T>)),
                                          idField, parentField, textField, imageField, actionField, childField, argsField);
                }

                if (dcoll.ContainsKey(0))
                {
                    dcoll[0].Add(item);
                }
                else
                {
                    litem = new List <SelectableItem>();
                    litem.Add(item);
                    dcoll.Add(0, litem);
                }
            }
            return(dcoll);
        }