protected virtual void SelectCurrentItem() { if (_listBox.SelectedIndex == -1) { return; } SelectedItem = ((ItemWrapper)_listBox.SelectedItem).Item; Text = ItemDisplayStringAdaptor.GetDisplayLabel(SelectedItem); // SelectedItem.ToString(); if (Text.Length > 0) { SelectionStart = Text.Length; } }
protected virtual void UpdateList() { int selectedIndex = _listBox.SelectedIndex; _listBox.BeginUpdate(); _listBox.Items.Clear(); _listBox.Font = Font; _listBox.ItemHeight = _listBox.Font.Height; int maxWidth = Width; using (Graphics g = (_autoSizePopup) ? CreateGraphics() : null) { // Text.Trim() allows "1 " to trigger updating the list and matching "1 Universe", where // the match involves "1" and "Universe" separately. (Note that "1 " does not match either // "1" or "Universe".) foreach (object item in ItemFilterer.Invoke(Text.Trim(), Items, ItemDisplayStringAdaptor)) { string label = ItemDisplayStringAdaptor.GetDisplayLabel(item); _listBox.Items.Add(new ItemWrapper(item, label)); if (_autoSizePopup) { maxWidth = Math.Max(maxWidth, MeasureItem(g, label).Width); } } } _listBox.EndUpdate(); _listBox.SelectedIndex = selectedIndex; if (_listBox.Items.Count == 0) { HideList(); } else { int visItems = _listBox.Items.Count; if (visItems > 8) { visItems = 8; } _listBox.Height = (visItems * _listBox.ItemHeight) + 4; switch (BorderStyle) { case BorderStyle.FixedSingle: { _listBox.Height += 2; break; } case BorderStyle.Fixed3D: { _listBox.Height += 4; break; } } if (_autoSizePopup) { bool hasScrollBar = visItems < _listBox.Items.Count; _listBox.Width = maxWidth + (hasScrollBar ? 10 : 0); // in theory this shouldn't be needed } _listBox.RightToLeft = RightToLeft; } }