Recent

Author Topic: Listview and combobox  (Read 2864 times)

Paulnewyork

  • New Member
  • *
  • Posts: 44
Listview and combobox
« on: June 03, 2020, 12:01:52 am »
Hallo,

I have a combobox With 4 items. And i have a listview weich Shows the data from a MySQL dB.
I Would like to select some data in the listview and the combobox Item should change to the specific one. But ist doesnt work.
The edit boxes changes When i select some data.

Is there anyone who can help me?

Thank you!

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Listview and combobox
« Reply #1 on: June 03, 2020, 12:31:36 am »
Did you set the  ItemIndex of the ComboBox that matches your entry ?

Its not as easy as just setting the caption..

What style of ComboBox are you using ?
The only true wisdom is knowing you know nothing

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Listview and combobox
« Reply #2 on: June 03, 2020, 12:57:59 am »
The combo box selected item changes when you set the ItemIndex Property. You can find the index by calling   Combo.ItemIndex := Combo.Items.IndexOf('The string you want to select');

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #3 on: June 03, 2020, 07:23:39 am »
Thank you for your fast answers.  I am not sure how to use the IndexIf(...)
this is my  the code:

Begin
stringsold_id := ListViewSold.Selected.Caption;
 Form2.connectDB; 
Form2.SQLQuery3.SQL.Text := 'SELECT * from customer, sold WHERE customer.customer_id = sold.customer_id;

Error: Incompatible type for arg no. 1: Got "LongInt", expected "AnsiString"
« Last Edit: June 09, 2020, 09:09:44 pm by Paulnewyork »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Listview and combobox
« Reply #4 on: June 03, 2020, 07:51:56 am »
Thank you for your fast answers.  I am not sure how to use the IndexIf(...)
this is my  the code:

Begin
stringsold_id := ListViewSold.Selected.Caption;
 Form2.connectDB; 
Form2.SQLQuery3.SQL.Text := 'SELECT * from customer, sold WHERE customer.customer_id = sold.customer_id;
   Form2.SQLQuery3.Open;     

  stringCutsomer:= Form2.SQLQuery3.FieldByName('customer_id').AsString;
 intCustomer:= StrToInt(stringCutsomer);     
Combo1.Items.IndexOf(intCustomer);



Error: Incompatible type for arg no. 1: Got "LongInt", expected "AnsiString"
It looks like you are doing things the hard way.  I have no idea what exactly you are trying to accomplish, the info you shared is not enough, for me to realize, what you need to do, so, I'm going to address the problem at hand, which, I suspect, is not going to produce the expected results.

Any way.

 IndexOf() takes a string as a parameter not an integer, try using
Code: Pascal  [Select][+][-]
  1. Combo1.Items.IndexOf(stringCustomer)
instead and see if that takes you to your expected destination.
« Last Edit: June 03, 2020, 07:53:40 am by eljo »

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #5 on: June 03, 2020, 08:19:45 am »
Combo1.Items.IndexOf(stringCustomer)
 this loooks very clear but it doesn`t work. i think the stringCustomer is empty.

because under:
var
stringcustomer : string;

this is because I used:
stringKunde:= Form2.SQLQuery3.FieldByName('custoner_id').AsString;


I am  not sure how the programm knows the correct  fields in the combobox, when i click in different data in ListView.


eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Listview and combobox
« Reply #6 on: June 03, 2020, 08:35:27 am »
Combo1.Items.IndexOf(stringCustomer)
 this loooks very clear but it doesn`t work. i think the stringCustomer is empty.

because under:
var
stringcustomer : string;

this is because I used:
stringKunde:= Form2.SQLQuery3.FieldByName('custoner_id').AsString;


I am  not sure how the programm knows the correct  fields in the combobox, when i click in different data in ListView.
My answer is based on the code you posted on your previous message, it has no stringKunde variable referenced at all. If you have changed the code in mean time then you need to change my suggestion accordingly.

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #7 on: June 03, 2020, 08:49:03 am »
Sorry, for that.

Here is the complete procedure, maybe this is easier.  I have the same problem with combo for time :/
procedure TForm4.ListViewSoldClick(Sender: TObject);

 var
 stringTime : string;
 stringCustomer: string;

intTime: integer;
intCustomer: integer;


  begin
  stringOrder_ID := ListViewMueller.Selected.Caption;
  Form2.connectDB;
  Form2.SQLQuery1.SQL.Text := 'SELECT * FROM sold where order_id = ''blub''';';
  Form2.SQLQuery3.SQL.Text := 'SELECT * from customer, sold WHERE customer.customer_id = sold.customer_id;

 Form2.SQLQuery1.Open;
Form2.SQLQuery3.Open;
     


