unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
List:TList;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
tem1,tem2:String;
pP:Pchar;
b:Integer;
begin
tem1:='alex';
tem2:='---';
pP:=pChar(tem1+tem2);
List.Add(pP);
tem1:='yy';
tem2:='-dd';
pP:=pChar(tem1+tem2);
List.Add(pP);//i:=i+1;
for b:=0 to (List.Count-1) do
ListBox1.AddItem(PChar(List.Items[b]),nil);
ListBox1.AddItem(PChar('------------------'),nil);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
List:=TList.Create();
end;end.为何当按钮事件响应4次后,在ListBox里面就出现乱码了呢?!请高手指点!谢谢!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
List:TList;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
tem1,tem2:String;
pP:Pchar;
b:Integer;
begin
tem1:='alex';
tem2:='---';
pP:=pChar(tem1+tem2);
List.Add(pP);
tem1:='yy';
tem2:='-dd';
pP:=pChar(tem1+tem2);
List.Add(pP);//i:=i+1;
for b:=0 to (List.Count-1) do
ListBox1.AddItem(PChar(List.Items[b]),nil);
ListBox1.AddItem(PChar('------------------'),nil);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
List:=TList.Create();
end;end.为何当按钮事件响应4次后,在ListBox里面就出现乱码了呢?!请高手指点!谢谢!
你试试list.items.add
我觉得字符串用ansistring 不挺容易么
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
List:TList;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
tem1,tem2:String;
pP:Pchar;
b:Integer;
begin
List:=TList.Create();
tem1:='alex';
tem2:='---';
pP:=pChar(tem1+tem2);
List.Add(pP);
tem1:='yy';
tem2:='-dd';
pP:=pChar(tem1+tem2);
List.Add(pP);//i:=i+1;
for b:=0 to (List.Count-1) do
ListBox1.AddItem(PChar(List.Items[b]),nil);
ListBox1.AddItem(PChar('------------------'),nil);
List.Free;
end;end.
系统自动取出十六进制数的最后255位就不一定是正确的字符结果。
你不需要Create的么TList一般保存 指针对象的比较好
你string的就用TStringList
记住以后,这类东西在这里释放一下,
procedure TForm1.FormDestroy(Sender: TObject);
begin
List.Free;
end;
tem2:='---';
pP:=pChar(tem1+tem2);
List.Add(pP);
// 我问你 你这个pp是指向哪里的?? 后面的pChar(tem1+tem2) 又在哪里??
这是临时产生的一个对象 而且没有分配空间的 <你没有给它分配空间> 然后你把一个临时的地址放到List中去,那最后不乱了套才怪,。。程序没崩溃已经不错了因为List传入的是指针,根本没有COPY过程,而TStringList传入的是string,会有一个COPY过程,所以是安全的。。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
List:TList;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
tem1,tem2:String;
pP:Pchar;
b:Integer;
begin
List:=TList.Create();
tem1:='alex';
tem2:='---';
pP:=pChar(tem1+tem2);
List.Add(pP);
tem1:='yy';
tem2:='-dd';
pP:=pChar(tem1+tem2);
List.Add(pP);//i:=i+1;
for b:=0 to (List.Count-1) do
ListBox1.AddItem(PChar(List.Items[b]),nil);
ListBox1.AddItem(PChar('------------------'),nil);
List.Free;
end;end.
2. List中间保存的变量<指针>是LOCAL的你第一次加了2个进去 那么List中count为2
然后第二次又加了2个进去 List中count为4
现在你获取List中的指针所指向的内容时,你说第一次加入的两个地址现在会怎么样??这谁也说不清
因为 这些是编译器 & 系统给你分配的。。原来的地址可能有效 也可能没效。。所以可能出错。。你可以在这样if List = nil then
List := TList.Create;
// your code.... // end;
List.Free;
List := nil;...