帮我看看下面程序的问题(100分)

  • 主题发起人 主题发起人 luke_hn
  • 开始时间 开始时间
L

luke_hn

Unregistered / Unconfirmed
GUEST, unregistred user!
unit ReceivedUnit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ExtCtrls, WinSkinData;

type
TReceivedMsgForm = class(TForm)
StatusBar: TStatusBar;
Notebook: TNotebook;
ReceivedMsgMemo: TMemo;
btnAnswer: TButton;
btnCancel: TButton;
btnPre: TButton;
btnNext: TButton;
SendMsgMemo: TMemo;
btnSend: TButton;
btnClose: TButton;
btnClear: TButton;
btnMsg: TButton;
SkinData1: TSkinData;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure SendMsgMemoKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure btnAnswerClick(Sender: TObject);
procedure btnClearClick(Sender: TObject);
procedure btnMsgClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnCancelClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnPreClick(Sender: TObject);
procedure btnNextClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MsgList:TStringList;
FromIP,RemoteComputerName:String;
MsgPointer:Integer;
end;

var
ReceivedMsgForm: TReceivedMsgForm;

implementation

uses main;

{$R *.DFM}

procedure TReceivedMsgForm.FormCreate(Sender: TObject);
begin
MsgList := TStringList.Create;
MsgPointer := 0;
end;

procedure TReceivedMsgForm.FormDestroy(Sender: TObject);
begin
MsgList.Free;
end;

procedure TReceivedMsgForm.SendMsgMemoKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
if (Key = 13) and (ssCtrl in Shift) then
btnSend.Click;
end;

procedure TReceivedMsgForm.btnAnswerClick(Sender: TObject);
begin
NoteBook.ActivePage:='Answer';
end;

procedure TReceivedMsgForm.btnClearClick(Sender: TObject);
begin
SendMsgMemo.Clear;
end;

procedure TReceivedMsgForm.btnMsgClick(Sender: TObject);
begin
NoteBook.ActivePage:='ReceivedMsg';
end;

procedure TReceivedMsgForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;

procedure TReceivedMsgForm.btnCancelClick(Sender: TObject);
begin
Close;
end;

procedure TReceivedMsgForm.btnCloseClick(Sender: TObject);
begin
if MsgPointer>=MsgList.Count-1 then
Close
else
NoteBook.ActivePage:='ReceivedMsg';
end;

procedure TReceivedMsgForm.btnSendClick(Sender: TObject);
var
Msg:String;
begin
with MainForm do
begin
Msg:=Format('%-15s%-6s',[LocalIP,'Chat']);
Msg := Msg+SendMsgMemo.Text;
SendMsg(FromIP,Msg);
end;

if MsgPointer>=MsgList.Count-1 then
Close
else
NoteBook.ActivePage:='ReceivedMsg';
end;

procedure TReceivedMsgForm.FormShow(Sender: TObject);
begin
StatusBar.Panels[0].Text:='IP:'+FromIP;
StatusBar.Panels[1].Text:='名称:'+RemoteComputerName;
if MsgList.Count = 0 then
btnMsg.Enabled := False
else
btnMsg.Enabled := True;

if MsgPointer <= 0 then
btnPre.Enabled := False
else
btnPre.Enabled := True;

if MsgPointer >= MsgList.Count - 1 then
btnNext.Enabled := False
else
btnNext.Enabled := True;

ShowMessage(IntToStr(MsgPointer));
ReceivedMsgMemo.Text := MsgList.Strings[MsgPointer]; //这句有问题
end;

procedure TReceivedMsgForm.btnPreClick(Sender: TObject);
begin
Dec(MsgPointer);
if MsgPointer <= 0 then
btnPre.Enabled := False;
if MsgPointer < MsgList.Count-1 then
btnNext.Enabled := True;
ReceivedMsgMemo.Text := MsgList.Strings[MsgPointer];
end;

procedure TReceivedMsgForm.btnNextClick(Sender: TObject);
begin
Inc(MsgPointer);
if MsgPointer > 0 then
btnPre.Enabled:=True;
if MsgPointer >= MsgList.Count-1 then
btnNext.Enabled := False;
ReceivedMsgMemo.Text := MsgList.Strings[MsgPointer];
end;

end.

 
报的什么错?
 
就是List Index out of bound(0)的错误。
 
你的MsgList Create后没有任何赋值操作,就直接执行MsgList[0]。。。。
所以出错。试试下面的语句就明白了:
procedure TForm1.Button1Click(Sender: TObject);
var s:tstringList;
begin
s:=tstringList.Create;
try
caption:=s[0];
finally
s.free;
end;
end;
 
试试:wfzha的方法吧。你的代码太长了。。。。晕
 
我按照wfzha的方法试了,还是不能解决这个问题.
 
下标(MsgPointer)越界。
 
怎么没有人能解决
 
楼主为什么不确定一下支运行时的出错位置,我觉得贴出来的代码要精要一点,
自个先试试
 
这个问题我也常碰到过,就是下标溢出.看了你了我想你是在编一个聊天室之类我的东东.代码太长我一时说不出问题在哪里,但我可以教你解决办法:
一、设断点跟踪程序,看看程序在哪里出错,什么情况下会出错。
二、跟据出错的情况,找到问题的原因对症下药。
例如下列代码也会出现上述的错误:
var
i: integer;
t: string;
list: Tstringlist;
begin
list := Tstringlist.Create;
list.Add('1');
list.Add('2');
list.Add('3');
try
for i := 0 to list.Count - 1 do
begin
if i = 0 then
list.Delete(i);
t := list.Strings;
end;
finally
list.Free;
end;
end;

想一想为什么?
 
ReceivedMsgMemo.Text := MsgList.Strings[MsgPointer]; //这句有问题

在FormShow事件中,MsgList.Count应该是0,即没有内容,访问它当然出错
如果要不出错,这样
if Assigned(MsgList) and (MsgList.Count > MsgPointer) thne
ReceivedMsgMemo.Text := MsgList.Strings[MsgPointer]; //这句有问题
 
接受答案了.
 
后退
顶部