Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

short comboBox

Posted on 2001-08-01
19
Medium Priority
?
263 Views
Last Modified: 2010-07-27
I had some items in the TcomboBox->items-TStrings
there are option to sort it ????
the sort need to be visual only , not the number they represnt ?

thank
0
Comment
Question by:krelman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +5
19 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 6339907
2 ways:
 #1 write sorting procedure
 #2 push items from items property to some instance of TStringList, use sort method then push data back to combobox
ziolko.
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6339958
Yes

Combobox1.Sorted := True;

:)
GEM
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6340141
yup, gem is right,

just set sorted to true before adding the items
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
marquinho earned 300 total points
ID: 6341111
Hi here is a example with a TStringlist
just copy and paste both files .dfm and .pas

.dfm

object Form1: TForm1
  Left = 123
  Top = 186
  Width = 634
  Height = 310
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ComboBox1: TComboBox
    Left = 72
    Top = 80
    Width = 145
    Height = 21
    ItemHeight = 13
    TabOrder = 0
  end
  object Button1: TButton
    Left = 360
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Load Stringlist'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 376
    Top = 144
    Width = 75
    Height = 25
    Caption = 'Sort'
    TabOrder = 2
    OnClick = Button2Click
  end
end

.pas

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    xItems    : TStringList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

begin
  xItems:=TStringList.Create;
  xItems.Clear;
  xItems.Add('9');
  xItems.Add('8');
  xItems.Add('7');
  xItems.Add('6');
  xItems.Add('5');
  ComboBox1.Items:=xItems;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  xItems.Clear;
  xItems.Add('9');
  xItems.Sort;
  xItems.Add('8');
  xItems.Sort;
  xItems.Add('7');
  xItems.Sort;
  xItems.Add('6');
  xItems.Sort;
  xItems.Add('5');
  xItems.Sort;
  ComboBox1.Items:=xItems;

end;

end.
0
 
LVL 1

Expert Comment

by:marquinho
ID: 6341112
Hi here is a example with a TStringlist
just copy and paste both files .dfm and .pas

.dfm

object Form1: TForm1
  Left = 123
  Top = 186
  Width = 634
  Height = 310
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ComboBox1: TComboBox
    Left = 72
    Top = 80
    Width = 145
    Height = 21
    ItemHeight = 13
    TabOrder = 0
  end
  object Button1: TButton
    Left = 360
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Load Stringlist'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 376
    Top = 144
    Width = 75
    Height = 25
    Caption = 'Sort'
    TabOrder = 2
    OnClick = Button2Click
  end
end

.pas

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    xItems    : TStringList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

begin
  xItems:=TStringList.Create;
  xItems.Clear;
  xItems.Add('9');
  xItems.Add('8');
  xItems.Add('7');
  xItems.Add('6');
  xItems.Add('5');
  ComboBox1.Items:=xItems;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  xItems.Clear;
  xItems.Add('9');
  xItems.Sort;
  xItems.Add('8');
  xItems.Sort;
  xItems.Add('7');
  xItems.Sort;
  xItems.Add('6');
  xItems.Sort;
  xItems.Add('5');
  xItems.Sort;
  ComboBox1.Items:=xItems;

end;

end.
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6341194
there is an interesting things for all experts :
------------------------------------------------

Sort method can not work for my language becouse we write from right not left therefor I always Set

Combobox1.Sorted := False;

and sort it myself

Also , one time that i need it , I made another component that sort right to left not left to right .

always I have problems like this.

I hope it was not boring for u.although this type of straing problems all always interesting for me ! ;-)

best regards
hamed
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6342295
Hamed it seems to me that if Borland was selling a product in your language they would have thought of the problem of the reveral of the writing or is this one of those mistakes like Chevrolet made when they came out with the Nova and tried to sell in Latin America?

I think... Nova means no go. :)
0
 

Expert Comment

by:GoodTimes
ID: 6342522
I'm experiencing the same difficulties in my language because we write from right to left, and only in Delphi 4 (I think) Borland thought of adding BiDiMode to its components (and also not to all of them). I hope that in a few years they would begin to take notice of Right-To-Left languages...

Omri
0
 
LVL 3

Expert Comment

by:rondi
ID: 6343634
Use TStringList's Objects property to store the data
that each string points to.
That way it doesn't matter how the strings are ordered/sorted - you can set combobox1.sorted := true;
and each item still points to the right data.

rondi
0
 

Author Comment

by:krelman
ID: 6343823
i have problem with this answer if
I write stored = true
It change the index of the string
for example
fff index 1
ddd index 2
ere  index 3

after i stored = true it change the index
and i will need to change all the program .
how can i do it const that i will not change the program
every time i add a string into the combobox

thank
0
 
LVL 21

Expert Comment

by:ziolko
ID: 6344900
hamed & GoodTimes > I think sort method of TStringList use AnsiCompareText or AnsiCompareString so it should use Windows regional settings.
ziolko.
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6345064
hi gemarti , :-)))

hi Omri , where do you come from ? I tested your suggestion it did not work( at least at first try) but i think it must be the answer ( for me not krelman ;-) ) , if it was true it can be so useful becouse is in TControl witch is a very basic Object;

best regards
hamed
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6345163
hi ziolko , yes you are right . and i used that exactly in Delphi3 ( if i remember true becouse now i have not that comp becouse of HDD damaging ) for change my Component and i changed func calling at source code and maked a new component.

but about your comment , meybe it is true and windows regional setting can help but there is some little problems
the regional setting of win98 has not my language and also arabian language that i know is from right to left like our language ( although win2000 has it ) . another problem is i think dependenve of one language to regional setting is not good way.are you agree with me?

but if you are shur that regional setting can solve the problem please tell me !

thanks alot for your help
hamed
0
 
LVL 21

Expert Comment

by:ziolko
ID: 6350412
Probably regional settings will help but only PROBABLY, basicly I don't "trust" Windows, so if there is way to do something independently from Windows (like sorting strings) I do that!!. There is many  good known sorting algorithms like QuickSort or HeapSort so it's really not a big problem to write Your own sorting procedure. (I supose TStringList's Sort method use QuickSort, but I'm not sure)
ziolko.
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6351119
hi ziolko ,thanks for your help. you are right TStringList uses QuickSort algorithm.I have used some Sort algorithm like QuickSort and i am agree with you, they are not hard.almost all of sort algorithms uses Compare func. and for problems like that mentioned just changing the used compare func. can be enouph.

anyway . i read your profile. I am 22 ( like you ) and a senior student of Electrical Control Eng.math is so intersting for me . I am glad to be friend with you.

best regards
hamed
0
 

Expert Comment

by:GoodTimes
ID: 6351219
Hamed,
I'm from Israel. Where are you from?

Omri
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6351342
hi Omri,
I am from Iran.

hamed
0
 
LVL 21

Expert Comment

by:ziolko
ID: 6355552
That's one of reasons why I like Internet: people from all around the world!!
Hamed it's pleasure to me !!
ziolko.
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6358200
hi ziolko,i am agree with you.
please give me your e-mail , also you can see my e-mail in my profile.

Hamed
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question