如何给listbox或combobox的每一个选项添加一个可以读写的value属性,以便在选择一个项目时获得一个值 ( 积分: 100 )

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

lssgj

Unregistered / Unconfirmed
GUEST, unregistred user!
如何给listbox或combobox的每一个选项添加一个可以读写的value属性,以便在选择一个项目时获得一个值
 
如何给listbox或combobox的每一个选项添加一个可以读写的value属性,以便在选择一个项目时获得一个值
 
在往listbox or combobox每一个Item赋值的时候,同时添一个指针,这个指针指向一个记录
这样,选择每个item的时候,就可以获取这个指针的所有信息.
 
楼上说的对呀
 
那就是用listbox.items.addobject()或combobox.items.addobject()来做了~~~

照着下面的做一下看看:
//定义一个类,来存取value的值
type
TYourObject = class
Value: string;
end;
//加入combobox.items
procedure TForm1.FormCreate(Sender: TObject);
var
obj: TYourObject;
s: string;
i: integer;
begin
for i := 1 to 10 do
begin
s := inttostr(i);
obj := TYourObject.Create;
obj.Value := s; //写入value
combobox1.Items.AddObject('第'+s+'个选项',obj);
end;
end;
//读出加入的值
procedure TForm1.BitBtn1Click(Sender: TObject);
var
obj: TYourObject;
value: string;
begin
if combobox1.itemindex < 0 then exit;
obj := TYourObject(combobox1.Items.Objects[combobox1.ItemIndex]) ;
value := obj.Value; //读取值
showmessage(value); //显示值,核对是否正确
end;
 
有没有做成控件的?
 
晕,你把这些拿到组件向导里写就可以了啊~~~
还要做好的吗??很简单~~~
解决方案二个:
1、从ListBox或者TComboBox继承一个,
然后你只需要添加一个property ValueList: TStrings read FValueList write FValueList;就可以了,然后对应着各个选项输入适当的值
然后访问这个ValueList[]中的对应值就可以了
2、(较复杂些)从ListBox或者TComboBox继承一个,添加一个property Value: variant read GetValue;然后再自己做一个属性编辑器,例如在左边输入一个项目值,右边就输入一个对应的value
 
定义一个tstringlist
postidlist:tstringlist;
窗体创建的时候
postidlist:=tstringlist.create;
窗体关闭的时候
freeandnil(postidlist);

procedure Tfrm_post.fillpost;
begin
ADOQuery1.close;
ADOQuery1.SQL.clear;
ADOQuery1.SQL.Add('select id,postname from tbl_postinfo order by postname');
ADOQuery1.Open;
postidlist.Clear;
listbox1.Items.Clear;
while not ADOQuery1.Eof do
begin
postidlist.Add(ADOQuery1.fieldbyname('id').AsString);
listbox1.Items.Add(trim(ADOQuery1.fieldbyname('postname').asstring));
ADOQuery1.Next;
end;
end;
应用的时候
procedure Tfrm_post.listbox1Click(Sender: TObject);
var
postid:string;
begin
if listbox1.itemindex<0 then exit;
postid:=postidlist[listbox1.itemindex];
end;
 
谢谢各位
 
已解决:unit VALComboBox;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Menus, Dialogs, StdCtrls;
type
TValComboBox = class(TComboBox)
private
FValue: PString;
FValues: TStrings;
FOnChange: TNotifyEvent;
function GetValue: string;
function GetButtonValue(Index: Integer): string;
procedure SetValue(const Value: string);
procedure SetValues(Value: TStrings);
protected
procedure Change; dynamic;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Value: string read GetValue write SetValue;
property ItemIndex;
published
property Values: TStrings read FValues write SetValues;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;

procedure Register;
implementation

constructor TValComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FValue := NullStr;
FValues := TStringList.Create;
style := csDropDownList;
end;

destructor TValComboBox.Destroy;
begin
DisposeStr (FValue);
FValues.Free;
inherited Destroy;
end;

procedure TValComboBox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
end;

function TValComboBox.GetValue : string;
begin
result:=values[itemindex];
end;

function TValComboBox.GetButtonValue(Index: Integer): string;
begin
if (Index < FValues.Count) and (FValues[Index] <> '') then
Result := FValues[Index]
else if (Index < Items.Count) then
Result := Items[Index]
else
Result := '';
end;

procedure TValComboBox.SetValue (const Value: string);
var
I : Integer;
begin
AssignStr(FValue, Value);
if (ItemIndex < 0) or (GetButtonValue(ItemIndex) <> Value) then
begin
if (ItemIndex >= 0) then ItemIndex := -1;
for I := 0 to Items.Count - 1 do
begin
if GetButtonValue(I) = Value then
begin
ItemIndex := I;
break;
end;
end;
Change;
end;
end;


procedure TValComboBox.SetValues(Value: TStrings);
begin
FValues.Assign(Value);
end;

procedure TValComboBox.Change;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure Register;
begin
RegisterComponents('mycomponent', [TValComboBox]);
end;
end.
 
后退
顶部