Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Working with 2 ComboBoxes

Posted on 2007-11-20
17
Medium Priority
?
165 Views
Last Modified: 2010-04-05
Hi all,

I have a comboBox with a list of Car Makes....

Now what I need to do is for example:

If I choose FORD for ComboBox1 then display a list of FORD models etc...

I have all the Data and I don't mind hardcoding it...(No Database for this please).

E.g:

ComboBox1        ComboxBox2

  FORD                   Fiesta
                              etc
                              etc

Hope you can help!

THanks

ST3VO
 
0
Comment
Question by:ST3VO
[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
  • 7
  • 6
  • 3
  • +1
17 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20318645
Okay you would enter the items in your first comobox at design time by clicking on it, then in the Object Inspector clicking Items...
Then add your first list of items:
FORD,
GM,
TOYOTA
etc

Then you would add a OnCloseUp event handler to ComboBox1. and say something like this:
procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
if Combobox1.Text = 'FORD' then
Combobox2.Items.Clear;
Combobox2.Items.Add('FORD MODEL 1');
Combobox2.Items.Add('FORD MODEL 2');
Combobox2.Items.Add('FORD MODEL 3');
end;
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20318650
Code correction. See new code below:
procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
  if Combobox1.Text = 'FORD' then
  begin
    Combobox2.Items.Clear;
    Combobox2.Items.Add('FORD MODEL 1');
    Combobox2.Items.Add('FORD MODEL 2');
    Combobox2.Items.Add('FORD MODEL 3');
  end;
end;

Open in new window

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20318653
this is an example of doing it 'semi automated':
procedure TForm2.ComboBox1Change(Sender: TObject);
begin
 if ComboBox1.ItemIndex <> -1 then
  ComboBox2.Items.Assign(TStringList(ComboBox1.Items.Objects[ComboBox1.ItemIndex]));
end;
 
procedure TForm2.FormCreate(Sender: TObject);
 
 procedure AddItem(Name: string; SubItems: array of string);
 var StringList: TStringList;
     i: integer;
 begin
  StringList := TStringList.Create();
 
  for i := 0 to Length(SubItems) - 1 do
   StringList.Add(SubItems[i]);
 
  ComboBox1.AddItem(Name, StringList);
 end;
 
begin
 AddItem('Ford', ['Fiesta', 'Mondeo', 'Galaxy']);
 AddItem('Seat', ['Altea', 'Leon', 'Albrahama']);
end;
 
procedure TForm2.FormDestroy(Sender: TObject);
var i: integer;
begin
 for i := 0 to Combobox1.Items.Count - 1 do
  ComboBox1.Items.Objects[1].Free();
end;

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:rfwoolf
ID: 20318670
You can also do LoadFromFile if you want to store the items in an external file:

If Combobox1.text = 'FORD' then
begin
combobox2.items.clear;
combobox2.Items.LoadFromFile(extractfilepath(Application.ExeName) + 'FordModels.txt);
end;

The part "extractfilepath(Application.ExeName)" gives the folder your exe is running from. So you would keep a txt file called "FordModels.text" in your application directory, and experiment with the contents
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20318691
> No Database for this please

what is your definition of a database, something like SQL, or any external file.
Would you prefer hard coding or storing external?
0
 

Author Comment

by:ST3VO
ID: 20318699
I don't mind external files....But I don't want to connect to a Table....for this...That's what I mean't!
0
 

Author Comment

by:ST3VO
ID: 20318703
I'm testing your's right now MerijnB!
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20318762
here an example which uses an external file.

The file is called 'cars.dat' and should be located in the directory the application is started.

Here an example of cars.dat:

List=Ford,Seat
Ford=Galaxy,Mondeo,Fiesta
Seat=Leon,Altea,Toledo
unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm2 = class(TForm)
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    fCars: TStringList;
  public
    { Public declarations }
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.dfm}
 
procedure TForm2.ComboBox1Change(Sender: TObject);
begin
 if ComboBox1.ItemIndex <> -1 then
  ComboBox2.Items.CommaText := fCars.Values[Combobox1.Items[Combobox1.ItemIndex]];
end;
 
procedure TForm2.FormCreate(Sender: TObject);
begin
 fCars := TStringList.Create();
 fCars.StrictDelimiter := true;
 fCars.LoadFromFile('cars.dat');
 
 ComboBox1.Items.CommaText := fCars.Values['List'];
end;
 
procedure TForm2.FormDestroy(Sender: TObject);
begin
 fCars.Free();
end;
 
end.

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20318914
Right....

Got a little problem here.

The list of MAKES is about 100

And I am coding it this way:

if sco.Text = 'ASTON MARTIN' then  //ASTON MARTIN Models
  begin
    sModels.items.clear;
    sModels.Text:='ANY';
    sModels.Items.LoadFromFile('.\data\MOD\ASTON.dat');;
  end;

    if sco.Text = 'AUDI' then  //AUDI Models
  begin
    sModels.items.clear;
    sModels.Text:='ANY';
    sModels.Items.LoadFromFile('.\data\MOD\AUDI.dat');;

The problem here is that the code is going to be massive!

MerijnB's code also works great but is also going to take me ages to put it together.

Anyone got a suggestion on how the get it done quicker without connecting to a table?

One one side I have the list of 100 Makes and on another file all the Massive list of models.

Any suggestions please?

Thanks

ST3VO


0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20318943
What was wrong with my suggestion to do loadfromfile?
You store your 100 models in a txt file and use loadfromfile?
I suppose you'd have plenty of if then statements like:
if sco.Text = 'AUDI' then...
if sco.text = 'ASTON MARTIN' then..

So you could call all your txt files the name of your markes, i.e.
AUDI.txt
ASTON MARTIN.txt

Thus you could say:
combobox2.items.clear;
combobox2.Items.LoadFromFile(extractfilepath(Application.ExeName) + sco.text + '.txt');

===

Is there any particular reason why you're opposed to tables?
You could use a nice master-detail relationship.
If you're not familiar with that, basically your one table would contain MAKES, the other ***ALL*** the models but with a column for the Make. The *models* table would set the *Make* table as its MasterTable, and its MasterField would be the "Make". Then connect your combo boxes accordingly and it will work automatically !
0
 

Author Comment

by:ST3VO
ID: 20318986
I already have all the MAKE's inside the ComboBox1 Strings....

I just want to avoid all the If..then's...because I would have 100 If...thens...

0
 

Author Comment

by:ST3VO
ID: 20318994
MerijnB, I'm trying out your latest code now...
0
 

Author Comment

by:ST3VO
ID: 20319007
MerijnB, Cannot use your code because the Database which I need to query after the selection contains fields like:

Models:

AC,544

so, If I separated it by commas it would see model AC,544 and two different models.

0
 
LVL 13

Accepted Solution

by:
rfwoolf earned 2000 total points
ID: 20319009
Like I said if you have 100 makes, and 100 files containing all the models of those makes, e.g.
Ford.txt
Toyota.txt
then this one line would do it:
combobox2.Items.LoadFromFile(extractfilepath(Application.ExeName) + sco.text + '.txt');
0
 

Author Comment

by:ST3VO
ID: 20319062
I'll try your's now rfwoolf!
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20319095
Now, if you do want to use 100 if... then statements, make sure you know about case of statements:

       Case sco.text of
         'FORD': begin
                  //
               end;
         'TOYOTA': begin
                  //
               end;
         'ASTON MARTIN': begin
                  //
               end;

--Haven't tested it, but you get the idea
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 20319834
How about a simple INI file.That Looks Like This

[Ford]
Fiesta=
Lincoln=
F150=
Ranger=

[Chevy]
Astro=
Nova=

[Dodge]
Ram=
Charger=
Caravan=

And the following simple code:

procedure TForm1.FormShow(Sender: TObject);
var ini : TiniFile;
begin
	ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Cars.ini');
	ini.ReadSections(cbMake.Items);
	ini.Free;
end;
 
procedure TForm1.cbMakeCloseUp(Sender: TObject);
var ini : TiniFile;
	i : integer;
begin
	ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Cars.ini');
	ini.ReadSectionValues(cbMake.Text, cbModel.Items);
	for i := 0 to cbModel.Items.Count -1 do
		cbModel.Items[i] := cbModel.Items.Names[i];
	ini.Free;
 
end;

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
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