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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@hassan118, from the answers here it's not visible on the first view, but have you set the Style property of your combo box to csOwnerDrawFixed as was suggested ?
@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.
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
and the part of after else begin never don't run until after exit or enter to combobox
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

7 Comments

It is necessary to set combobox Style property to OwnerDrawFixed
@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.
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.
Is this event (OnDrawItem) ever fired? Is (if odComboBoxEdit in State) ever fired?
oh! codes working, but if no style is not active Upon activation styles in Delphi EX2 codes do not work : (
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.