Thread: C++ Builder TCombobox problem

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    105

    C++ Builder TCombobox problem

    Hi there!

    I had just started programming with the Borland C++ Builder 5, and I experienced some problems. First of my problems is the handling of TComboboxes.

    The situation is the following. I made a form with 3 comboboxes, a button. I want to set some values in the comboboxes (they are rolldown lists, and I filled them with the necessary stuff), and I want to store the selected item in a variable (NOT the index of the selected item).

    How can I do that? I feel that this is something plain simple, but this language is a waay too complicated compared to VBasic which I've used recently.

    Please help me!!

    Thank you

    Hankyaku

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Try using the TComboBox->Text property. It is also important that you set the TComboBox->Style property correctly, as this alters the behaviour of the combo box.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    I've tried it. It gets me the Item at Itemindex=0. This index was set at the beginnig of my program to make the ComboBox show the first available option. Does the Style have the options as DropDown, and DropDownList? When it's that, then it's already set.

    If you could spend some minutes with it, I can send the project (not big, just some Ks). You can see then what kinda application this one will be, and see what I've figured out till now.

    Thanks
    Han

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I'm not sure what it is you want to do? I thought you wanted the text of the selected item, rather than its index number. That's what TComboBox->Text does. What is it you want to store?

    If you set the style to csDropDownList, the user may only select from the items in the list. However, if the style is csDropDown, the user may also enter free text, in this case the only way you can retrieve the entry is with the Text property.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    You see what I'd like to do, but the Text Property does not give me the text of the selected item. I use DropDownList.

    The SW I use is Borland C++ Bulider 5.0
    I'll send you the code, okay? You'll see then what I made wrong.

    I'll put some comments with it. It's zipped, and contains all files except the tds file.

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Text Property does not give me the text of the selected item. I use DropDownList

    OK. I see.

    Try this:

    Code:
    AnsiString txt = ComboBox1->Items->Strings[ ComboBox1->ItemIndex ];
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    Hi!

    I've tried the method you've described, and it doesn't work. It seems to "forget" the Index of the selected item.

    Could you please take a look at the source code I attached to my last reply?

    Thanks
    Han

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    OK. But will be tomorrow - I'm leaving for home now.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    Thanks! Have a nice day!

    Han

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Had a look at your code:

    Code:
    >  combocomport->Items->AddObject("1",combocomport);
    >  combocomport->Items->AddObject("2",combocomport);
    Not sure what the purpose of this is. Why are your storing the pointer to the ComboBox in the ComboBox's own string list?

    You can access the pointers in the string list by calling:

    combocomport->Items->Objects[n]

    However, the pointer value returned will always be the same the Sender pointer in an event, such as the ComboBox's OnClick event:

    Code:
    void __fastcall TfrmComminit::combocomportClick(TObject *Sender)
    Why don't you just initialise the boxes with string values, and respond in an click event based on the newly selected index value or text string.

    You could store this somewhere as either an integer index value, which maps to a baud rate, or a string which maps to a baud rate?

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    Hi there!!

    Hmm, there were quite interesting things you've written about.

    The two rows you quoted aren't assigning pointers, but the actual strings in the combo box. I looked up how to fill the combo box with strings, and I found this way you've quoted.

    After this I figured you said to store the actual itemindex in an integer value, right? How can I access a string from the list which has for example itemindex 4?
    Is this something like this?

    combocomport->Items[4]->Text; ??

    I'm really new, so sorry for the dumb questions.

    Thanks
    Han

  12. #12
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    To add a string to a (TComboBox) stringlist, do this:

    Code:
    ComboBox->Items->Add("new string");
    Forget about the AddObject method for the time being. Also, you don't need to initialise the comboboxes in your code. You could enter them in a design time by double clicking on the 'Items' property of the ComboBox in the object inspector.

    >How can I access a string from the list which has for example itemindex 4?

    Like so:

    Code:
    AnsiString selStr = ComboBox->Items->Strings[ ComboBox->ItemIndex];

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    So, let's see if I take this right....

    1st: I define the Combobox strings with the Item property

    2nd: Create the Click events, which will change the itemindex

    3rd: Take the object specified in the itemindex form the combobox string list.

    If I'm right about it, then I still have a problem with step 2. How can I do that? Shall I write a click event of the Comboboxes?

    Thanks
    Han

    BTW: Could you please send me a small example with only 1 Combobox, which would make the thing I'd like to?

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    I've tried to make it, but it seems like the Itemindexes don't change...

  15. #15
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I've put together a quick demo. Hope it helps.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM