PDA

View Full Version : غیر فعال کردن Item در ComboBox



Reza_kh
پنج شنبه 17 فروردین 1385, 17:33 عصر
کدی رو می خوام که با کلیک روی یک دکمه یه Item از ComboBox1 غیر فعال بشه؟ مثل اینکه Enabled اونو False کرده باشیم...

mzjahromi
پنج شنبه 17 فروردین 1385, 17:44 عصر
تا اونجا که من میدونم این کار امکان پذیر نیست میتونی اونو از لیست حذف کنی.

realman
شنبه 19 فروردین 1385, 09:55 صبح
دوست عزیز متاسفانه من در حال حاضر دلفی ندارم.این کد رو میگزارم لطفا امتحان کنید.در صورتی که جواب داد لطفا اطلاع بدید.



unit Unit1;

interface

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

type
// **********************************************
// This class is going to be used to set the properties
// for each item in the combo box. It will be added to
// each item using the AddObject function.
// The properties determine how the item is drawn and
// selected.
// **********************************************
TItemProperties = class(TObject)
private
FDisabled: Boolean;
FParentIndex: Integer;
public
property Disabled: Boolean read FDisabled write FDisabled;
property ParentIndex: Integer read FParentIndex write FParentIndex;
end;

// **********************************************
// Subclass the ComboBox to add the CN_COMMAND handler.
// **********************************************
TDisabledCombo = class(TComboBox)
private
procedure CNCommand
( var Message: TWMCommand
); message CN_COMMAND;
end;

TForm1 = class(TForm)
procedure FormCreate
( Sender: TObject
);
procedure FormDestroy
( Sender: TObject
);
private
// **********************************************
// Form method to handle drawing of items in our combo.
// **********************************************
procedure ComboBoxDrawItem
( Control: TWinControl
; Index: Integer
; Rect: TRect
; State: TOwnerDrawState
);
public
DC: TDisabledCombo;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate
( Sender: TObject
);
var
ItemProps: TItemProperties;
begin
// **********************************************
// This one is created in code but it can be moved to
// a seperate unit and installed in in the palette.
// **********************************************
DC := TDisabledCombo.Create(Self);
DC.Parent := Form1;
DC.OnDrawItem := ComboBoxDrawItem;
DC.Top := 100;
DC.Left := 100;
DC.Style := csOwnerDrawFixed;

// **********************************************
// An ItemProperties object must be created for each
// item and it's properties set for display and selection
// handling.
//
// Use AddObject to add it for each item.
//
// The tab at the beginning of the item text signifies that
// the item needs to be indented
// **********************************************
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 0;
DC.Items.AddObject('GroupA', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 0;
DC.Items.AddObject(#9+'One', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 0;
DC.Items.AddObject(#9+'Two', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 0;
DC.Items.AddObject(#9+'Three',TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := True;
ItemProps.ParentIndex := 0;
DC.Items.AddObject(#9+'Four', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 0;
DC.Items.AddObject(#9+'Five', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 7;
DC.Items.AddObject('GroupB', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := True;
ItemProps.ParentIndex := 7;
DC.Items.AddObject(#9+'Six', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 7;
DC.Items.AddObject(#9+'Seven',TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 7;
DC.Items.AddObject(#9+'Eight',TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 7;
DC.Items.AddObject(#9+'Nine', TObject(ItemProps));
ItemProps := TItemProperties.Create;
ItemProps.Disabled := False;
ItemProps.ParentIndex := 7;
DC.Items.AddObject(#9+'Ten', TObject(ItemProps));

end;

procedure TForm1.FormDestroy
( Sender: TObject
);
var
i: Integer;
begin
// **********************************************
// The added objects must be destroyed
// **********************************************
for i := 0 to DC.ItemCount-1 do
begin
TItemProperties(DC.Items.Objects[i]).Free;
end;
DC.Free;
end;

// **********************************************
//
// Here, draw the items that are supposed to be disabled
// in clGrayText and items that need to be indented on
// the basis of the item string beginning with a tab (#9).
//
// **********************************************
procedure TForm1.ComboBoxDrawItem
( Control: TWinControl
; Index: Integer
; Rect: TRect
; State: TOwnerDrawState
);
var
LRect: TRect;
CB: TDisabledCombo;
ItemState: TOwnerDrawState;
ItemProp: TItemProperties;
ParentProp: TItemProperties;
begin
LRect := Rect;
CB := TDisabledCombo(Control);
ItemProp := TItemProperties(CB.Items.Objects[Index]);
ItemState := State;
ParentProp := TItemProperties(CB.Items.Objects[ItemProp.ParentIndex]);
if ItemProp.Disabled or ParentProp.Disabled then
begin
ItemState := ItemState + [odDisabled];
end;
with CB.Canvas do
begin
FillRect( LRect );
Font.Color := clWindowText;
if odSelected in ItemState then
begin
if odDisabled in ItemState then
begin
Font.Color := clGrayText;
end;
if CB.Items[index][1] = #9 then
TextOut(LRect.left + 20, LRect.top, Copy(CB.Items[index],
2, Length(CB.Items[index])))
else
TextOut(LRect.left, LRect.top, CB.Items[index]);
end
else
begin
if odDisabled in ItemState then
begin
Font.Color := clGrayText;
end;
if CB.Items[index][1] = #9 then
TextOut(LRect.left + 20, LRect.top, Copy(CB.Items[index],
2, Length(CB.Items[index])))
else
TextOut(LRect.left, LRect.top, CB.Items[index]);
end;
end;
end;

// **********************************************
// This is the key to handling the disabled items!
//
// The CBN_SELENDOK notification message is handled
// to determine if the item has been designated to
// be disabled. If it has, then the ItemIndex is set
// to 0 if the item's parent has also been disabled
// or is set to it's parent if its parent has not
// been disabled
//
// **********************************************
procedure TDisabledCombo.CNCommand
(
var Message: TWMCommand
);
var
ItemProp: TItemProperties;
ParentProp: TItemProperties;
begin
case Message.NotifyCode of
CBN_SELENDOK:
begin
ItemProp := TItemProperties(Items.Objects[ItemIndex]);
ParentProp := TItemProperties(Items.Objects[ItemProp.ParentIndex]);
if ItemProp.Disabled or ParentProp.Disabled then
begin
Message.NotifyCode := CBN_SELENDCANCEL;
if ParentProp.Disabled then
begin
// Here, just set the itemindex to 0 if this item
// is disabled and the parent is also disabled
ItemIndex := 0;
end
else
begin
// else set the itemindex to the parent
ItemIndex := ItemProp.ParentIndex;
end;
end;
end;
else
inherited;
end;
end;

end.