1

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

Item 1 : 'Singapore SG'

Item 2 : 'India IND'

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

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'

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 ?

Thanks.

Pejman Nikram
  • 570
  • 2
  • 11
  • 26

2 Answers2

2

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;

and clear the OnChange event.

balazs
  • 5,698
  • 7
  • 37
  • 45
  • yes, with delphi 7, and bds2006 it works. Than tell me what does this code in your computer. In my pc when the combobox drops down lists the elements displayed as usual, but when you select something, it'll display the short texts. – balazs Jun 28 '12 at 16:49
  • 1
    @hassan118, from the answers here it's not visible on the first view, but have you set the [`Style`](http://docwiki.embarcadero.com/Libraries/XE2/en/Vcl.StdCtrls.TComboBox.Style) property of your combo box to `csOwnerDrawFixed` as was suggested ? – TLama Jun 28 '12 at 17:23
  • 3
    @hassan118: "dosent work" is absolutely meaningless to everyone here but you. You need to explain what you mean by "dosent work" - we can't see your screen. Just saying it doesn't work tells us nothing - be clear about **how** it doesn't work, so we can help you. – Ken White Jun 28 '12 at 17:47
  • thanks, i set Style to csOwnerDrawFixed and use your code but when the combobox drops down lists the elements displayed as usual and when i select something display the item text not a short of text. i use Delphi XE2 – Pejman Nikram Jun 28 '12 at 18:10
  • and the part of after `else begin` never don't run until after exit or enter to combobox – Pejman Nikram Jun 28 '12 at 18:18
2

With OwnerDrawFixed style of Combobox you can use OnDrawItem event. Short example. Note that ComboBox.Text property is not changed - this method only changes how it looks.

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;

enter image description here

MBo
  • 77,366
  • 5
  • 53
  • 86
  • 1
    It is necessary to set combobox Style property to OwnerDrawFixed – MBo Jun 28 '12 at 17:33
  • 1
    @hassan118: "isnt work for me" means nothing to anyone here but you. You need to explain what you mean by doesn't work - we can't see your screen. Just saying it doesn't work tells us nothing - be clear about how it doesn't work, so we can help you. – Ken White Jun 28 '12 at 17:48
  • I tried very hard! :D and when I clicked out of the program (eg desktop) short text is displayed when the mouse back on combobox the main text will appear. – Pejman Nikram Jun 29 '12 at 06:27
  • Is this event (OnDrawItem) ever fired? Is (if odComboBoxEdit in State) ever fired? – MBo Jun 29 '12 at 06:29
  • oh! codes working, but if no style is not active Upon activation styles in Delphi EX2 codes do not work : ( – Pejman Nikram Jun 29 '12 at 06:37
  • It's rather strange issue. Possible reasons: XE2 issues, problems with Windows Themed API or Windows configuration (I see left-handed combo triangle at your picture - right-to-left language support?) – MBo Jun 29 '12 at 07:06
  • Thank you!yes It is supported The problem can be solved by removing styles in Delphi EX2, but I need this style! – Pejman Nikram Jun 29 '12 at 07:19