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

x
?
Solved

Combobox example....

Posted on 1999-09-17
19
Medium Priority
?
513 Views
Last Modified: 2010-04-06
Hi I need an example of the use of a combobox, and a litle about the use of a combobox, and memo box...can u give a litle example....
0
Comment
Question by:nicce
[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
  • 5
  • 4
  • +2
19 Comments
 
LVL 1

Expert Comment

by:TAZI
ID: 2053687

To Add Data to a ComboBox:
---------------------------
Combobox1.Items.Add(Whatever you want to add);

If you want to add data from a Database then you run you query.
Query1.Close;
Query1.Params[0].asString := parameter
Query1.Open;
Query1.First;
while not Query1.eof do
   begin
    Combobox1.Items.Add(Whatever you want to add);
    Query1.Next;
   end;
The above query allows you to add data for all the records that the query brings back.

Combobox1.ItemIndex := -1;  
This clears the combobox the display will be blank, but data will still be in the combobox.

Combobox1.ItemIndex := 0;
This takes you to the first record of the combobox;

MEMO BOX;

To add data to a memo...
Memo1.Lines.Add('THIS IS LINE ONE');

Hope this will be of some assistance
Regards
Tazi


0
 
LVL 7

Expert Comment

by:RBertora
ID: 2053699
Tazzi is fast...
Rob

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ComboBox1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


// clear the memo and combobox
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;
  ComboBox1.Clear;
end;

// add the contents of the edit box to the memo and combobox
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines.Add(Edit1.Text);
  Combobox1.Items.Add(Edit1.Text);
end;


// assing the memo to the combobox
procedure TForm1.Button3Click(Sender: TObject);
var
  lp : integer;
begin
  Combobox1.Items.Clear;
  for lp := 0 to Memo1.Lines.Count-1 do
  begin
    Combobox1.Items.Add(Memo1.Lines[lp]);
  end;

  // or simply :  Combobox1.Items := Memo1.Lines;


end;

// show what you selected on the combobox
procedure TForm1.ComboBox1Click(Sender: TObject);
begin
  showmessage('you selected ' + Combobox1.Items[Combobox1.ItemIndex]);
end;

end.
0
 

Author Comment

by:nicce
ID: 2053722
Hi thanks for your quick response, but we cant quite figure it out, we need the combobox to be filled when the app starts and the same with the listbox, and then the user needs to choose from both of them, and the result should be shown in the memobox...

ex. combobox1 should contain information about a letters weight and the listbox shouldt contain the destination,  and then the memo shouldt show the price...
sorry for the inconvenience but if you´l give it a try we´ll be happy ;.)
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 7

Expert Comment

by:RBertora
ID: 2053733
You can fill out the combobox and listbox.. by clicking on
the (...) button next to the lines&items property in the object inspector..

if you need to fill it out programatically then do this
in the onactivate or oncreate event of the form...

Rob.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 2053740
Select the combobox or listbox on your form.

Then select the "Items" property in the Object Inspector.

Click on the ellipsis (...) button.

In the form that pops up, enter in the items you want in the combobox/listbox, one per line.

This will cause the list/combobox to be filled with these items when your app starts.

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 2053742
d'oh     not quick enuff
0
 
LVL 1

Expert Comment

by:TAZI
ID: 2053751
Okay...you are probably reading the information from a Database..If the Combobox will contain standard information throughout the program you can do what RBertora has suggested, however if not then

Fill the combobox when the app starts....
On The Forms OnFormCreate Event add the code that I have mentioned above

Combobox1.Items.ADD('Database Values')
Listbox1.Items.Add(Value);
>>In a loop to add all the data that the query brings back

What is the Destination that you talk about ? ? Give me more information as to what you want the program to do ?

Regards
Tazi

0
 

Author Comment

by:nicce
ID: 2053760
the destination is the place where the letter is supposed to be send to, and the app is supposed to calculate the amount of stamps .-)
0
 
LVL 1

Expert Comment

by:TAZI
ID: 2053776
Okay...So, where is all the data being read from.

A Database or What ? ?

Regards
Tazi
0
 

Author Comment

by:nicce
ID: 2053802
no database datas, just the lines in the combobox, ex.
letter weight 100g
destination europe
and then the app shows us the total price..
really simple...
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2053821
Is the price calculated or
is it looked up based on weight and destination category?
Rob.
0
 
LVL 1

Expert Comment

by:TAZI
ID: 2053840
So the data in the Combobox
Letter weight 100g is fixed..thus, the Combobox will contain a fixed list...use RBetora's, ScrapDog's comment to add data to the Combobox via the Object Inspector.

