我如何在Delphi的ComboBox中使用项目文本的不同文本 [英] How i can use different text of item text in ComboBox in Delphi

查看:69
本文介绍了我如何在Delphi的ComboBox中使用项目文本的不同文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a TComboBox component(comboxCountry) on my form. And here's the items inside the TComboBox.

项目1:新加坡SG

项目2:印度IND

项目3:澳大利亚AUS等等等。

Item 3 : 'Australia AUS' and etc etc etc ..

何时组合框的值已更改,我希望将combboxCounty.Text设置为
仅显示国家/地区代码,而不显示项目列表中的整个字符串。
例如,我只想显示'SG'而不是'Singapore SG'..这是我对cboxBankCategory OnChange函数执行
的方法:

When the combobox value is changed, i want the combboxCounty.Text to only display the country code instead of the whole String in the items list. For example, i want to only display 'SG' instead of 'Singapore SG' .. Here's how i do for cboxBankCategory OnChange function:

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

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

else
comboxCountry.Text := 'AUS'

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

It seems correct, but it doesn't works for me as the comboxCountry.Text still display the whole country definition in the item list instead of only the country code, anything wrong with my code anyone ?

谢谢。

推荐答案

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

Set the combobox style to csOwnerDrawFixed, and in the onDrawItem event put this:

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事件。

and clear the OnChange event.

这篇关于我如何在Delphi的ComboBox中使用项目文本的不同文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