Solved

short comboBox

Posted on 2001-08-01
19
248 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
  • 6
  • 4
  • 2
  • +5
19 Comments
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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
Comment Utility
Yes

Combobox1.Sorted := True;

:)
GEM
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
yup, gem is right,

just set sorted to true before adding the items
0
 
LVL 1

Accepted Solution

by:
marquinho earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:krelman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hamed,
I'm from Israel. Where are you from?

Omri
0
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
hi Omri,
I am from Iran.

hamed
0
 
LVL 21

Expert Comment

by:ziolko
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now