【问题标题】:.NET WinForm ComboBox - How to alter the DropDown behavior.NET WinForm ComboBox - 如何更改 DropDown 行为
【发布时间】:2010-07-11 21:43:58
【问题描述】:

我在 ComboBox 中有一个相当长的列表,我希望 DropDown 行为有所不同。

通常,当您单击箭头时,列表会展开并显示所有选项,从所选选项开始。 所选选项上方列出的选项是隐藏的,但可以通过向上滚动查看。

我希望列表向上滚动一点,尽可能在列表中间显示所选选项。

我已经看到了在启用滚动条的 FlowLayoutPanel 中执行此操作的方法,但我对 DDL 没有运气。该列表超过 50 项,因此仅显示整个列表是不切实际的。

【问题讨论】:

    标签: c# .net winforms scroll drop-down-menu


    【解决方案1】:

    在我看来,你可以使用自己的绘图项方法来达到效果。我的意思是,您将处理程序附加到 DrawItem 事件,然后在处理程序中,您将获得所有您希望显示的所需数据。然后将其绘制到屏幕上。

    例如:

    private void myComboBox_DrawItem(object sender, DrawItemEventArgs e)
            {
                if ( boundDataSource.Count > 0 && e.Index >= 0 )
                {
                  if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        //Get the data here
                        string dataToShow=  GetDataToShow()
    
                        e.DrawFocusRectangle();
    
                        System.Drawing.Graphics g = e.Graphics;
                        Rectangle r = e.Bounds;             
    
    
                        e.Graphics.FillRectangle(new SolidBrush(Color.Blue), r);
                        g.DrawStringdataToShow, e.Font, Brushes.White, r, stringFormat);
                        e.DrawFocusRectangle();
                        g.Dispose();
                    }
    
    
    
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 2010-11-04
      • 2016-08-03
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多