大家帮我看看这个程序有什么问题(关于listbox)

喝口水 2007-09-29 06:00:25
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
IdSocketHandle,
bsSkinCtrls, StdCtrls, ComCtrls, bsSkinBoxCtrls, IdUDPClient, Psock,
NMDayTim;

type
TForm1 = class(TForm)
NMDayTime1: TNMDayTime;
IdUDPClient1: TIdUDPClient;
IdUDPServer1: TIdUDPServer;
bsSkinRichEdit1: TbsSkinRichEdit;
bsSkinRichEdit2: TbsSkinRichEdit;
bsSkinLabel1: TbsSkinLabel;
bsSkinButton1: TbsSkinButton;
bsSkinButton2: TbsSkinButton;
bsSkinButton3: TbsSkinButton;
ListBox1: TListBox;
Button1: TButton;
procedure bsSkinButton2Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
procedure bsSkinButton3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);

private
{ Private declarations }
procedure dealstr(str:string);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.dealstr(str:string); //处理数据函数
var
tempstr,user,IP,cmdstr:string;
len,i:integer;
begin
len:=length(str);
cmdstr:=copy(str,1,10);
tempstr:=copy(str,11,len-10);
if cmdstr= '##online## ' then
//如果为上线标志,则把上线计算机名和IP地址添加到listbox
begin
i:=pos( ': ',tempstr);
user:=copy(tempstr,1,i-1); //取得用户名
len:=length(tempstr);
IP:=COPY(tempstr,i+1,len-i); //取得IP地址
ListBox1.Items.AddObject(user,tobject(IP));//添加到listbox
end;
end;

procedure TForm1.bsSkinButton2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
data:TStringStream;
begin
data:=TStringStream.Create( ' ');
data.CopyFrom(adata,adata.Size);
dealstr(data.DataString);
data.Free;
end;

procedure TForm1.bsSkinButton3Click(Sender: TObject); //上线按钮单击事件
var
str:string;
begin
str:= '##online## '+self.IdUDPClient1.LocalName+ ': '+self.NMDayTime1.LocalIP;
self.IdUDPServer1.Active:=true;
self.IdUDPClient1.Host:= '255.255.255.255 ';
self.IdUDPClient1.Active:=true;
self.IdUDPClient1.Send(str); //如果上线则广播通知
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Items.Clear;
end;

procedure TForm1.ListBox1Click(Sender: TObject); //listbox的单击事件
//这儿光出问题,没法显示IP地址,帮忙看看
var
str:string;
begin
if ListBox1.ItemIndex < >-1 then
begin
str:=string(ListBox1.Items.Objects[ListBox1.itemindex]);
showmessage(str); //显示IP地址
end;
end;

end.
拜托了,编译是没有问题的,但就是当点击列表框时没法显示相应的IP地址,请帮忙看看
...全文
53 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
喝口水 2007-09-29
  • 打赏
  • 举报
回复
不过像下面的语句,却能顺利地执行,真不知我的程序到底哪有问题,请大家帮帮我
procedure TForm1.Button1Click(Sender: TObject);
var
ip:string;
begin
ip:='192.168.0.21';
listbox1.Items.AddObject('user1',tobject(ip));
ip:='192.168.0.22';
listbox1.Items.AddObject('user2',tobject(ip));
ip:='192.168.0.23';
listbox1.Items.AddObject('user3',tobject(ip));
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
if self.ListBox1.ItemIndex<>-1 then
showmessage(string(self.ListBox1.Items.Objects[listbox1.ItemIndex]));
end;
喝口水 2007-09-29
  • 打赏
  • 举报
回复
不行啊,楼上的,还是出现问题
hsmserver 2007-09-29
  • 打赏
  • 举报
回复
object实际就是指针
写的时候你可以这样写
var
sIp:^string;
begin
new(sIP);
sIP^:=IP;
ListBox1.Items.AddObject(user,tobject(sIP));


然后显示的时候

var
s:^string;

s:=pointer(listbox.items.objects(listbox.itemindex));
showmessage(s^);

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