一个UDP小聊天程序,提示有错,欢迎来访。 ( 积分: 50 )

  • 主题发起人 主题发起人 701004158
  • 开始时间 开始时间
7

701004158

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient, ImgList, ExtCtrls, ComCtrls, StdCtrls,IdSocketHandle;
const receivetimeout=5000;
type
tcommblock=record
sendname,msg:string[100];
nowstr:string[20];
end;
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
remoteip: TEdit;
remotename: TEdit;
localname: TEdit;
nickname: TEdit;
Label6: TLabel;
inputbox: TEdit;
Button2: TButton;
ImageList1: TImageList;
IdUDPClient1: TIdUDPClient;
IdUDPServer1: TIdUDPServer;
chatlog: TRichEdit;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure fontchange(comm:tcommblock;devicename:trichedit);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
idudpclient1.Host :=remoteip.Text ;
idudpclient1.Port :=strtoint(remotename.Text );
idudpclient1.ReceiveTimeout := receivetimeout;
idudpserver1.DefaultPort :=strtoint(localname.Text );
idudpserver1.Active:=true;
button2.Enabled :=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
comm:tcommblock;
begin
comm.sendname:=nickname.text;
comm.msg:=inputbox.text;
comm.nowstr:=datetimetostr(Now);
idudpclient1.sendbuffer(comm,sizeof(comm));
fontchange(comm,chatlog);
end;
procedure tform1.fontchange(comm:tcommblock;devicename:trichedit);
begin
with tform1 do
with devicename do
begin
chatlog.Lines.Add('('+comm.nowstr+')'+comm.sendname+':'+comm.msg);
--------------------------------------
[red]编译时,有instancce variable 'chatlog'inaccessiable here,错误!请帮忙[/red---------------------------------------
end;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
comm:tcommblock;
begin
adata.ReadBuffer(comm,adata.Size);
fontchange(comm,chatlog);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
close;
end;
end.
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient, ImgList, ExtCtrls, ComCtrls, StdCtrls,IdSocketHandle;
const receivetimeout=5000;
type
tcommblock=record
sendname,msg:string[100];
nowstr:string[20];
end;
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
remoteip: TEdit;
remotename: TEdit;
localname: TEdit;
nickname: TEdit;
Label6: TLabel;
inputbox: TEdit;
Button2: TButton;
ImageList1: TImageList;
IdUDPClient1: TIdUDPClient;
IdUDPServer1: TIdUDPServer;
chatlog: TRichEdit;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure fontchange(comm:tcommblock;devicename:trichedit);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
idudpclient1.Host :=remoteip.Text ;
idudpclient1.Port :=strtoint(remotename.Text );
idudpclient1.ReceiveTimeout := receivetimeout;
idudpserver1.DefaultPort :=strtoint(localname.Text );
idudpserver1.Active:=true;
button2.Enabled :=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
comm:tcommblock;
begin
comm.sendname:=nickname.text;
comm.msg:=inputbox.text;
comm.nowstr:=datetimetostr(Now);
idudpclient1.sendbuffer(comm,sizeof(comm));
fontchange(comm,chatlog);
end;
procedure tform1.fontchange(comm:tcommblock;devicename:trichedit);
begin
with tform1 do
with devicename do
begin
chatlog.Lines.Add('('+comm.nowstr+')'+comm.sendname+':'+comm.msg);
--------------------------------------
[red]编译时,有instancce variable 'chatlog'inaccessiable here,错误!请帮忙[/red---------------------------------------
end;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
comm:tcommblock;
begin
adata.ReadBuffer(comm,adata.Size);
fontchange(comm,chatlog);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
close;
end;
end.
 
chatlog在哪定义的? 是个什么东西?
看起来象个TMEMO
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
remoteip: TEdit;
remotename: TEdit;
localname: TEdit;
nickname: TEdit;
Label6: TLabel;
inputbox: TEdit;
Button2: TButton;
ImageList1: TImageList;
IdUDPClient1: TIdUDPClient;
IdUDPServer1: TIdUDPServer;
chatlog: TRichEdit;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure fontchange(comm:tcommblock;devicename:trichedit);
{ Public declarations }
end;
改成
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
remoteip: TEdit;
remotename: TEdit;
localname: TEdit;
nickname: TEdit;
Label6: TLabel;
inputbox: TEdit;
Button2: TButton;
ImageList1: TImageList;
IdUDPClient1: TIdUDPClient;
IdUDPServer1: TIdUDPServer;
chatlog: TRichEdit;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure fontchange(comm:tcommblock;devicename:trichedit);
private
{ Private declarations }
public
{ Public declarations }
end;
 
改成Form1.chatlog.Lines.Add('('+comm.nowstr+')'+comm.sendname+':'+comm.msg);
 
comm:tcommblock; 应该是全局变量

放到
var
Form1: TForm1;
。。。。。。。。。。←
implementation
 
后退
顶部