PDA

Ver la Versión Completa : Declaracion If --- Then


Caral
06-10-2006, 22:03:48
Hola a todos
Bueno aqui molestando, tengo una pequeña consulta.
Tengo esto:

if ComboBox3.Text = 'Sumar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) + StrToFloat(ComboBox5.Text));
If Edit3.Text <> Edit4.Text Then
begin
Label4.Color:= CLRed;
Label4.Caption:= 'X';
end else
begin
Label4.Color:= CLLime;
Label4.Caption:= 'OK';
end;

if ComboBox3.Text = 'Restar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) + StrToFloat(ComboBox5.Text));
If Edit3.Text <> Edit4.Text Then
begin
Label4.Color:= CLRed;
Label4.Caption:= 'X';
end else
begin
Label4.Color:= CLLime;
Label4.Caption:= 'OK';
end;

Las dos sentencias son iguales, Sumar y Restar, pero quiero saber si se puedo hacer algo como esto, de alguna forma?

if ComboBox3.Text = 'Sumar' or 'Restar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) + StrToFloat(ComboBox5.Text));
If Edit3.Text <> Edit4.Text Then
begin
Label4.Color:= CLRed;
Label4.Caption:= 'X';
end else
begin
Label4.Color:= CLLime;
Label4.Caption:= 'OK';
end;

Asi por supuesto no me funciona, como podria hacerlo ?
Saludos y gracias de antemano

Neiro
06-10-2006, 22:14:18
intenta lo siguiente
if (ComboBox3.Text = 'Sumar') or (ComboBox3.Text = 'Restar') Then

Caral
06-10-2006, 22:15:01
En realidad el programa lo tengo actualmente asi y es peor, es repetitivo,
son 20 edit.

procedure TForm1.Button3Click(Sender: TObject);
begin
Timer1.Enabled:= False;
if ComboBox3.Text = 'Sumar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) + StrToFloat(ComboBox5.Text));
If Edit3.Text <> Edit4.Text Then
Label4.Color:= CLRed;
Label4.Caption:= 'X';
If Edit3.Text = Edit4.Text Then
Label4.Color:= CLLime;
Label4.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit6.Text := FloatToStr(StrToFloat(ComboBox6.Text) + StrToFloat(ComboBox7.Text));
If Edit5.Text <> Edit6.Text Then
Label5.Color:= CLRed;
Label5.Caption:= 'X';
If Edit5.Text = Edit6.Text Then
Label5.Color:= CLLime;
Label5.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit8.Text := FloatToStr(StrToFloat(ComboBox8.Text) + StrToFloat(ComboBox9.Text));
If Edit7.Text <> Edit8.Text Then
Label6.Color:= CLRed;
Label6.Caption:= 'X';
If Edit7.Text = Edit8.Text Then
Label6.Color:= CLLime;
Label6.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit10.Text := FloatToStr(StrToFloat(ComboBox10.Text) + StrToFloat(ComboBox11.Text));
If Edit9.Text <> Edit10.Text Then
Label7.Color:= CLRed;
Label7.Caption:= 'X';
If Edit9.Text = Edit10.Text Then
Label7.Color:= CLLime;
Label7.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit12.Text := FloatToStr(StrToFloat(ComboBox12.Text) + StrToFloat(ComboBox13.Text));
If Edit11.Text <> Edit12.Text Then
Label8.Color:= CLRed;
Label8.Caption:= 'X';
If Edit11.Text = Edit12.Text Then
Label8.Color:= CLLime;
Label8.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit14.Text := FloatToStr(StrToFloat(ComboBox14.Text) + StrToFloat(ComboBox15.Text));
If Edit13.Text <> Edit14.Text Then
Label9.Color:= CLRed;
Label9.Caption:= 'X';
If Edit13.Text = Edit14.Text Then
Label9.Color:= CLLime;
Label9.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit16.Text := FloatToStr(StrToFloat(ComboBox16.Text) + StrToFloat(ComboBox17.Text));
If Edit15.Text <> Edit16.Text Then
Label10.Color:= CLRed;
Label10.Caption:= 'X';
If Edit15.Text = Edit16.Text Then
Label10.Color:= CLLime;
Label10.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit18.Text := FloatToStr(StrToFloat(ComboBox18.Text) + StrToFloat(ComboBox19.Text));
If Edit17.Text <> Edit18.Text Then
Label11.Color:= CLRed;
Label11.Caption:= 'X';
If Edit17.Text = Edit18.Text Then
Label11.Color:= CLLime;
Label11.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit20.Text := FloatToStr(StrToFloat(ComboBox20.Text) + StrToFloat(ComboBox21.Text));
If Edit19.Text <> Edit20.Text Then
Label12.Color:= CLRed;
Label12.Caption:= 'X';
If Edit19.Text = Edit20.Text Then
Label12.Color:= CLLime;
Label12.Caption:= 'OK';
if ComboBox3.Text = 'Sumar' Then
Edit22.Text := FloatToStr(StrToFloat(ComboBox22.Text) + StrToFloat(ComboBox23.Text));
If Edit21.Text <> Edit22.Text Then
Label13.Color:= CLRed;
Label13.Caption:= 'X';
If Edit21.Text = Edit22.Text Then
Label13.Color:= CLLime;
Label13.Caption:= 'OK';

