Desabilitar item dentro de um combobox. Dá pra fazer isso?
Bom dia companheiros..
Como faço para desabilitar um item dentro de um combobox?
Se puderem me ajudar ficarei muito grato.
Um grande abraço a todos...
Pablo - Marília/SP
Como faço para desabilitar um item dentro de um combobox?
Se puderem me ajudar ficarei muito grato.
Um grande abraço a todos...
Pablo - Marília/SP
Pablo_lima
Curtidas 0
Respostas
Lfernandos
25/08/2003
Uma solução que ainda pode ser melhorada, mas já é um bom caminho andado:
Mude a propriedade Style do ComboBox para csOwnerDrawFixed ou para csOwnerDrawVariable. E no evento OnDrawItem, que neste caso desabilita o primeiro item, coloque o seguinte código:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with ComboBox1.Canvas do
begin
FillRect(Rect);
// verifica se é o indice a ser desabilitado
if (Index = 0) then
begin
// Cor cinza
Font.Color := clGray;
// cor branca para o retangulo de seleção
if (odSelected in State) then
begin
Brush.Color := clWhite;
FillRect(Rect);
end;
end
else
begin
if (odSelected in State) then
begin
Font.Color := clWhite;
end
else
Font.Color := clBlack;
end;
TextOut(Rect.Left, Rect.Top, ComboBox1.Items.Strings[Index]);
end;
end;
E no evento OnChange:
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if (ComboBox1.ItemIndex = 0) then
begin
ComboBox1.ItemIndex := -1;
ComboBox1.DroppedDown := True;
end;
end;
Mude a propriedade Style do ComboBox para csOwnerDrawFixed ou para csOwnerDrawVariable. E no evento OnDrawItem, que neste caso desabilita o primeiro item, coloque o seguinte código:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with ComboBox1.Canvas do
begin
FillRect(Rect);
// verifica se é o indice a ser desabilitado
if (Index = 0) then
begin
// Cor cinza
Font.Color := clGray;
// cor branca para o retangulo de seleção
if (odSelected in State) then
begin
Brush.Color := clWhite;
FillRect(Rect);
end;
end
else
begin
if (odSelected in State) then
begin
Font.Color := clWhite;
end
else
Font.Color := clBlack;
end;
TextOut(Rect.Left, Rect.Top, ComboBox1.Items.Strings[Index]);
end;
end;
E no evento OnChange:
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if (ComboBox1.ItemIndex = 0) then
begin
ComboBox1.ItemIndex := -1;
ComboBox1.DroppedDown := True;
end;
end;
GOSTEI 0