我的工程有一个窗体,这个窗体里面有一个ListBox1控件,我声明了一个静态字符数组MyArray其值包含A-Z。我想在创建该窗体的时候,把数组中的元素赋值到ListBox1,一行一个元素。
我的Unit9.pas文件如下:
=======================================================================
unit Unit9;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm9 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form9: TForm9;
MyArray: array['A'..'Z'] of Char;
implementation
{$R *.DFM}
procedure TForm9.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Length(MyArray) do
ListBox1.Items[i] := MyArray[i];//该句出现报错
end;
end.
======================================================================
在运行的时候出现这样的错误提示:
======================================================================
[Error] Unit9.pas(32): Incompatible types: 'Char' and 'Integer'
======================================================================
请问我到底错在什么地方了,如何更正呢?这个错误涉及那些方面?
我的Unit9.pas文件如下:
=======================================================================
unit Unit9;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm9 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form9: TForm9;
MyArray: array['A'..'Z'] of Char;
implementation
{$R *.DFM}
procedure TForm9.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Length(MyArray) do
ListBox1.Items[i] := MyArray[i];//该句出现报错
end;
end.
======================================================================
在运行的时候出现这样的错误提示:
======================================================================
[Error] Unit9.pas(32): Incompatible types: 'Char' and 'Integer'
======================================================================
请问我到底错在什么地方了,如何更正呢?这个错误涉及那些方面?
错了,下面是整形的
MyArray: array[0..1000] of Char; 或 MyArray: array of Char;
然后在给数组负值就行了
=========================================================================
静态数组类型以下面的格式声明:
array[indexType1, ..., indexTypen] of baseType
这里,每个indexType 是有序类型并且范围不超过2G。因为indexType 是数组的索引,所以,数组包含
的元素个数由indexType 的范围限定。在实际应用中,indexType 通常是整数子界类型。
=========================================================================
2.同时引用Delphi5里面的帮助文件如下所示:
=========================================================================
Static array types are denoted by constructions of the formarray[indexType1, ..., indexTypen] of baseTypewhere each 'indexType' is an ordinal type whose range does not exceed 2GB. Since the 'indexTypes' index the array, the number of elements an array can hold is limited by the product of the sizes of the indexTypes. In practice, 'indexTypes' are usually integer subranges.
=========================================================================
MyArray: array[1..26] of Char;
然后赋值
for i := 0,j := 'a' to Length(MyArray),'z' do
MyArray[i] := IntToStr(j);
var
i,j:integer;
begin
for i:=0 to 25 do
begin
j:=97+i;
myarray[i]:=chr(j);
end;
end;
=================================================================
unit Unit9;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm9 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form9: TForm9;
MyArray: array[0..25] of Char;implementation{$R *.DFM}procedure TForm9.FormCreate(Sender: TObject);
var
i,j: Integer;
begin
for i := 0 to 25 do
begin
j := i + 97; //ASCII 'a' = #97
MyArray[i] := Chr(j);
ListBox1.Items[i] := MyArray[i];
end;end;
end.
=================================================================终于运行出来。谢谢hellolongbin(一个人) 的打烂沙锅解答到底,我从这个示例,明白了使用Delphi应该离不开熟记ASCII码吧。Delphi里面有没有获得每个键的ASCII码的函数?如果没有的话,那么我该得知104键盘上面的ASCII码呢?
这里的#97在程序里面是代表什么意思?
#97就是指的小写字母'a'的ASCII码,这样的赋值就表示把'a'赋给了myarray[0],类似的例子还有我们showmessage时如果提示信息太长一般会在中间加#10#13用来换行,这里的#10和#13分别代表的换行和回车的ASCII码,比如
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage('这个句子太长了一句话显示'+#10#13+'下来不好看我来给它换下行');
end;
如果单从显示上来看#10和#13只用一个也可以起到换行的效果。
procedure TForm1.Button1Click(Sender: TObject);
var
strtmp:string;
chrtmp:array [0..2] of char;
begin
strtmp:=InputBox('提示','请输入要查询的键值','');
strcopy(chrtmp,pchar(strtmp));
if length(strtmp)>0 then
showmessage(strtmp+'的ASCII码为'+inttostr(ord(chrtmp[0])));
end;