![]() |
![]() |
| Paypal | FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
|||||||
| Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Temas de Hoy |
![]() |
|
|
Herramientas | Buscar en Tema | Desplegado |
|
#1
|
|||
|
|||
|
Problema con una TStringGrid
Bueno, realmente no se q pase, lo he checado mil veces y no se q anda mal, alguien me puede ayudar. Les dejo todo el codigo y tambien les dejo todos los botones q tengo. Espero alguien me ayude. Gracias.
El Programa simula el trafico de autos. Código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Edit1: TEdit;
StringGrid2: TStringGrid;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Columnas, veces, Renglones : Integer;
begin
Hint := 'Da valores iniciales a la primer cadena';
Randomize;
for Renglones := 0 to StringGrid1.RowCount - 1 do
for Columnas := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[Columnas,Renglones] := IntToStr(0);
for Columnas := 1 to StringGrid1.ColCount - 1 do
Begin
veces := Random(9) + 1;
If (veces = 1) and (StrToInt(StringGrid1.Cells[Columnas-1,0]) = 0)then
StringGrid1.Cells[Columnas,0] := IntToStr(Random(5) + 1);
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var
Velocidad : string;
Renglon : integer;
begin
with StringGrid1 do
begin
for renglon := 0 to RowCount - 1 do
begin
Velocidad := Cells[ACol,ARow];
if (Arow=Renglon) and (Velocidad='0') then
begin
Canvas.Brush.Color:=ClMaroon;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='1') then
begin
Canvas.Brush.Color:=Clred;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='2') then
begin
Canvas.Brush.Color:=Clblue;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='3') then
begin
Canvas.Brush.Color:=Clgreen;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='4') then
begin
Canvas.Brush.Color:=Clyellow;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='5') then
begin
Canvas.Brush.Color:=Clgray;
Canvas.FillRect(rect);
end;
end;
end;
end;
procedure TForm1.StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var
Velocidad : string;
begin
with StringGrid2 do
begin
Velocidad := Cells[ACol,ARow];
if (ACol=1) and (Velocidad='1') then
begin
Canvas.Brush.Color:=Clred;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='2') then
begin
Canvas.Brush.Color:=Clblue;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='3') then
begin
Canvas.Brush.Color:=Clgreen;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='4') then
begin
Canvas.Brush.Color:=Clyellow;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='5') then
begin
Canvas.Brush.Color:=Clgray;
Canvas.FillRect(rect);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
Velocidad, Renglon : Integer;
begin
Velocidad := 0;
for Renglon := 0 to StringGrid2.RowCount - 1 do
Begin
Velocidad := Velocidad + 1;
StringGrid2.Cells[0,Renglon]:= IntToStr(Velocidad);
StringGrid2.Cells[1,Renglon]:= IntToStr(Velocidad);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button4Click(Sender: TObject);
VAR
Columnas2, Espacio, i, automoviles : Integer;
VelocidadV : ARRAY[0..100] OF Integer;
Automoviles2, J, Probabilidad, VelMax : Integer;
Columnas, NewVel, Spacio, Renglones : Integer;
BEGIN
Randomize;
FOR Renglones := 0 TO StringGrid1.RowCount - 1 DO
BEGIN
FOR Columnas2 := 0 TO StringGrid1.ColCount - 1 DO
BEGIN
IF StringGrid1.Cells[Columnas2,Renglones] = '1' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '2' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '3' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '4' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '5' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '0' THEN
Espacio := Espacio + 1;
IF StringGrid1.Cells[Columnas2,Renglones] <> '0' THEN
BEGIN
Memo3.Lines.Add(StringGrid1.Cells[Columnas2,Renglones]);
Automoviles := Automoviles + 1;
END;
Edit1.Text := IntToStr( automoviles );
END;
FOR i:= 0 TO StringGrid1.Colcount - 1 DO
Memo1.Lines.Add(IntToStr(VelocidadV[i]));
VelMax := 5 ;
Automoviles2 := StrToInt(Edit1.Text) - 1;
FOR J := 0 TO Automoviles2 DO
BEGIN
IF J = Automoviles2 THEN
BEGIN
NewVel := StrToInt(Memo3.Lines[j]);
Spacio := 79 - StrToInt(Memo2.Lines[J]);
IF Spacio > VelMax THEN
Spacio := VelMax;
IF Spacio <= NewVel THEN
NewVel := Spacio;
Probabilidad := Random(5) + 1;
IF (Probabilidad = 1) AND (NewVel > 1) THEN
BEGIN
NewVel := NewVel - 1;
ShowMessage('La velocidad ha sido disminuida en 1');
END;
Columnas := StrToInt(Memo2.Lines[J]) + NewVel;
IF Columnas < 79 THEN //Aqui
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel)
ELSE
BEGIN
Columnas := NewVel;
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel);
END; //Termina
END
ELSE
BEGIN
NewVel := StrToInt(Memo3.Lines[j]);
Spacio := StrToInt(Memo2.Lines[j+1]) - StrToInt(Memo2.Lines[J]) - 1;
IF Spacio > 5 THEN
Spacio := 5;
IF Spacio <= NewVel THEN
NewVel := Spacio;
Probabilidad := Random(5) + 1;
IF (Probabilidad = 1) AND (NewVel > 1) THEN
BEGIN
NewVel := NewVel - 1;
ShowMessage('La velocidad ha sido disminuida en 1');
END;
Columnas := StrToInt(Memo2.Lines[J]) + NewVel;
IF Columnas < 79 THEN //Aqui
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel)
ELSE
BEGIN
Columnas := NewVel;
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel);
END; //Termina
END;
END;
Memo1.Clear;
Memo2.Clear;
Memo3.Clear;
END;
END;
END.
|
|
#2
|
||||
|
||||
|
Hola y bienvenido a los foros.
Revisa la guía de estilo de los foros, seguramente te dará algunas ideas para los próximos mensajes que tengas que postear. En cuanto al problema, no explicas nada d lo que te sucede, sólo que no funciona; No sabemos si es que te da algun error, si es que hace algo mal, si te falta algo,... También añades el código (PAS), entiendo que para que la gente pruebe el código (ya que no explicas nada de lo que hece -sólamente que simula vehículos-), pero esto no sirve de nada si no pones el DFM; Yo te recomiendo que lo comprimas y lo subas aquí y si no tienes espacio, le cambies la extensión y lo subas a algun sitio público. Si no tienes dónde ponerlo, mándanos un mensaje a alguno de los moderadores y te alojaremos temporalmente. Lo dicho, intenta explicar un poco mejor el programa y el problema, si no, es difícil ayudarte. Un saludo.
__________________
Germán Estévez => Web/Blog Guía de estilo, Guía alternativa Utiliza TAG's en tus mensajes. Contactar con el Clubdelphi ![]() P.D: Más tiempo dedicado a la pregunta=Mejores respuestas. |
|
#3
|
|||
|
|||
|
Cita:
¿simula el tráfico de autos? No entiendo que es lo que hace mal o si te da un error, cual es. Yo probé el código y no da ningún error pero resulta incomprensible. No se que querés hacer con el. saludos |
![]() |
|
|
Temas Similares
|
||||
| Tema | Autor | Foro | Respuestas | Último mensaje |
| Duda sobre un TStringGrid | Drahcir | Varios | 3 | 23-01-2006 20:58:05 |
| TStringGrid | jorge_mosquera | Varios | 1 | 10-02-2005 16:08:45 |
| Botón Derecho y TStringGrid | Wulvay | Varios | 5 | 22-09-2004 00:53:54 |
| Número de columnas de un TstringGrid | Yenifer | Varios | 1 | 13-08-2004 00:39:50 |
| colorear las celdas de un TstringGrid | mangel | OOP | 2 | 16-12-2003 17:22:47 |
|