1

我的表单上有一个 TComboBox 组件(comboxCountry)。这是 TComboBox 中的项目。

第 1 项:“新加坡 SG”

第 2 项:“印度 IND”

第 3 项:'Australia AUS' 等等等等等等..

当组合框值更改时,我希望 combboxCounty.Text 仅显示国家代码而不是项目列表中的整个字符串。例如,我只想显示“SG”而不是“Singapore SG”。这是我对 cboxBankCategory OnChange 函数的操作:

if comboxCountry.ItemIndex = 0 then
comboxCountry.Text := 'SG'

else if comboxCountry.ItemIndex = 1 then
comboxCountry.Text := 'IND'

else
comboxCountry.Text := 'AUS'

这似乎是正确的,但它对我不起作用,因为comboxCountry.Text 仍然在项目列表中显示整个国家/地区定义,而不仅仅是国家/地区代码,我的代码有什么问题吗?

谢谢。

4

2 回答 2

2

将组合框样式设置为csOwnerDrawFixed,并在onDrawItem事件中输入:

procedure TForm1.comboxCountryDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  s: String;
begin
  if not (odComboBoxEdit in State )then
    s := comboxCountry.Items[Index]
  else begin

if comboxCountry.ItemIndex = 0 then
s := 'SG'

else if comboxCountry.ItemIndex = 1 then
s := 'IND'

else
s := 'AUS'
  end;
  comboxCountry.Canvas.FillRect(Rect);
  comboxCountry.Canvas.TextOut(Rect.Left + 2, Rect.Top, s);

end;

并清除 OnChange 事件。

于 2012-06-28T16:02:11.463 回答
2

使用 OwnerDrawFixed 样式的组合框,您可以使用 OnDrawItem 事件。简短的例子。请注意,ComboBox.Text 属性未更改 - 此方法仅更改其外观。

Singapore SG
India IND
Australia AUS

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  s: string;
begin
  s := ComboBox1.Items[Index];
  if odComboBoxEdit in State then
    Delete(s, 1, Pos(' ', s));  //make your own job with string
  with (Control as TComboBox).Canvas do begin
    FillRect(Rect);
    TextOut(Rect.Left + 2, Rect.Top + 2, s);
   end;
end;

在此处输入图像描述

于 2012-06-28T16:02:22.500 回答