The Destination . . Europe is proabably also a fixed list of all the places.  So this data could also be added in the same way via the object inspector.

Now..the calculation of the Total Price.  How do you do this ?  How do you know what is the cost ?

Regards
Tazi
0
 

Author Comment

by:nicce
ID: 2053901
thats what im asking u about ;-), if the 0-100 grams is chosen and denmark is choosen in the destination list, tehn the price should be calculated and putten in a panelbox or something like that, ill use the information from the postoffice ex. 0-100 grams in danmrk cost approx 1 $ an so on....im not quite sure what to do.

The reason for all this is that we are doing a beginners excercice in school...and we have to explain the combobox and the listbox and the memobox... and uot of this make a small app and i just thougt that a post calculator could be a good example...
0
 
LVL 7

Accepted Solution

by:
RBertora earned 200 total points
ID: 2053936
This should help you
Rob.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ListBox1: TListBox;
    Button1: TButton;
    Memo1: TMemo;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormActivate(Sender: TObject);
begin
  Combobox1.Clear;
  Memo1.Clear;
  Listbox1.Clear;
  Combobox1.Items.Add('20 grams');
  Combobox1.Items.Add('50 grams');
  Combobox1.Items.Add('100 grams');
  ListBox1.Items.Add('Sweden');   // group1 close countries
  ListBox1.Items.Add('USA');  //// group2 medium distance countries.
  ListBox1.Items.Add('Australia');  // group3 far countries
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  cost : integer;
begin
  Memo1.Clear;
  case Combobox1.ItemIndex of
  -1: showmessage('you have to select a weight');
  1:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 10');
    2: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 20');
    3: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 30');
    end;
  end;
  2:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 20');
    2: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 35');
    3: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 50');
    end;
  end;
  3:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 60');
    2: begin
        Cost := 3*2*100;
         Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: ' + inttostr(Cost));
       end;
    3: begin // this will calculate a cost
         Cost := 3*3*100;
         Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] + ' cost: ' + inttostr(Cost));
       end;
    end;
  end;
  end;
end;

end.
0
 

Author Comment

by:nicce
ID: 2053946
thanks alot for your help
0
 

Author Comment

by:nicce
ID: 2053999
Roberta please help...this was your code...but the app doesn´t do any thing it runs but the boxes are not being filled???...

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    ComboBox1: TComboBox;
    ListBox1: TListBox;
    Button1: TButton;
    Memo1: TMemo;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormActivate(Sender: TObject);
begin
  Combobox1.Clear;
  Memo1.Clear;
  Listbox1.Clear;
  Combobox1.Items.Add('20 grams');
  Combobox1.Items.Add('50 grams');
  Combobox1.Items.Add('100 grams');
  ListBox1.Items.Add('Sweden');   // group1 close countries
  ListBox1.Items.Add('USA');  //// group2 medium distance countries.
  ListBox1.Items.Add('Australia');  // group3 far countries
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  cost : integer;
begin
  Memo1.Clear;
  case Combobox1.ItemIndex of
  -1: showmessage('you have to select a weight');
  1:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 10');
    2: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 20');
    3: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 30');
    end;
  end;
  2:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 20');
    2: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 35');
    3: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 50');
    end;
  end;
  3:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 60');
    2: begin
        Cost := 3*2*100;
         Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: ' + inttostr(Cost));
       end;
    3: begin // this will calculate a cost
         Cost := 3*3*100;
         Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] + ' cost: ' + inttostr(Cost));
       end;
    end;
  end;
  end;
end;

end.






0
 

Author Comment

by:nicce
ID: 2054258
Hi again i hope u dont find me to anoying..
i have a booklet from the post office and i have to use the information from it in the app.
but i dont know how to do this exactly...
ex. "Danmark" (Denmark) has 20,50,100,250,500,1000,2000 grams letter prices and the prices for them is (4.00, 5.25, 5.25, 9.25, 16.00, 20.00, 28.00) and so has the all thew other 5 areas. how can i put there informations in the code, i dont need any calculation i guess not..i think. :-)



unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ComboBox1: TComboBox;
    Memo1: TMemo;
    Button1: TButton;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}



