0
我有一個不同的WinForm的DataGridView,我試圖從main.pas訪問DataGridView,但我不能。如何從main.pas訪問DataGridView?
我得到以下錯誤:「MonoPro.UnitForm」 不表達包含 「dataGridView1」 的定義, 「fr.dataGridView1」
下面是代碼:
Form1.pas與UnitForm Winform with DataGridView
namespace MonoPro.Units;
interface
uses
System.Drawing,
System.Collections,
System.Windows.Forms,
System.ComponentModel;
type
UnitForm = partial class
{$REGION Windows Form Designer generated fields}
private
var components: System.ComponentModel.Container := nil;
dataGridView1: System.Windows.Forms.DataGridView;
Column3: System.Windows.Forms.DataGridViewTextBoxColumn;
Column2: System.Windows.Forms.DataGridViewTextBoxColumn;
Column1: System.Windows.Forms.DataGridViewTextBoxColumn;
method InitializeComponent;
{$ENDREGION}
end;
implementation
{$REGION Windows Form Designer generated code}
method UnitForm.InitializeComponent;
begin
self.dataGridView1 := new System.Windows.Forms.DataGridView();
self.Column1 := new System.Windows.Forms.DataGridViewTextBoxColumn();
self.Column2 := new System.Windows.Forms.DataGridViewTextBoxColumn();
self.Column3 := new System.Windows.Forms.DataGridViewTextBoxColumn();
(self.dataGridView1 as System.ComponentModel.ISupportInitialize).BeginInit();
self.SuspendLayout();
//
// dataGridView1
//
self.dataGridView1.BackgroundColor := System.Drawing.SystemColors.ControlLightLight;
self.dataGridView1.ColumnHeadersHeightSizeMode := System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
self.dataGridView1.Columns.AddRange(array of System.Windows.Forms.DataGridViewColumn([self.Column1,
self.Column2,
self.Column3]));
self.dataGridView1.Location := new System.Drawing.Point(12, 12);
self.dataGridView1.Name := 'dataGridView1';
self.dataGridView1.RowHeadersVisible := false;
self.dataGridView1.Size := new System.Drawing.Size(795, 221);
self.dataGridView1.TabIndex := 0;
//
// Column1
//
self.Column1.HeaderText := 'Column1';
self.Column1.Name := 'Column1';
//
// Column2
//
self.Column2.HeaderText := 'Column2';
self.Column2.Name := 'Column2';
//
// Column3
//
self.Column3.HeaderText := 'Column3';
self.Column3.Name := 'Column3';
//
// UnitForm
//
self.ClientSize := new System.Drawing.Size(819, 245);
self.Controls.Add(self.dataGridView1);
self.Name := 'UnitForm';
self.Text := 'Form1';
(self.dataGridView1 as System.ComponentModel.ISupportInitialize).EndInit();
self.ResumeLayout(false);
end;
{$ENDREGION}
end.
這是Main.pas。我想從Form1.pas(UnitForm)訪問dataGridView1。
namespace MonoPro;
interface
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Windows.Forms,
System.ComponentModel,
System.Threading,
System.IO.Ports,
MonoPro.*;
type
/// <summary>
/// Summary description for MainForm.
/// </summary>
MainForm = partial class(System.Windows.Forms.Form)
private
method SignalBtn_Click(sender: System.Object; e: System.EventArgs);
method CommBtn_Click(sender: System.Object; e: System.EventArgs);
method button1_Click(sender: System.Object; e: System.EventArgs);
method button2_Click(sender: System.Object; e: System.EventArgs);
method button4_Click(sender: System.Object; e: System.EventArgs);
method button5_Click(sender: System.Object; e: System.EventArgs);
method ShutdownBtn_Click(sender: System.Object; e: System.EventArgs);
method MySerialData(sender: System.Object; e:SerialDataReceivedEventArgs);
protected
method Dispose(disposing: Boolean); override;
public
RX:Array[0..5] of byte;
TX:Array[0..6] of byte;
serialPort1:System.IO.Ports.SerialPort;
thr:Thread;
stoploop:Boolean;
mcommand:Byte;
thechannel:Integer;
fr : UnitForm;
constructor;
method FillTable;
procedure mythread;
end;
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
mcommand:=$AA;
thechannel:=$01;
stoploop:=false;
thr:=nil;
fr := new UnitForm;
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
//if assigned(components) then
// components.Dispose();
//
// TODO: Add custom disposition code here
//
SerialPort1.Close;
stoploop:=true;
thr.Abort;
end;
inherited Dispose(disposing);
end;
{$ENDREGION}
method MainForm.FillTable;
begin
fr.dataGridView1; //Here is my problem. With this line of code I am trying to see if the dataGridView1 belongs to fr winform.
end;
method MainForm.MySerialData(sender: Object; e: SerialDataReceivedEventArgs);
begin
if not SerialPort1.IsOpen then Exit;
try
SerialPort1.Read(RX,0,5);
FillTable;
except on ex: exception do
begin
exit;
end;
end;
end;
procedure MainForm.mythread;
begin
while true do
begin
TX[0]:=$FF;
TX[1]:=$01;
TX[2]:=$01;
TX[3]:=thechannel;
TX[4]:=mcommand;
TX[5]:=(TX[2] xor TX[3] xor TX[4]);
SerialPort1.Write(TX,0,6);
while SerialPort1.BytesToWrite>0 do;
Thread.Sleep(100);
if (stoploop) then
break;
end;
end;
method MainForm.CommBtn_Click(sender: System.Object; e: System.EventArgs);
begin
{$IFDEF CLR}
if SerialPort1 = nil then
SerialPort1 := new System.Io.Ports.SerialPort();
SerialPort1.Close;
SerialPort1 := new System.Io.Ports.SerialPort();
SerialPort1.BaudRate:=19200;
SerialPort1.DataBits:=8;
SerialPort1.DtrEnable:=true;
SerialPort1.Parity:=System.IO.Ports.Parity.Even;
SerialPort1.PortName:='COM1';//'/dev/ttyS0';
SerialPort1.ReadBufferSize:=4096;
SerialPort1.ReadTimeout:=1000;
SerialPort1.RtsEnable:=true;
SerialPort1.StopBits:=System.IO.Ports.StopBits.One;
SerialPort1.WriteTimeout:=1000;
SerialPort1.DataReceived += new SerialDataReceivedEventHandler(MySerialData);
SerialPort1.Open;
thr:= new Thread(@mythread);
thr.Start;
{$ENDIF}
end;
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
begin
thechannel:=$04;
mcommand:=$A1;
end;
method MainForm.button2_Click(sender: System.Object; e: System.EventArgs);
begin
thechannel:=$04;
mcommand:=$A2;
end;
method MainForm.button4_Click(sender: System.Object; e: System.EventArgs);
begin
thechannel:=$04;
mcommand:=$A4;
end;
method MainForm.button5_Click(sender: System.Object; e: System.EventArgs);
begin
thechannel:=$04;
mcommand:=$A8;
end;
任何想法?
謝謝
DataGridView沒有可見性屬性。 – ThN
它在「設計」 - >「修飾符」下的屬性中。默認ist保護,將其更改爲公開狀態以使該屬性可公開訪問。 –
謝謝你,塞巴斯蒂安,爲你的所有幫助。我發現它並將其改爲公共,並且它可以工作。我正在學習Delphi Prism。 – ThN