DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: aaaarrrrrrrrrrrr
今日帖子: 58
在线用户: 5
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 snakewu (ss) ★☆☆☆☆ -
普通会员
2004/5/5 0:00:51
标题:
帮我看看这个功能 浏览:1209
加入我的收藏
楼主: 在一个TListBox里面有3016行items,我想从索引1开始,每隔一行赋给一个TMemo,比如TListBox内容为
1
a
2
b
3
c
我想把a,b,c赋给TMemo,最后分别保存到两个TXT文本中,但是为什莫我的代码不对呢:
procedure TForm1.Button1Click(Sender: TObject);
var i,j: integer;
begin
  j:=0;
  i:=0;
  while i<= 3015 do
  begin
    Memo1.Lines[j]:= ListBox1.Items[i];
    j:=j+1;
    i:=i+2;
  end;
  ListBox1.Items.SaveToFile('e:\n1.txt');
  Memo1.Lines.SaveToFile('e:\n2.txt');
end;
  

----------------------------------------------
ignore,Ctrl+Alt+Del,then begin;is die,but just not die。
作者:
男 sephil (NAILY Soft) ★☆☆☆☆ -
盒子中级会员
2004/5/5 0:04:06
1楼: procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
  for i := 0 to ListBox1.Items.Count - 1 do
  begin
    if i mod 2 <> 0 then Memo1.Lines.Add(ListBox1.Items[i]);
  end;
  ListBox1.Items.SaveToFile('e:\n1.txt');
  Memo1.Lines.SaveToFile('e:\n2.txt');
end;

你的代码其他地方应该也没什么问题
只是Memo1.Lines[j]:= ListBox1.Items[i];
   ~~~~~~~~~~~~~~~
这里用的不对,应该Memo1.Lines.Add(ListBox1.Items[i]);
否则会索引越界,因为Memo1.Lines.Count未必有j大
----------------------------------------------
Copyright 2008 ? NAILY Soft

Click here to redirect to my home
Click here to redirect to my blog
作者:
男 snakewu (ss) ★☆☆☆☆ -
普通会员
2004/5/5 0:07:02
2楼: 谢谢,sephil,
不好意思。。。。。。。后来又试了试,我弄了两个TListBox,倒是可以运行的,为什莫我的代码中使用TMemo倒是不行??
----------------------------------------------
ignore,Ctrl+Alt+Del,then begin;is die,but just not die。
作者:
男 sephil (NAILY Soft) ★☆☆☆☆ -
盒子中级会员
2004/5/5 0:07:42
3楼: 修改了一下
看上面的回答
----------------------------------------------
Copyright 2008 ? NAILY Soft

Click here to redirect to my home
Click here to redirect to my blog
作者:
男 snakewu (ss) ★☆☆☆☆ -
普通会员
2004/5/5 0:13:35
4楼: 请问,我用两个TListBox,然后也是ListBox1.Items[j] := ListBox2.Items[i];但是正常的,这是为什莫呢?
----------------------------------------------
ignore,Ctrl+Alt+Del,then begin;is die,but just not die。
作者:
男 sephil (NAILY Soft) ★☆☆☆☆ -
盒子中级会员
2004/5/5 0:28:31
5楼: 你前面可能有对ListBox1.Items初始化过吧
否则会出错的(索引越界)
----------------------------------------------
Copyright 2008 ? NAILY Soft

Click here to redirect to my home
Click here to redirect to my blog
作者:
男 snakewu (ss) ★☆☆☆☆ -
普通会员
2004/5/5 2:07:01
6楼: 明白了,应该使用Add方法是比较好的习惯,谢谢。
----------------------------------------------
ignore,Ctrl+Alt+Del,then begin;is die,but just not die。
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行70.3125毫秒 RSS