views:

43

answers:

1

I've got a fairly long list in a ComboBox, and I want the DropDown behavior to be different.

Normally, when you click the arrow, the list expands showing all options, starting with the selected option. Options listed above the selected option are hidden, but can be seen by scrolling up.

I want the list to scroll up a bit, showing the selected option in the middle of the list, whenever possible.

I've seen ways to do this in a Scrollbar enabled FlowLayoutPanel, but I've had no luck with the DDL. The list is over 50 items long, so simply showing the whole list isn't practical.

+1  A: 

In my opinion, you can achieve the effect by using your own drawing item method. By this I mean, you attach an handler to the DrawItem event, then in the handler, you get all your required data that you wish to show. After that you draw it to the screen.

For example:

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();
                }



            }
        }
DrakeVN