if ComboBox3.Text = 'Restar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) - StrToFloat(ComboBox5.Text));
If Edit3.Text <> Edit4.Text Then
Label4.Color:= CLRed;
Label4.Caption:= 'X';
If Edit3.Text = Edit4.Text Then
Label4.Color:= CLLime;
Label4.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit6.Text := FloatToStr(StrToFloat(ComboBox6.Text) - StrToFloat(ComboBox7.Text));
If Edit5.Text <> Edit6.Text Then
Label5.Color:= CLRed;
Label5.Caption:= 'X';
If Edit5.Text = Edit6.Text Then
Label5.Color:= CLLime;
Label5.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit8.Text := FloatToStr(StrToFloat(ComboBox8.Text) - StrToFloat(ComboBox9.Text));
If Edit7.Text <> Edit8.Text Then
Label6.Color:= CLRed;
Label6.Caption:= 'X';
If Edit7.Text = Edit8.Text Then
Label6.Color:= CLLime;
Label6.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit10.Text := FloatToStr(StrToFloat(ComboBox10.Text) - StrToFloat(ComboBox11.Text));
If Edit9.Text <> Edit10.Text Then
Label7.Color:= CLRed;
Label7.Caption:= 'X';
If Edit9.Text = Edit10.Text Then
Label7.Color:= CLLime;
Label7.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit12.Text := FloatToStr(StrToFloat(ComboBox12.Text) - StrToFloat(ComboBox13.Text));
If Edit11.Text <> Edit12.Text Then
Label8.Color:= CLRed;
Label8.Caption:= 'X';
If Edit11.Text = Edit12.Text Then
Label8.Color:= CLLime;
Label8.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit14.Text := FloatToStr(StrToFloat(ComboBox14.Text) - StrToFloat(ComboBox15.Text));
If Edit13.Text <> Edit14.Text Then
Label9.Color:= CLRed;
Label9.Caption:= 'X';
If Edit13.Text = Edit14.Text Then
Label9.Color:= CLLime;
Label9.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit16.Text := FloatToStr(StrToFloat(ComboBox16.Text) - StrToFloat(ComboBox17.Text));
If Edit15.Text <> Edit16.Text Then
Label10.Color:= CLRed;
Label10.Caption:= 'X';
If Edit15.Text = Edit16.Text Then
Label10.Color:= CLLime;
Label10.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit18.Text := FloatToStr(StrToFloat(ComboBox18.Text) - StrToFloat(ComboBox19.Text));
If Edit17.Text <> Edit18.Text Then
Label11.Color:= CLRed;
Label11.Caption:= 'X';
If Edit17.Text = Edit18.Text Then
Label11.Color:= CLLime;
Label11.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit20.Text := FloatToStr(StrToFloat(ComboBox20.Text) - StrToFloat(ComboBox21.Text));
If Edit19.Text <> Edit20.Text Then
Label12.Color:= CLRed;
Label12.Caption:= 'X';
If Edit19.Text = Edit20.Text Then
Label12.Color:= CLLime;
Label12.Caption:= 'OK';
if ComboBox3.Text = 'Restar' Then
Edit22.Text := FloatToStr(StrToFloat(ComboBox22.Text) - StrToFloat(ComboBox23.Text));
If Edit21.Text <> Edit22.Text Then
Label13.Color:= CLRed;
Label13.Caption:= 'X';
If Edit21.Text = Edit22.Text Then
Label13.Color:= CLLime;
Label13.Caption:= 'OK';

Y no puse las multiplicaciones y divisiones, si no seria mas codigo repetido.
Como simplifico esto ?
Saludos

seoane
06-10-2006, 22:15:13
Prueba asi:

if (ComboBox3.Text = 'Sumar') or (ComboBox3.Text ='Restar') Then