stringCustomer := Form2.SQLQuery3.FieldByName('customername').AsString;
Combo1.ItemIndex := Combo1.Items.IndexOf(Form2.SQLQuery3.FieldByName('customername').AsString);
Combo1.Items.IndexOf(stringCustomer);


//    intLieferzeitpunkt := Form2.SQLQuery1.FieldByName('time').AsInteger;
 //  intshow:= StrToInt(stringtime);
 //   ComboLieferzeitpunkt.Items.IndexOf(stringTime);
    Form2.closeDB;         
« Last Edit: June 09, 2020, 09:11:04 pm by Paulnewyork »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Listview and combobox
« Reply #8 on: June 03, 2020, 09:13:29 am »
It's better to use edit or label if combobox doesn't have items.
Please tell what form2 is. It's better to use datamodule.
Code: Pascal  [Select][+][-]
  1. var
  2.  stringTime : string;
  3.  stringCustomer: string;
  4.  intTime: integer;
  5.  intCustomer: integer;
  6.  
  7. begin
  8.   stringOrder_ID := ListViewVerkauforders.Selected.Caption;
  9.   connectDB;
  10.  
  11.   //assume that order_id in an integer
  12.   SQLQuery1.SQL.Text := 'SELECT * FROM sold where order_id = :id';
  13.   SQLQuery1.parambyname('id').AsInteger := StrToInt(stringOrder_ID);
  14.   SQLQuery3.SQL.Text := 'SELECT * from customer, sold WHERE customer.customer_id = sold.customer_id';
  15.   { the query you want can do at once.
  16.     it's better to select the fields you want instead of all fields
  17.    
  18.     SQLQuery1.SQL.Text := 'SELECT s.*, c.marktteilnehmername FROM sold s ' +
  19.                           ' INNER JOIN customer c on c.costumer_id = s.customer_id ' +
  20.                           ' WHERE s.order_id = :id ';
  21.     SQLQuery1.parambyname('id').AsInteger := StrToInt(stringOrder_ID)
  22.   }
  23.  
  24.   SQLQuery1.Open;
  25.   SQLQuery3.Open;
  26.  
  27.   stringCustomer := SQLQuery3.FieldByName('marktteilnehmername').AsString;
  28.    
  29.   // this is only working if values of 'marktteilnehmername' is in items.
  30.   // otherwise it gives -1 back
  31.   Combo1.ItemIndex := Combo1.Items.IndexOf(SQLQuery3.FieldByName('marktteilnehmername').AsString);
  32.   //this is doing nothing
  33.   Combo1.Items.IndexOf(stringMarktteilnehmer);
  34.  
  35.   //this id doing nothing
  36.   Combo2.Items.IndexOf(stringTime);    // Combo for Time
  37.   closeDB;                
  38. end;      
  39.  
« Last Edit: June 03, 2020, 02:43:12 pm by mangakissa »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #9 on: June 03, 2020, 09:38:18 am »
the form2 is just for the connection to the database (there are the transaction, sql query, sqlconnection).

In the Comboboxes are items
in customer are 4 items.
and in time there are 24 items (00:00:00 to 23:00:00)


May just the information why i made the SQL statements:
If I just take this

but in combobox1 are the names of the customer. so i made a new SQLQuery
 Form2.SQLQuery3.SQL.Text := 'SELECT * from customer, sold WHERE customer.customer_id = sold.customer_id;

in the listview i can see the customer names, because of the second SQL Statement. But the Combox doesn't change the items by clicking on different data in listview
« Last Edit: June 09, 2020, 09:10:20 pm by Paulnewyork »

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Listview and combobox
« Reply #10 on: June 03, 2020, 01:44:51 pm »
A combo is just a list of items. U need to add to that list if you accumulating items from a data base

 Combobox.items.add(some strung).

 To locate a matching index.

 Combobox.itemindex := combobox.items.indexof(some string).

If it succeeds the it will be shown in the caption.

Hope that helps.

From my phone Jamie.
The only true wisdom is knowing you know nothing

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #11 on: June 03, 2020, 08:19:13 pm »
no it doesn`t work. :-(

i have already 4 items in the combobox, so i dont need the add command?!

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Listview and combobox
« Reply #12 on: June 03, 2020, 08:41:47 pm »
The entry must match exactly.

This means it is case sensitive and must match the whole word.
Also the space style must also match. Normal or white space which are different values.

The only true wisdom is knowing you know nothing

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #13 on: June 03, 2020, 08:51:40 pm »
I checked the items with the db entries and they are the same (capitel letters etc).


maybe make some edit fields in the next days, because this works, but the combobox is better to use for the user.

Thank you very much for your support!!!!!

Paulnewyork

  • New Member
  • *
  • Posts: 44
Re: Listview and combobox
« Reply #14 on: June 03, 2020, 10:04:54 pm »
Is there something I need to change or enable in the object inspector of the combobox?

 

TinyPortal © 2005-2018