-1

How to get the selected value from a TComboBox?

The TCombobox name is comboTest

vaue_is := comboTest ???
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Pointer
  • 2,123
  • 3
  • 32
  • 59

1 Answers1

4

You can use ItemIndex:

if comboTest.ItemIndex >= 0 then
  vaue_is := comboTest.Items[comboTest.ItemIndex];

This should work equally well for VCL and FireMonkey (FMX).

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Velthus Tnx for help but in ShowMessage(vaue_is ); I get name no value eg. 100 – Pointer Sep 19 '18 at 10:44
  • 1
    You get a string, right. What "value" do you want to get? – Rudy Velthuis Sep 19 '18 at 10:46
  • Yes, value eg. 100 or 101 – Pointer Sep 19 '18 at 10:48
  • @Pointer, add list of your values you expect to get in your question, because Rudy answer works well. – Josef Švejk Sep 19 '18 at 10:50
  • @Pointer When you populated the combo you converted the integer values into strings. When you read from the combo, you get those strings back. See if you can think about the problem that you now face. You have a string. You want an integer. How do you solve this problem? – David Heffernan Sep 19 '18 at 10:51
  • What does that 100 or 101 signify? the index of the selected item? That is ItemIndex? I answered that already. To display it: `ShowMessageFmt('%d', [comboTest.ItemIndex]);`. – Rudy Velthuis Sep 19 '18 at 10:52
  • And if you did what David said, then I don't understand your comment "I get name no value". Did you mean you got a string like `'100'` or something else, i.e. the string you selected? In other words: you should be a little clearer what you actually want to achieve. You could put the relevant code and declarations in your question (**no link**, actual code), so people can understand what you actually want. – Rudy Velthuis Sep 19 '18 at 10:56
  • I combo fill from database, eg. value 100 --> Audi, 101 --> Bmw in combo stay Audi, Bmw how get id from that no index. When select Audi I need get '100'. – Pointer Sep 19 '18 at 10:57
  • 2
    So the strings in your combobox are 'Audi', 'BMW', etc.? And now you want to get back the id that was used in the database? You could store that id together with the string, e.g. in the Objects part of the TStrings. Or you get the string like I showed ('Audi') and then search the database for the id. **And that you wanted this is not at all clear from your question**. – Rudy Velthuis Sep 19 '18 at 11:02
  • Yes string is Audi or Bmw and I need id from database, I'm bind combo wit odac control. – Pointer Sep 19 '18 at 11:14
  • That is information that should be in your question. I answered what you actually asked, but what you really want is a little more involved. So add that information to your question and perhaps someone will be able to help you retrieving the id (or the entire record) from the string. Your question does not mention binding, nor anything about an ID from a database. That changes the whole question. – Rudy Velthuis Sep 19 '18 at 11:17
  • Tnx for answer, I'm create new question https://stackoverflow.com/questions/52404766/get-selected-id-from-tcombobox-delphi-10-rad-studio +1 vote for you – Pointer Sep 19 '18 at 11:25