Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 24-10-2018
TavoBran TavoBran is offline
Miembro
NULL
 
Registrado: oct 2018
Posts: 13
Poder: 0
TavoBran Va por buen camino
Declarar un nombre a un edit creado en tiempo de ejecucion

Hola a todos...

lo que pasa es que quiero declararle un nombre a un TEdit que estoy creando en tiempo de ejcucion pero el problema es que los estoy creando con una matriz y no se como hacer para declararle todos los nombres a todos los TEdit...
les agradesco su ayuda.

Este es el codigo.

Código Delphi [-]

unit Ejecucion;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Menus;

type
  matriz = array of array of Integer;
  TfrmEjecucion = class(TForm)
    Button1: TButton;
    Edt: TEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    Button2: TButton;
    Button3: TButton;
    MainMenu1: TMainMenu;
    Volver1: TMenuItem;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Volver1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmEjecucion: TfrmEjecucion;
  NEdits : Integer;
  Edits: TEdit;
  m : matriz;

implementation

{$R *.dfm}

procedure TfrmEjecucion.Button1Click(Sender: TObject);
var
  c,f : Integer;
begin
  //Desicion para que no sea tan grande la matriz
  if StrToInt(Edt.Text) >= 11 then
  begin
    ShowMessage('El valor tiene que ser igual o menor a 10');
    Edt.Clear;
  end
  else
  begin
    //Establece el valor de la matriz y de los TEdit
    NEdits := StrToInt(Edt.Text);
    SetLength(m, StrToInt(Edt.Text),StrToInt(Edt.Text));
    Button1.Visible := False;
    Edt.Visible := False;
  end;
  // Creo y posiciono los Edits
  for c := 1 to NEdits do
  begin
    for f := 1 to NEdits do
    begin
      Edits := TEdit.Create(Self);
      Edits.Left := 21 * (c + 1);
      Edits.Top := 21 * (f + 1);
      Edits.Width := 20;
      Edits.Height := 20;
      Edits.Enabled := False;
      Edits.Parent := Panel1;
      Button3.Visible := True;
    end;
  end;
end;

procedure TfrmEjecucion.Volver1Click(Sender: TObject);
begin
    Close;
end;

end.
Responder Con Cita
  #2  
Antiguo 24-10-2018
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Poder: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
¿Para qué quieres ponerle un nombre? Normalmente, el nombre de un control se utiliza sólamente para referirnos a él en código, pero, dado que lo creas en tiempo de ejecución, no hace mucho sentido.

// Saludos
Responder Con Cita
  #3  
Antiguo 24-10-2018
TavoBran TavoBran is offline
Miembro
NULL
 
Registrado: oct 2018
Posts: 13
Poder: 0
TavoBran Va por buen camino
roman lo que pasa es que después necesito llamar a todos los Edit creados para así llenarlos con una matriz y la única forma que se me ocurría era llamarlos por el nombre.
Responder Con Cita
  #4  
Antiguo 24-10-2018
juniorSoft juniorSoft is offline
Miembro
 
Registrado: abr 2005
Posts: 178
Poder: 20
juniorSoft Va por buen camino
Hola,

Probaste hacer esto:

Código:
for f := 1 to NEdits do
    begin
      Edits := TEdit.Create(Self);
      Edits.Left := 21 * (c + 1);
      Edits.Top := 21 * (f + 1);
      Edits.Width := 20;
      Edits.Height := 20;
      Edits.Enabled := False;
      Edits.Parent := Panel1;
      Edits.name := 'Edit'+f.ToString;
      Button3.Visible := True;
    end;
Responder Con Cita
  #5  
Antiguo 24-10-2018
TavoBran TavoBran is offline
Miembro
NULL
 
Registrado: oct 2018
Posts: 13
Poder: 0
TavoBran Va por buen camino
juniorSoft si claro ya lo intente
Responder Con Cita
  #6  
Antiguo 24-10-2018
TavoBran TavoBran is offline
Miembro
NULL
 
Registrado: oct 2018
Posts: 13
Poder: 0
TavoBran Va por buen camino
Amigo muchas gracias por su ayuda ya busque y pude solucionar mi error

les dejo mi solucion y me dicen que les parece

Código Delphi [-]

unit Ejecucion;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Menus;

type
  matriz = array of array of Integer;
  TfrmEjecucion = class(TForm)
    Button1: TButton;
    Edt: TEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    Button2: TButton;
    Button3: TButton;
    MainMenu1: TMainMenu;
    Volver1: TMenuItem;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Volver1Click(Sender: TObject);
  private
    { Private declarations }
  public
    NEdits : Integer;
    Edits: TEdit;
    m : matriz;
    f,c : Integer;
    con : Integer;
  end;

var
  frmEjecucion: TfrmEjecucion;


