const
int disabled_index = 0; // first
item for example
//---------------------------------------------------------------------------
//
(1) Implement a handler for the OnDrawItem event...
void __fastcall
TForm1::ComboBox2DrawItem(TWinControl *Control, int Index,
TRect
&Rect, TOwnerDrawState State)
{
//
eliminate artifacts
ComboBox2->Canvas->FillRect(Rect);
//
check to see if Index is our "disabled" item
if
(Index == 0)
{
// gray out it's text
ComboBox2->Canvas->Font->Color = clGray;
// white out the selection rectangle
if (State.Contains(odSelected))
{
ComboBox2->Canvas->Brush->Color = clWhite;
ComboBox2->Canvas->FillRect(Rect);
}
}
else
ComboBox2->Canvas->Font->Color = clBlack;
ComboBox2->Canvas->TextOut(Rect.Left,
Rect.Top, ComboBox2->Items->Strings[Index]);
}
//
(2) If the clicked item is our "disabled" item, keep
//
it from being selected. Use the OnChange event for this task...
void __fastcall
TForm1::ComboBox2Change(TObject *Sender)
{
if
(ComboBox2->ItemIndex == 0)
{
ComboBox2->ItemIndex = -1;
ComboBox2->DroppedDown = true;
}
}
|