环境为Delphi 2010
当获取ComboBox.Items.Objects属性出现错误
'Access violation at address 004075E8 in module 'Test.exe'. Read of address 00000027'
调用语句为
ShowMessage(String(ComboBox1.Items.Objects[ComboBox1.ItemIndex]));
请教各位,哪里错了?
当获取ComboBox.Items.Objects属性出现错误
'Access violation at address 004075E8 in module 'Test.exe'. Read of address 00000027'
调用语句为
ShowMessage(String(ComboBox1.Items.Objects[ComboBox1.ItemIndex]));
请教各位,哪里错了?
解决方案 »
- 关于无法删除目录的一个问题
- delphi开发的3架构和.net开发b/s架构结合的问题。
- 主界面,背景是幅图,左边显示的几个功能模块按钮,点击后右边列出详细几个按钮,请问是什么控件做的?
- 刚接触delphi图形方面的内容,提个最菜的问题
- 如何为treeview中的每个item都加一个标志变量,在以后能够准确操作每个item?
- 同志们,哪里有pdf格式的delphi教程下载啊?
- 在Delphi4和Delphi6中如何读写XML
- 大家帮我看一下这段代码,(读别人的程序)看不懂:
- 谁能推荐一本专门讲COM技术的Delphi书,高分酬谢
- 请问如何把数字转成字符串?(有没有函数)
- Undeclared identifier: 'TIdTextEncoding'应该包含什么
- RTTI TGUID 不支持?
2.当前的ComboBox1.Items.Objects没有存入实例。
begin
ShowMessage(cbb1.Items.Objects[cbb1.ItemIndex].ClassName);
if cbb1.Items.Objects[cbb1.ItemIndex] is TButton then
ShowMessage(TButton(cbb1.Items.Objects[cbb1.ItemIndex]).name);
end;procedure TForm11.FormCreate(Sender: TObject);
begin
cbb1.Items.AddObject(btn1.Name, btn1);
cbb1.Items.AddObject(btn2.Name, btn2);
cbb1.ItemIndex := 0;
end;
begin
ShowMessage(String(ComboBox1.Items.Objects[ComboBox1.ItemIndex]));
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.AddItem('测试',TObject('1'));
ComboBox1.ItemIndex:=0;
end;
必须是对象
你如果非要放字符串, 可以这么干
unit Unit11;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm11 = class(TForm)
btn1: TButton;
cbb1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure cbb1Change(Sender: TObject);
private
strArr : array of string;
{ Private declarations }
public
{ Public declarations }
end;var
Form11: TForm11;implementation{$R *.dfm}procedure TForm11.cbb1Change(Sender: TObject);
begin
Caption := string(cbb1.Items.Objects[cbb1.ItemIndex]);
end;procedure TForm11.FormCreate(Sender: TObject);
var
I: Integer;
begin
SetLength(strArr, 10);
for I := 0 to 10 do
strArr[i] := 'aa' + IntToStr(i); for I := low(strArr) to High(strArr) do
cbb1.Items.AddObject(IntToStr(i), TObject(strArr[i]));
end;end.
strArr[i]不也是个字符串吗?
为什么要弄个字符串数组,字符串数组就是对象了?字符串就不是了?新手,还望赐教!
strArr : array of string;
//这样声明在当前单元内strArr的值始终有效(除非你修改了)
我一直认为这些变量内容被赋值到cbb1中去了,已经和strArr[]或者'1'无关了呢。所以也就是case5166说的生命周期了。
多谢了,结贴.
ComboBox1.AddItem('测试',TObject('1'));
改成
ComboBox1.AddItem('测试', TObject(PChar('1')));
就可以了
我试过你这个PChar('1'),是不行的。
Caption := pchar(cbb1.Items.Objects[cbb1.ItemIndex]);