You could easily accomplish this by custom drawing and using a state
object stored in each items object property.

OK, I took a few minutes to create an example. Create a new project,
save the form as MAIN.pas and then drop a Tcombobox on it. Generate
event stubs for FormCreate, FormDestroy and ComboBox1DrawItem.

Copy the following unit over the existing main.pas source.

While this works fine for the one off chance, I'd recommend creating a
TComboBox descendant that has a DisableItems TStrings published
property. This would then eliminate the need to utilise the Objects
property for each entry in the combobox.

///////// unit code

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TStateObject = class
  private
    FEnabled: Boolean;
  public
    constructor Create(const AEnabled: Boolean);
    property Enabled: Boolean read FEnabled;
  end;

  TForm29 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form29: TForm29;

implementation

{$R *.dfm}

{ TStateObject }

constructor TStateObject.Create(const AEnabled: Boolean);
begin
  FEnabled := AEnabled;
end;

procedure TForm29.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  lObj: TStateObject;
  lCombo: TComboBox;
  lDrawFlags: Cardinal;
  lStr: string;
begin
  if not (Control is TComboBox) then
    exit;
  lCombo := (Control as TComboBox);
  lObj := TStateObject(lCombo.Items.Objects[Index]);
  lCombo.Canvas.Brush.Style := bsSolid;
  // setup background color
  if (odSelected in State) and lObj.Enabled then
    lCombo.Canvas.Brush.Color := clHighlight
  else
    lCombo.Canvas.Brush.Color := lCombo.Color;
  lCombo.Canvas.FillRect(Rect);
  lCombo.Canvas.Brush.Style := bsClear;
  // setup font color
  if (odSelected in State) and lObj.Enabled then
    lCombo.Canvas.Font.Color := clHighlightText
  else if not lObj.Enabled then
    lCombo.Canvas.Font.Color := clGrayText
  else
    lCombo.Canvas.Font.Color := Font.Color;
  InflateRect(Rect, -2, -2);
  lDrawFlags := DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX
or DT_END_ELLIPSIS;
  lStr := lCombo.Items[Index];
  DrawText(lCombo.Canvas.Handle, PChar(lStr), Length(lStr), Rect, lDrawFlags);
  // comment this out if you want the focus rect to be draw even when a
  // disabled item has focus
  if (odSelected in State) and not lObj.Enabled then
  begin
    InflateRect(Rect, 2, 2);
    DrawFocusRect(lCombo.Canvas.Handle, Rect);
  end;
end;

procedure TForm29.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
  ComboBox1.Style := csOwnerDrawFixed;
  ComboBox1.Items.AddObject('One', TStateObject.Create(True));
  ComboBox1.Items.AddObject('Two', TStateObject.Create(True));
  ComboBox1.Items.AddObject('Three', TStateObject.Create(False));
  ComboBox1.Items.AddObject('Four', TStateObject.Create(False));
  ComboBox1.Items.AddObject('Five', TStateObject.Create(True));
end;

procedure TForm29.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to ComboBox1.Items.Count - 1 do
    TStateObject(ComboBox1.Items.Objects[i]).Free;
end;

end.

///////// end of unit code

Hope this helps,

Jeremy

On Dec 2, 2007 2:09 AM, John Barrat <[EMAIL PROTECTED]> wrote:
> Is it possible to enable individual items in the list of items.
> I want to show a complete list of items but only show specific items as
> being enabled and therefore selectable.
> I am sure I have done this before but damned if I can remember how.
>
> JohnB
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://lists.elists.org/cgi-bin/mailman/listinfo/delphi
>
_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to