meditacion
06-10-2006, 22:23:10
Buenas,

Si pruebas con lo siguiente te quedará más claro el código y más reducido:

case combobox3.text of
'n' = {sentencias}
'ppp' = {sentencias}
else {sentencias}
end;


Un saludo

Neiro
06-10-2006, 22:23:36
por que no dices mejor que quieres hacer, se me hace que tienes mucho codigo repetido que se pudiera simplificar

Caral
06-10-2006, 22:23:56
Gracias pero si os fijais no sirve solo asi por esta parte:
en el caso de la suma:

if ComboBox3.Text = 'Sumar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) + StrToFloat(ComboBox5.Text));

en el caso de la resta:

if ComboBox3.Text = 'Sumar' Then
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) - StrToFloat(ComboBox5.Text));

En mi segundo mensaje puse parte del codigo, como lo tengo, son 20 edit, fijaros que el signo cambia o + o -, dependiendo de la operacion.
Saludos

seoane
06-10-2006, 22:25:42
Buenas,

Si pruebas con lo siguiente te quedará más claro el código y más reducido:

case combobox3.text of
'n' = {sentencias}
'ppp' = {sentencias}
else {sentencias}
end;


Un saludo

Seguro que quedaría mas claro pero no se pueden usar strings en un case, al menos no en Delphi ;)

Caral
06-10-2006, 22:31:27
Se que hay un problema de repeticion, ya pense en variables, incluso en un array, (pero no lo manejo muy bien), la verdad el sistema funciona solo que quiero simplificarlo.
Saludos

maeyanes
06-10-2006, 22:44:21
Algo que podrías hacer con el TComboBox, es usar la propiedad ItemIndex:


case ComboBox3.ItemIndex of
0: // Sumar
begin
// Aquí agrupas todo lo que se hace cuando la operación es sumar
end;
1: // Restar
begin
// Aquí agrupas todo lo que se hace cuando la operación es restar
end;
2: // etc...
begin
// Aquí agrupas todo lo que... etc... :p
end
end


Saludos...

Caral
06-10-2006, 23:04:56
Muchas gracias a todos
Hare una mezcla de todo un poco.
Saludos

German
07-10-2006, 03:50:55
Creo que dandole un poco mas de vuelta a la tuerca, el codigo podria ser el mismo en los dos casos (sumar o restar), si añades una condicion y un valor al principio, por ejemplo:

(...)
var SumaResta: Integer;
(...)

case ComboBox3.ItemIndex of
0: SumaResta := 1; // Esta seleccionado el primer item -> Hay que sumar
1: SumaResta := -1; // Esta seleccionado el segundo item -> Hay que restar
end;

// Mismo código para suma y resta:
Edit4.Text := FloatToStr(StrToFloat(ComboBox4.Text) + (StrToFloat(ComboBox5.Text) * SumaResta));
Edit6.Text := FloatToStr(StrToFloat(ComboBox6.Text) + (StrToFloat(ComboBox7.Text) * SumaResta));

etc...

50 + (10 * 1) = 60
50 + (10 * -1) = 40

Salu2.

Lepe
07-10-2006, 23:17:01
Caral, si funciona no lo toques, así de simple.

Si quieres optimizarlo, tocaría rehacer todo. Saber qué estas haciendo y buscar alternativas.

Yo no cambiaría los textos por Itemindex como han propuesto, es un lio total....
"Seleccionado el ItemIndex 3 y eso ¿era sumar o restar?, en el código tengo puesto un comentario diciendo que es sumar, pero el comentario está equivocado porque copié y pegué......"

El caso es que no entiendo nada de nada.....

If Edit7.Text <> Edit8.Text Then
Label6.Color:= CLRed;
Label6.Caption:= 'X';
If Edit7.Text = Edit8.Text Then
Label6.Color:= CLLime;
Label6.Caption:= 'OK';


Según está ese código, si Edit7 tiene algo distinto al Edit8, las lineas a ejecutar son:

Label6.Color:= CLRed;
Label6.Caption:= 'X';
Label6.Caption:= 'OK';


Es decir, se queda con el color rojo y diciendo "OK", porque no hay bloques begin ... end dentro de los "if" y por tatnto, si un "if" se cumple, solo ejecuta la siguiente sentencia que haya (aunque con el identado que haces quieras decir otra cosa).

Creo que lo correcto sería:

If Edit7.Text <> Edit8.Text Then
begin
Label6.Color:= CLRed;
Label6.Caption:= 'X';
end
else
begin
Label6.Color:= CLLime;
Label6.Caption:= 'OK';
end