procedure TForm1.FormActivate(Sender: TObject);
begin
  Combobox1.Clear;
  Memo1.Clear;
  Listbox1.Clear;
  Combobox1.Items.Add('20 gram');     //this the new weights
  Combobox1.Items.Add('50 gram');     //
  Combobox1.Items.Add('100 gram');     //
  Combobox1.Items.Add('250 gram');     //
  Combobox1.Items.Add('500 gram');     //
  Combobox1.Items.Add('1000 gram');    //
  Combobox1.Items.Add('2000 gram');  //
  ListBox1.Items.Add('Danmark');   // group1 close countries
  ListBox1.Items.Add('Færøerne');  //// group2 pretty close countries.
  ListBox1.Items.Add('Grønland');  // group3 medium countries
  ListBox1.Items.Add('Norden');   // group4  quite far countries
  ListBox1.Items.Add('Europa');  //// group5  far distance countries.
  ListBox1.Items.Add('Øvrige Udland');  // group6 farest countries
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  cost : integer;
begin
  Memo1.Clear;
  case Combobox1.ItemIndex of
  -1: showmessage('you have to select a weight');
  1:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 10');
    2: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 20');
    3: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 30');

   end;
  end;

  2:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 20');
    2: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 35');
    3: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 50');
    end;
  end;
  3:
  begin
    case Listbox1.ItemIndex of
    -1: showmessage('you have to select a destination');
    1: Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: 60');
    2: begin
        Cost := 3*2*100;
         Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] +' cost: ' + inttostr(Cost));
       end;
    3: begin // this will calculate a cost
         Cost := 3*3*100;
         Memo1.Lines.Add('Weight : ' + Combobox1.Items[Combobox1.ItemIndex] + ' Dest: ' +
         Listbox1.Items[Listbox1.ItemIndex] + ' cost: ' + inttostr(Cost));
       end;
    end;
  end;
  end;
end;



end.
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2054578
 Combobox1.Items.Add('20 gram');     //this the new weights
  Combobox1.Items.Add('50 gram');     //
  Combobox1.Items.Add('100 gram');     //
  Combobox1.Items.Add('250 gram');     //
  Combobox1.Items.Add('500 gram');     //
  Combobox1.Items.Add('1000 gram');    //
  Combobox1.Items.Add('2000 gram');  //
  ListBox1.Items.Add('Danmark');   // group1 close countries
  ListBox1.Items.Add('Færøerne');  //// group2 pretty close countries.
  ListBox1.Items.Add('Grønland');  // group3 medium countries
  ListBox1.Items.Add('Norden');   // group4  quite far countries
  ListBox1.Items.Add('Europa');  //// group5  far distance countries.
  ListBox1.Items.Add('Øvrige Udland');  // group6 farest countries
  end;

Combobox1 stores weights. in this case there are 6 items,
item 1 (20gram) is stored in postion 0  (Combobox1.itemindex=0)
item 2 (50gram) is stored in position 1 (Combobox1.itemindex=1)
....
item 6 (2000gram) is stored in position 5 (Combobox1.itemindex=5)

Listbox1 stores destinations.
item 1 (Danmark) is stored in postion 0 (Listbox1.itemindex =0)
...
item5 (ovrige) is stored in position 5 (Listbox1.itemindex-5)

if a user selects a value from combobox1 say 50 grams
then the value of combobox's itemindex =0
in other words  Combobox1.ItemIndex = 0
if a user selects 500 grams then Combobox1.ItemIndex = 4

this is the same with the listbox.


now the case statement:

case combobox1.Itemindex of
0 :
  begin
    <>HERE
  end;
1 :
  begin
    <>HERETOO
  end;
.....
5 begin
  end;
end;

<>HERE will be executed if the value of combobox1.itemindex is 0
<>HERETOO will be run if the  value of combobox1.itemindex is 1
if <>HERE is executed then cases 1..5 will not be executed because
the value of itemindex =0

so let say the user clicked 50 gram therefore combobox1itemindex = 1
the program will try to run this bit of code

  1 :
  begin
    <>HERETOO
  end;

if Itemindex is one then we know the weight is 50 grams, now we want to
see what desrination he has selected.

so the code for <>HERETOO is:

case Listbox1.Itemindex of
0 :
  begin
    <>This
  end;
1 :
  begin
    <>This too
  end;
.....
5 begin
  <>The 5th destination
  end;
end;


say the user clicked on Færøerne
this means Listbox1.Itemindex = 1
therefore <>This too code is run...

<>This too

if the above line is to execute we know the user has clicked on 50grams,
 and Færøerne therefore you must now

 do Memo1.Lines.Add('amount = 100'); //or whatever value you have for this selection..

NOW FIGURE IT OUT, j
give up, or go and ask one of your mates
to help you, or if you have the courage ask
your teacher to explain what I just wrote here.
Rob.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6865821
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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