Delphi - Como traigo un valor de un vector a un formulario con campos

 
Vista:

Como traigo un valor de un vector a un formulario con campos

Publicado por LUIS EDUARDO (1 intervención) el 06/02/2021 03:50:38
HOLA TENGO UN GRAN INCOVENIENTE RESULTA QUE MANEJO HACE POCO DELPHI , Y ESTABA HACIENDO UNA PRUEBA CON UN VECTOR DE TIPO RECORD O REGISTRO, Y EN UN FORMULARIO PRINCIPAL GRABO 2 CAMPOS LOS CUALES SE GUARDAN EN LA PRIMERA POSICION DEL VECTOR CON SUS DOS COMPONENTES EN ESTE CASO EJEMPLO NOMBRE Y CODIGO Y LOS AGREGO POR MEDIO DE UN BUTON Y LOS AGREGA BIEN.
EL HECHO ES QUE QUIERO QUE ESOS DATOS SE PUEDAN TOMAR EN OTRO FORMULARIO, Y LO ESTOY HACIENDO POR MEDIO DE OTRO BOTON EN EL FORMULARIO 2 Y NO ME APARECE, NO SE PORQUE LLEGAN VACIOS.
ACONTINUACION ENVIO LOS CODIGOS
FORMULARIO 1

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses Unit2, Unit3;


{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
var nom,ape:String;

begin
nom:=Edit1.Text;
ape:=Edit2.Text;

llenar(nom,ape);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form3.Show;

end;

end.



AQUI ESTA EL FORMULARIO 2
unit Unit3;

interface

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

type
TForm3 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form3: TForm3;


implementation

{$R *.dfm}

uses Unit2;
var arr: empl;

procedure TForm3.Button1Click(Sender: TObject);
begin
Label1.Caption:= arr.Nombre;
Label2.Caption:=arr.Codig;
end;

end.

Y AQUI ESTA LA UNIDAD DONDE ESTA EL VECTOR CON UN PROCEDIMIENTO DE CARGA
unit Unit2;


interface
uses Vcl.Dialogs;
type
empl =record
Nombre:String;
Codig:String;

end;

Temp= array [1..10] of empl;
procedure llenar(nom,cod:String);

implementation
var arr: Temp;


procedure llenar(nom,cod:String);
begin
arr[1].Nombre:=nom;
arr[1].Codig:=cod;
ShowMessage(arr[1].Nombre);
ShowMessage(arr[1].Codig);

end;

end.

LES AGRADEZCO LA AYUDA
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 65
Oro
Ha mantenido su posición en Delphi (en relación al último mes)
Gráfica de Delphi

Como traigo un valor de un vector a un formulario con campos

Publicado por Ezequiel (1244 intervenciones) el 08/02/2021 20:09:42
No te funciona por el simple hecho de que no es la misma variable que usas para guardar que para acceder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses unit2, unit3;
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var nom,ape:String;
 
begin
  nom:=Edit1.Text;
  ape:=Edit2.Text;
 
  llenar(nom,ape);
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  Form3.ShowModal;
end;
 
end.

En la unidad 2 debes de mover la declaración de la variable a la parte de arriba, para poder accederla desde la unidad 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  empl =record
    Nombre:String;
    Codig:String;
  end;
 
 
  Temp= array [1..10] of empl;
  procedure llenar(nom,cod:String);
 
var arr: Temp;
 
implementation
 
procedure llenar(nom,cod:String);
begin
  arr[1].Nombre:=nom;
  arr[1].Codig:=cod;
  ShowMessage(arr[1].Nombre);
  ShowMessage(arr[1].Codig);
end;
 
end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
unit Unit3;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm3 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form3: TForm3;
 
implementation
 
uses unit2;
 
{$R *.dfm}
 
procedure TForm3.Button1Click(Sender: TObject);
begin
Label1.Caption:= unit2.arr[1].Nombre;
Label2.Caption:=unit2.arr[1].Codig;
end;
 
end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar