请教ComboBox的问题?(20分)

  • 主题发起人 主题发起人 taoqg
  • 开始时间 开始时间
T

taoqg

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在输入字符时自动到items中检索呢,并显示items(已定位的)?
 
var
i: integer;

begin
i:=combobox.items.indexof(combobox.text);
if i >= 0 then combobox.itemindex := i;
 
Another_eYes:你这种方法等于将要检索的内容输入了,好象不能检索。
 
items怎样才能显示出来?
 
你用listbox和edit结合起来可以达到你的目的。
 
你的意思是随着你的输入不断定位?
我做过, 不过用一个combobox不能达到要求, 因为你不知道当前定位到哪条记录了. 用liuwen
的方法吧.
思路是在edit.onchange中不断检索edit.text, 匹配到listbox中第一条符合的记录上.
for i := 0 to listbox.items.count - 1 do
if copy(listbox.items, 1, length(edit.text))=edit.text then
begin
listbox.itemindex := i;
break;
end;
 
ComboBox本身就有此功能呀!!!
先点出下拉列表。
 
以前做过。


var inputstr:String='';
newst:Boolean=False;

...
procedure TJHForm.ComboBox1Change(Sender: TObject);
begin
if inputstr=ComboBox1.Text then
begin
ComboBox1.Text:=copy(inputstr,0,length(inputstr)-1);
ComboBox1.SelStart:=length(inputstr);
end;
inputstr:=ComboBox1.Text;
newst:=not DM.TStock.Locate('goodscode',inputstr,[loPartialKey]);
if not newst then begin
ComboBox1.ItemIndex:=DM.TStock.RecNo-1;
ComboBox1.SelStart:=length(inputstr);
ComboBox1.SelLength:=length(ComboBox1.Text)-length(inputstr);
Label2.Caption:=DM.TStockName.AsString;
DM.TStock.cancel;
end
else begin
Label2.Caption:='';
end;
end;

这是在table中找goodscode字段,并在label2上显示stockname,分太少,你自己改改吧。
 
unit NewDBLookupComboBox;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DBCtrls, DB;

type
TNewDBLookupComboBox = class(TDBLookupComboBox)
private
FPressed:Boolean;
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;

implementation

{ TNewDBLookupComboBox }

procedure TNewDBLookupComboBox.Paint;
var
W, X, Flags, FButtonWidth: Integer;
Text: string;
AAlignment: TAlignment;
Selected: Boolean;
R: TRect;
begin
FButtonWidth := GetSystemMetrics(SM_CXVSCROLL);
Canvas.Font := Font;
Canvas.Brush.Color := Color;
Selected := HasFocus and not ListVisible and
not (csPaintCopy in ControlState);
if Selected then
begin
Canvas.Font.Color := clHighlightText;
Canvas.Brush.Color := clHighlight;
end;
if (csPaintCopy in ControlState) and
(ListFields.Count>0) and
(TField(ListFields[0]) <> nil){ and
(Field.Lookup) }then
begin
Text := TField(ListFields[0]).DisplayText;
AAlignment := TField(ListFields[0]).Alignment;
end else
begin
if (csDesigning in ComponentState) and
(ListFields.Count>0) and
(not (TField(ListFields[0]) = nil)) then
Text := Name
else if (ListFields.Count>0) and
(not (TField(ListFields[0]) = nil)) then
Text := TField(ListFields[0]).Text
else
Text := '';
AAlignment := taLeftJustify;
end;
if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
W := ClientWidth - FButtonWidth;
X := 2;
case AAlignment of
taRightJustify: X := W - Canvas.TextWidth(Text) - 3;
taCenter: X := (W - Canvas.TextWidth(Text)) div 2;
end;
SetRect(R, 1, 1, W - 1, ClientHeight - 1);
if (BiDiMode = bdRightToLeft) then
begin
Inc(X, FButtonWidth);
Inc(R.Left, FButtonWidth);
R.Right := ClientWidth;
end;
if SysLocale.MiddleEast then TControlCanvas(Canvas).UpdateTextFlags;
if KeyValue=Null then Text:='';
Canvas.TextRect(R, X, 2, Text);
if Selected then Canvas.DrawFocusRect(R);
SetRect(R, W, 0, ClientWidth, ClientHeight);
if (BiDiMode = bdRightToLeft) then
begin
R.Left := 0;
R.Right:= FButtonWidth;
end;
if not ListActive then
Flags := DFCS_SCROLLCOMBOBOX or DFCS_INACTIVE
else if FPressed then
Flags := DFCS_SCROLLCOMBOBOX or DFCS_FLAT or DFCS_PUSHED
else
Flags := DFCS_SCROLLCOMBOBOX;
DrawFrameControl(Canvas.Handle, R, DFC_SCROLL, Flags);
end;

procedure Register;
begin
RegisterComponents('NoctWolf', [TNewDBLookupComboBox]);
end;

end.

设置
ListField:='name,code';
ListFieldIndex:=1;

这是按照code字段索引查找,并显示name。
分太少!如果只有这么一点分,以后就不回答你的问题了。
 
好象有这样一个控件的!
 
从TCustomCombox继承一个控件,
然后截取键盘消息,再到Items中查找,即可实现增量查找。:)
 
long time...........
 
后退
顶部