Fórum Desabilitar item dentro de um combobox. Dá pra fazer isso? #177294
25/08/2003
0
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
Curtir tópico
+ 0
Responder
Posts
25/08/2003
Lfernandos
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;
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)