コード例 #1
0
        private void toolStripContainer1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.P && e.Control)
            {
                var model = new PopupListModel();

                // Dummy items - pass model into constructor?
                for (int loop = 1; loop <= 99; loop++)
                {
                    model.Items.Add(new PopupListItem($"Item {loop}", $"Hint {loop}"));
                }

                var popup = new PopupList(model);
                popup.Show(this);

                if (popup.DialogResult == DialogResult.OK)
                {
                }
            }
        }
コード例 #2
0
ファイル: PopupList.cs プロジェクト: Motivesoft/dmand.forms
        public PopupList(PopupListModel model, int visibleItemCount = 20)
        {
            Model            = model;
            VisibleItemCount = visibleItemCount;

            InitializeComponent();

            listBox1.DrawMode  = DrawMode.OwnerDrawFixed;
            listBox1.DrawItem += (object sender, DrawItemEventArgs e) => {
                // e.ForeColor and e.BackColor will be set based on selection status
                // We can amend them if we do a bit of the drawing ourselves and have
                // chosen alternative colors

                var foreColor = e.ForeColor;

                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    // Use a custom selection colour
                    Color selectionForeColor;
                    if (ThemeManager.TryAndGet(listBox1, "SelectionForeColor", out selectionForeColor))
                    {
                        foreColor = selectionForeColor;
                    }

                    Color selectionBackColor;
                    if (ThemeManager.TryAndGet(listBox1, "SelectionBackColor", out selectionBackColor))
                    {
                        // Use a custom selection colour
                        using (var brush = new SolidBrush(selectionBackColor))
                        {
                            e.Graphics.FillRectangle(brush, e.Bounds);
                        }
                    }
                    else
                    {
                        // Let the system deal with highlighting
                        e.DrawBackground();
                    }
                }
                else
                {
                    // Let the system deal with highlighting
                    e.DrawBackground();
                }

                // I don't think we want to do this - it has no value in this list control as we
                // dispose as soon as an item is clicked on (and by that, receives focus)
                //e.DrawFocusRectangle();

                using (var brush = new SolidBrush(foreColor))
                {
                    var item = (PopupListItem)listBox1.Items[e.Index];

                    var value = item.Value;
                    e.Graphics.DrawString(value, e.Font, brush, e.Bounds);

                    var hint = item.Hint;
                    if (!string.IsNullOrEmpty(hint))
                    {
                        var widthValue = Math.Ceiling(e.Graphics.MeasureString(value, e.Font).Width);
                        var widthHint  = Math.Ceiling(e.Graphics.MeasureString(hint, e.Font).Width);
                        if (e.Bounds.Width > widthValue + widthHint)
                        {
                            var hintBounds = new Rectangle((int)(e.Bounds.X + e.Bounds.Width - widthHint), e.Bounds.Y, (int)widthHint, e.Bounds.Height);
                            e.Graphics.DrawString(hint, e.Font, brush, hintBounds);
                        }
                    }
                }
            };

            UpdateList();

            // Set a default in case any creative exit routes are discovered
            DialogResult = DialogResult.Cancel;
        }