End Google Ads 201810 - BS.net 01 --> I have a Windows Forms ListBox object showing items that originate in a dictionary.
To apply different coloring etc to some of the entries I have set
dropDownListBox.DrawMode = DrawMode.OwnerDrawFixedand handling the listbox DrawItem and MeasureItem events.

The different colored items are basically additional choices or items I add or remove depending on other factors.
These are commands if you like eg. "Advanced Filter", "Remove This Filter" etc and always appear at the top of the listbox items, and are highlighted to separate them from the normal data values.
When I show the listbox I set the dropDownListBox.SelectedItem to the previously selected item which may be well down the list.

I would like to be able to "freeze" the required top row(s) or commands eg "Advanced Filter", so that as I scroll down, or the listbox shows with the previously selected item, the required rows are always visible.

This would save scrolling back right back to the start to select "Advanced Filter".

Not having much joy so far, can anyone assist?

Thanks
AussieLew

DrawItem and MeasureItem handlers below...


void dropDownListBox_DrawItem(object sender, DrawItemEventArgs e)
{
Brush myBrush = Brushes.Green;
Brush backBrush = Brushes.Red; // This color not used

// string to draw
string listString = dropDownListBox.Items[e.Index].ToString();

// paint the list background
e.DrawBackground();

switch (listString)
{
case "Advanced Filter...":
myBrush = Brushes.DarkGreen;
backBrush = Brushes.PaleGreen;
e.Graphics.FillRectangle(backBrush, e.Bounds);
// check what filter commands are included in the list
// draw a separator line below the command if it is the last before the autofilter choices
if (filterCommandFlag == 2)
{
// needed to draw the line at e.Bounds.Bottom-1 otherwise the line was overwritten at times
e.Graphics.DrawLine(Pens.Green, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
}
break;

case "Remove This Filter":
myBrush = Brushes.DarkGreen;
backBrush = Brushes.PaleGreen;
e.Graphics.FillRectangle(backBrush, e.Bounds);
// no need for separator line here. This entry is never the last.
//e.Graphics.DrawLine(Pens.Black, e.Bounds.Left, e.Bounds.Bottom-1, e.Bounds.Right, e.Bounds.Bottom-1);
break;

case "Remove All Filters":
myBrush = Brushes.OrangeRed;
backBrush = Brushes.PaleGreen;
e.Graphics.FillRectangle(backBrush, e.Bounds);
// check what filter commands are included in the list
// draw a separator line below the command if it is the last before the autofilter choices
if (filterCommandFlag > 3 && filterCommandFlag < 8)
{
// needed to draw the line at e.Bounds.Bottom-1 otherwise the line was overwritten at times
e.Graphics.DrawLine(Pens.Green, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
}
break;

case "Blanks":
myBrush = Brushes.OrangeRed;
backBrush = Brushes.PaleGreen;
e.Graphics.FillRectangle(backBrush, e.Bounds);
// check what filter commands are included in the list
// draw a separator line below the command if it is the last before the autofilter choices
//if (filterCommandFlag == 3)
// {
// // needed to draw the line at e.Bounds.Bottom-1 otherwise the line was overwritten at times
// e.Graphics.DrawLine(Pens.Green, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
// }
break;

case "NonBlanks":
myBrush = Brushes.OrangeRed;
backBrush = Brushes.PaleGreen;
e.Graphics.FillRectangle(backBrush, e.Bounds);
// check what filter commands are included in the list
// draw a separator line below the command if it is the last before the autofilter choices
if (filterCommandFlag > 7)
{
// needed to draw the line at e.Bounds.Bottom-1 otherwise the line was overwritten at times
e.Graphics.DrawLine(Pens.Green, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
}

break;
}
// If the item is the selected item, then draw the rectangle
// filled in. The item is selected when a bitwise And
// of the State property and the DrawItemState.Selected
// property is true.
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(Brushes.SpringGreen, e.Bounds);
// Draw a rectangle in blue around the selected item.
//e.Graphics.DrawRectangle(Pens.Blue, e.Bounds);
}
// draw the string
e.Graphics.DrawString(listString, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
}



private void dropDownListBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
Font font = dropDownListBox.Font;
SizeF stringSize = e.Graphics.MeasureString(font.Name, font);
e.ItemHeight = (int)stringSize.Height;

}