Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Rellenar una fila y columna dada en un TStringGrid (https://www.clubdelphi.com/foros/showthread.php?t=88558)

ermac 22-06-2015 23:31:45

Rellenar una fila y columna dada en un TStringGrid
 
Tengo q hacer un programa en el cual rellene la fila y columna ingresada por el usuario

Mi codigo es el siguiente:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ExtCtrls;

type
  TRellenar = class(TForm)
    Tabla: TStringGrid;
    Label1: TLabel;
    Fila: TEdit;
    Rellenar: TButton;
    Label2: TLabel;
    Columna: TEdit;
    Panel: TPanel;
    procedure RellenarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Rellenar: TRellenar;

implementation

{$R *.dfm}

procedure TRellenar.RellenarClick(Sender: TObject);
var
i,j,m,n:integer;

begin
  i:= strtoint (Fila.Text);
  j:= strtoint (columna.text);
  If (i=0) or (j=0) then
    ShowMessage ('Error')
  else
  begin
    for m:=0 to Tabla.ColCount-1 do
    begin
      for n:=0 to Tabla.RowCount-1 do
        begin
          Tabla.Cells[m,0]:= '*';
          Tabla.Cells[0,n]:= '*';
        end;
    end;
  end;
end;

end.
Necesitaria saber como puedo hacer para q la fila y columna q ingresan en los edits me los relacione con los 2 for de abajo para q se rellene esa fila y columna

gracias

ecfisa 23-06-2015 01:48:48

Hola ermac.

Hay muchas variantes para lo que buscas hacer, te pongo un ejemplo simple pero bastante flexible :
Código Delphi [-]
// Permite ingresar solo números
procedure WCOnlyNumber(WC: TWinControl);
begin
  SetWindowLong(WC.Handle, GWL_STYLE,
    GetWindowLong(WC.Handle, GWL_STYLE) + ES_NUMBER);
end;

// Inicializar
procedure TForm1.FormCreate(Sender: TObject);
begin
  WCOnlyNumber(edColDes);   // edit columna desde
  WCOnlyNumber(edColHas);   // edit columna hasta
  WCOnlyNumber(edFilaDes);  // edit fila desde
  WCOnlyNumber(edFilaHas);  // edit fila hasta
end;

// Rellenar
procedure RellenaGrid(SG: TStringGrid; const cDes, fDes, cHas, fHas: Integer;
const Character: Char);
var
  f, c: Integer;
begin
  if (cDes < 0)or(fDes < 0)or(cHas >= SG.ColCount)or(fHas >= SG.RowCount) or
     (cDes > cHas) or (fDes > fHas) then
    raise Exception.Create('Error en rango');
  for c:= cDes to cHas do
    for f:= fDes to fHas do
      SG.Cells[c,f] := Character;
end;

// llamada ejemplo:
procedure TForm1.btnRellenaClick(Sender: TObject);
begin
  RellenaGrid(StringGrid1, StrToInt(edColDes.Text), StrToInt(edFilaDes.Text),
              StrToInt(edColHas.Text), StrToInt(edFilaHas.Text), '>')
end;

Saludos :)

AgustinOrtu 23-06-2015 02:15:29

Daniel,
Cita:

Empezado por ecfisa (Mensaje 493579)
Código Delphi [-]
// Permite ingresar solo números
procedure WCOnlyNumber(WC: TWinControl);
begin
  SetWindowLong(WC.Handle, GWL_STYLE,
    GetWindowLong(WC.Handle, GWL_STYLE) + ES_NUMBER);
end;

Aprovecho que estas de oferta y me llevo uno de estos :D

ermac 23-06-2015 20:26:22

ya encontre el error, en Tabla.cells estaba asignando m y n pero despues la fila y la columna la deje constante con 0.. por eso no me escribia en la fila o columna q ingresaban.. solo tenia q reemplazar el 0 con i y con j y se solucionaba :)


La franja horaria es GMT +2. Ahora son las 18:34:40.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi