colorindo errado
estou com o seguinte codigo e tá colorindo errado
begin
with TDBGrid(Sender) do
begin
if DataSource.DataSet.FieldByName('margem').AsString <= EDtnivel.text then
begin
if (gdSelected in State) then
Canvas.Brush.Color := clhighlight
else
begin
Canvas.Font.Style := [fsBold];
Canvas.Font.Color := clhighlight;
end
end;
Canvas.FillRect(Rect);
DefaultDrawColumnCell(Rect,DataCol,Column,State);
begin
if DataSource.DataSet.FieldByName('margem').AsString > EDtnivel.text then
begin
if (gdSelected in State) then
Canvas.Brush.Color := clteal
else
begin
Canvas.Font.Style := [fsBold];
Canvas.Font.Color := clteal;
end;
Canvas.FillRect(Rect);
DefaultDrawColumnCell(Rect,DataCol,Column,State);
end
end;
end;
o resultado é esse
[IMG]http://i64.tinypic.com/w7oms2.jpg[/IMG]
begin
with TDBGrid(Sender) do
begin
if DataSource.DataSet.FieldByName('margem').AsString <= EDtnivel.text then
begin
if (gdSelected in State) then
Canvas.Brush.Color := clhighlight
else
begin
Canvas.Font.Style := [fsBold];
Canvas.Font.Color := clhighlight;
end
end;
Canvas.FillRect(Rect);
DefaultDrawColumnCell(Rect,DataCol,Column,State);
begin
if DataSource.DataSet.FieldByName('margem').AsString > EDtnivel.text then
begin
if (gdSelected in State) then
Canvas.Brush.Color := clteal
else
begin
Canvas.Font.Style := [fsBold];
Canvas.Font.Color := clteal;
end;
Canvas.FillRect(Rect);
DefaultDrawColumnCell(Rect,DataCol,Column,State);
end
end;
end;
o resultado é esse
[IMG]http://i64.tinypic.com/w7oms2.jpg[/IMG]
Valmir Inacio
Curtidas 0
Respostas
Natanael Ferreira
30/01/2016
A comparação não está funcionando porque você está comparando texto (strings).
Altere seu código para que compare Float.
Troque isto:
Por isto:
Altere seu código para que compare Float.
Troque isto:
if DataSource.DataSet.FieldByName('margem').AsString <= EDtnivel.text then
Por isto:
if DataSource.DataSet.FieldByName('margem').AsFloat <= StrToFloat(EDtnivel.text) then
GOSTEI 0
Valmir Inacio
30/01/2016
resolveu o problema e apareceu outro, que é o seguinte se o Edit tiver valor não dá erro e roda normal, mas se ele não tiver valor acontece esse erro:
---------------------------
Project1
---------------------------
'' is not a valid floating point value.
---------------------------
OK
---------------------------
---------------------------
Project1
---------------------------
'' is not a valid floating point value.
---------------------------
OK
---------------------------
GOSTEI 0
Raimundo Pereira
30/01/2016
Format o campo edit para float ou use um componente próprio para valores
GOSTEI 0
Valmir Inacio
30/01/2016
ele já com o codigo, o problema agora é que se não tiver valor nenhum ele dá esse erro, se tiver valor ele funciona. eu poderia ja deixar algum valor fixo , porem se o usuario apagar o valor e executar vai dar o erro.
GOSTEI 0
Raimundo Pereira
30/01/2016
Primeira linha do executar
if (EDtnivel.text='') then
begin
ShowMessage('Acesso negado, informar a margem');
EDtnivel.SetFocus;
exit;
end;
if ( StrToFloat(EDtnivel.text)<=0)then
begin
ShowMessage('Acesso negado, informar a margem maior que 0,00');
EDtnivel.SetFocus;
exit;
end;
ShowMessage('Prossegue com a rotina');
end;
if (EDtnivel.text='') then
begin
ShowMessage('Acesso negado, informar a margem');
EDtnivel.SetFocus;
exit;
end;
if ( StrToFloat(EDtnivel.text)<=0)then
begin
ShowMessage('Acesso negado, informar a margem maior que 0,00');
EDtnivel.SetFocus;
exit;
end;
ShowMessage('Prossegue com a rotina');
end;
GOSTEI 0
Natanael Ferreira
30/01/2016
Troque StrToFloat para StrToFloatDef.
A diferença entre eles é que o StrToFloatDef atribui um valor padrão quando a conversão não é feita com sucesso.
Troque:
Por:
Neste caso, será atribuído zero se o edit estiver vazio e o erro não ocorrerá.
A diferença entre eles é que o StrToFloatDef atribui um valor padrão quando a conversão não é feita com sucesso.
Troque:
if DataSource.DataSet.FieldByName('margem').AsFloat <= StrToFloat(EDtnivel.text) then
Por:
if DataSource.DataSet.FieldByName('margem').AsFloat <= StrToFloatDef(EDtnivel.text, 0) then
Neste caso, será atribuído zero se o edit estiver vazio e o erro não ocorrerá.
GOSTEI 0
Valmir Inacio
30/01/2016
Obrigado Natanael resolveu e tá funcionando e P2 sua dica foi implementada em outro código que estava faltando conforme foi citado no exemplo e obrigado também
GOSTEI 0
Firstzillan
30/01/2016
??????????????
GOSTEI 0
Asadsdggfhfh
30/01/2016
?????????????
GOSTEI 0
Asadsdggfhfh
30/01/2016
?????????????
GOSTEI 0