/// <summary> /// Construct a ListBox instance from the given template. /// </summary> /// <param name="template"></param> public ListBox(ListBoxTemplate template) : base(template) { _items = template.Items; Title = template.Title; if (Title == null) { Title = ""; } CurrentSelected = -1; OwnerDraw = template.OwnerDraw; if (this.Size.Width < 3 || this.Size.Height < 3) { template.HasFrameBorder = false; } HasFrame = template.HasFrameBorder; _useSmallVersion = template.FrameTitle; HilightWhenMouseOver = template.HilightWhenMouseOver; CanHaveKeyboardFocus = template.CanHaveKeyboardFocus; LabelAlignment = template.LabelAlignment; TitleAlignment = template.TitleAlignment; CurrentSelected = template.InitialSelectedIndex; _mouseOverIndex = -1; _topIndex = 0; CalcMetrics(template); }
private void CalcMetrics(ListBoxTemplate template) { int numItems = _items.Count; int expandTitle = 0; int delta = Size.Height - numItems - 1; if (template.HasFrameBorder) { if (template.FrameTitle) { delta -= 1; } else { delta -= 3; } } _numberItemsDisplayed = _items.Count; if (delta < 0) { _numberItemsDisplayed += delta; } else if (delta > 0) { expandTitle = delta; } int titleWidth = Size.Width; int titleHeight = 1 + expandTitle; if (Title != "") { if (template.HasFrameBorder) { _titleRect = new Rectangle(Point.One, new Size(titleWidth - 2, titleHeight)); } else { _titleRect = new Rectangle(Point.Origin, new Size(titleWidth, titleHeight)); } } int itemsWidth = Size.Width; int itemsHeight = _numberItemsDisplayed; if (template.HasFrameBorder) { if (template.FrameTitle) { _itemsRect = new Rectangle(Point.One, new Size(itemsWidth - 2, itemsHeight)); } else { _itemsRect = new Rectangle(_titleRect.BottomLeft.Shift(0, 1), new Size(itemsWidth - 2, itemsHeight)); } } else { _itemsRect = new Rectangle(_titleRect.BottomLeft, new Size(itemsWidth, itemsHeight)); } }