implementation

{$R *.dfm}

procedure TfrmEjecucion.Button1Click(Sender: TObject);
var
  i,j : Integer;
begin
  if Trim(Edt.Text) = '' then
  begin
    ShowMessage('Debe escribir un número');
    Exit;
  end;
  //Desicion para que no sea tan grande la matriz
  if StrToInt(Edt.Text) >= 11 then
  begin
    ShowMessage('El valor tiene que ser igual o menor a 10');
    Edt.Clear;
  end
  else
  begin
    //Establece el valor de la matriz y de los TEdit
    c := StrToInt(Edt.Text);
    f := StrToInt(Edt.Text);
    SetLength(m, StrToInt(Edt.Text),StrToInt(Edt.Text));
    Button1.Visible := False;
    Edt.Visible := False;
  end;
  //Creo y posiciono los Edits
 for i := 1 to c do
  begin
    for j := 1 to f do
    begin
      Edits := TEdit.Create(Self);
      Edits.Name := 'Edit' + IntToStr(i) + IntToStr(j);
      Edits.Text := '';
      Edits.Top := 21 * (i + 1);
      Edits.Left := 21 * (j + 1);
      Edits.Width := 20;
      Edits.Height := 20;
      Edits.AutoSize := False;
      Edits.Enabled := False;
      Edits.Parent := Panel1;
      Button3.Visible := True;
    end;
  end;
end;


procedure TfrmEjecucion.Button2Click(Sender: TObject);
begin
  m := nil;
end;

procedure TfrmEjecucion.Button3Click(Sender: TObject);
var
  i,j,c : Integer;
  Comp : TComponent;
begin
  //Agrega numeros a la matriz aleatoriamente.
  c := StrToInt(Edt.Text);
  For i := 0 to c do
  begin
    for j := 0 to c do
    begin
       Comp := FindComponent ('Edit' + IntToStr(i)+ IntToStr(j));
       if Assigned (Comp) then
       begin
         (Comp as TEdit).Text := IntToStr(Random(100));
       end;
    end;
  end;

end;

procedure TfrmEjecucion.Volver1Click(Sender: TObject);
begin
    Close;
end;

end.
Responder Con Cita
  #7  
Antiguo 24-10-2018
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Poder: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
El problema es que, para encontrar un control por su nombre utilizas FindComponent. Pero este método, a su vez, es un ciclo que recorre todos los componentes del formulario para encontrar una coincidencia de nombre. Entonces, si tu rejilla es, digamos, de 10x10, estarías recorriendo el formulario 100 veces.

Dado que estás creando los controles por código, fácilmente puedes declarar una matriz de Edits:

Código Delphi [-]
var
  Edits: array[1..10,1..10] of TEdit;

y llenarla con los controles que creas. Posteriormente será mucho más fácil utilizarlos por sus coordenadas:

Código Delphi [-]
Edits[2, 8].Text := 'Algo';

// Saludos
Responder Con Cita
  #8  
Antiguo 24-10-2018
TavoBran TavoBran is offline
Miembro
NULL
 
Registrado: oct 2018
Posts: 13
Poder: 0
TavoBran Va por buen camino
Si ramon yo lo declaro así solo que es dinámico y defino la matriz a través de un TEdit y como es una matriz cuadrada no hay problema,

Declaro la matriz asi:
Código Delphi [-]

matriz = array of array of Integer;

y defino el valor asi:

Código Delphi [-]

SetLength(m, StrToInt(Edt.Text),StrToInt(Edt.Text));
Responder Con Cita
  #9  
Antiguo 24-10-2018
TavoBran TavoBran is offline
Miembro
NULL
 
Registrado: oct 2018
Posts: 13
Poder: 0
TavoBran Va por buen camino
Si ramon yo lo declaro así solo que es dinámico y defino la matriz a través de un TEdit y como es una matriz cuadrada no hay problema,

Declaro la matriz asi:
Código Delphi [-]

matriz = array of array of Integer;

y defino el valor asi:

Código Delphi [-]

SetLength(m, StrToInt(Edt.Text),StrToInt(Edt.Text));
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Asignar un evento a un componente creado en tiempo de ejecucion FGarcia OOP 7 12-09-2014 23:27:35
Evento en BitBtn creado en tiempo de ejecución newtron OOP 2 10-05-2012 16:54:14
eventos de PageControl creado en tiempo de ejecucion kaozz OOP 5 17-07-2007 15:02:10
Mostrar un texto creado en tiempo de ejecución FunBit Varios 1 10-10-2005 13:23:39
saber el nombre de un control creado en tiempo de ejecucion xxxlincexxx Varios 10 10-08-2003 23:45:54


La franja horaria es GMT +2. Ahora son las 13:22:01.


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
Copyright 1996-2007 Club Delphi