Fórum Campo vazio - TEdit #579698
04/04/2017
0
Preciso de uma ajuda estou fazendo um programa teste onde tenho quatro Tedit, um RadioGroup, um Panel e 1 Botão. No evento onshow do formulário todos os TEdits ficam com status desabilitado e recebem uma string '#######'. No RadioGroup tem 4 opções de cálculo: Soma, Subtração, Multiplicação e Divisão. O Panel é o resultado do cálculo. O problema é quando seleciono soma e tento realizar o cálculo ele informa que '######' não é ponto flutuante e esse erro acontece em todas as opções.
Teriam como ajudar?
procedure Tfrmteste.Button1Click(Sender: TObject);
var
a,b: integer;
c,d: double;
begin
a:= strtoint(edit1.Text);
b:= strtoint(edit2.Text);
c:= strtofloat(edit3.Text);
d:= strtofloat (edit4.Text);
if RadioGroup1.ItemIndex = 0 then
begin
panel1.Caption:= inttostr(a+b);
end;
if RadioGroup1.ItemIndex = 1 then
begin
panel1.Caption:= inttostr(a-b);
end;
if RadioGroup1.ItemIndex = 2 then
begin
panel1.Caption:= floattostr(c*d);
end;
if RadioGroup1.ItemIndex = 3 then
begin
panel1.Caption:= floattostr(c/d);
end;
end;
procedure Tfrmteste.Button2Click(Sender: TObject);
begin
edit1.Clear;
edit2.Clear;
edit3.Clear;
edit4.Clear;
end;
procedure Tfrmteste.FormShow(Sender: TObject);
begin
for i := 0 to frmteste.ComponentCount - 1 do
if frmteste.Components[i] is TEdit then
begin
(frmteste.Components[i] as TEdit).Enabled:= false;
end;
end;
procedure Tfrmteste.RadioGroup1Click(Sender: TObject);
begin
if (RadioGroup1.ItemIndex = 0) or (RadioGroup1.ItemIndex = 1) then
begin
edit1.Enabled:= true;
edit2.Enabled:= true;
edit3.Enabled:= false;
edit4.Enabled:= false;
end;
if (RadioGroup1.ItemIndex = 2) or (RadioGroup1.ItemIndex = 3) then
begin
edit3.Enabled:= true;
edit4.Enabled:= true;
edit1.Enabled:= false;
edit2.Enabled:= false;
end;
end;
end.
Carlos Leonel
Curtir tópico
+ 0Post mais votado
05/04/2017
Então, o seu problema está nesse método, Button1Click:
procedure TForm1.Button1Click(Sender: TObject);
var
a,b: integer;
c,d: double;
begin
a:= strtoint(edit1.Text);
b:= strtoint(edit2.Text);
c:= strtofloat(edit3.Text);
d:= strtofloat(edit4.Text);
case RadioGroup1.ItemIndex of
0: begin
panel1.Caption:= inttostr(a+b);
end;
1: begin
panel1.Caption:= inttostr(a-b);
end;
2: begin
panel1.Caption:= floattostr(c*d);
end;
3: begin
panel1.Caption:= floattostr(c/d);
end;
end;
end;
Eu fiz uma adaptação do seu código, tirei os ifs do seu radiobuttons e utilizando a propriedade ItemIndex fiz a mesma coisa, apenas para ficar mais organizado (na minha opinião);
Vamos lá, qual é o problema?
Se você marcar 'soma' e preencher o edit1 e edit2 teoricamente deveria funcionar, porém, mesmo assim ele irá tentar converter o valor do edit3 e edit4, armazenados em "c" e depois "d", você terá o erro quando o seu código da linha c:= strtofloat(edit3.text) for executado, pois o conteúdo armazenado será '#####'
Temos algumas formas de resolver isso:
a) você pode setar sempre "0" em todos os edits
b) você pode fazer uma verificação antes de fazer a conversão para a,b,c,d tipo
c) vc pode criar uma rotina para validar o conteúdo dos edits
Ou seja, tem diversas formas.. eu fiz uma aqui mais simples para te ajudar, veja se te atende :)=
procedure TForm1.Button1Click(Sender: TObject);
var
a,b: integer;
c,d: double;
saida_int: integer;
saida_dou: double;
begin
case RadioGroup1.ItemIndex of
0..1: begin
if TryStrToInt(Edit1.Text, saida_int) then
begin
a:= saida_int;
end
else
begin
ShowMessage('Erro no conteúdo do Edit1. Impossível continuar');
Exit;
end;
if TryStrToInt(Edit2.Text, saida_int) then
begin
b:= saida_int;
end
else
begin
ShowMessage('Erro no conteúdo do Edit2. Impossível continuar');
Exit;
end;
end;
2..3: begin
if TryStrToFloat(Edit3.Text, saida_dou) then
begin
c:= saida_dou;
end
else
begin
ShowMessage('Erro no conteúdo do Edit3. Impossível continuar');
Exit;
end;
if TryStrToFloat(Edit4.Text, saida_dou) then
begin
d:= saida_dou;
end
else
begin
ShowMessage('Erro no conteúdo do Edit4. Impossível continuar');
Exit;
end;
end;
end;
case RadioGroup1.ItemIndex of
0: begin
panel1.Caption:= inttostr(a+b);
end;
1: begin
panel1.Caption:= inttostr(a-b);
end;
2: begin
panel1.Caption:= floattostr(c*d);
end;
3: begin
panel1.Caption:= floattostr(c/d);
end;
end;
end;
[]'
Wesley Yamazack
Gostei + 1
Mais Posts
05/04/2017
Carlos Leonel
Então refiz o código no evento do Botão onclick, tirei o evento do Form OnShow coloquei o TEdit recebendo ''0'' e funcionou.
Valeu pela ajuda.
Gostei + 0
06/04/2017
Wesley Yamazack
Uhuuuu .. qualquer coisa eh só falar.
Vamo q vamo
[]'
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)