Hi,

I'm having a problem with retrieving the text from a combobox. When i do the index of where the text is in the combobox is returned as well as the text.

My code is as follows:

//default video format
  dirac_encoder_presets_t preset = VIDEO_FORMAT_CUSTOM;
 
  //get the video format selected by the user
  VideoFormat = FormatTypeComboBox->Text();
 
  if(VideoFormat == "QCIF")
  {
        preset = VIDEO_FORMAT_QCIF;
  }

The preset value is set to 3/*VIDEO_FORMAT_QCIF*/
where 3 is the index of VIDEO_FORMAT_QCIF.

I'm really baffled by this :rolleyes:

Can anybody help?

Thanks:)

Dani AI

Generated

The behaviour described by usually comes down to two things: a type mismatch (the value you assign/compare is not the VCL string you think it is) or simply seeing the underlying integer of an enum in the debugger and assuming it came from the combobox. 's remark about strcmp is only relevant if the variable you compare is a raw C string; 's point about checking types and environment is exactly the right first step.

Checklist and immediate fixes

  • Verify the type that receives the combobox value. In C++Builder the combobox Text is a VCL String (AnsiString or UnicodeString depending on version). If the left-hand variable is an int or an enum it will not hold a textual value — it will hold a numeric value.
  • Inspect the actual combobox item text at runtime (use Items->Strings[ItemIndex] or log FormatTypeComboBox->Text) to confirm there is no numeric prefix in the stored strings.
  • Prefer mapping by ItemIndex instead of string compares (safer and faster).

Example: map selected index to enum (avoid fragile string compares)

const dirac_encoder_presets_t presetMap[] = {
    VIDEO_FORMAT_QCIF,  // index 0
    VIDEO_FORMAT_CIF,   // index 1
    VIDEO_FORMAT_SQCIF, // index 2
    // ...
};

int idx = FormatTypeComboBox->ItemIndex;
if (idx >= 0 && idx < (int)(sizeof(presetMap)/sizeof(presetMap[0])))
    preset = presetMap[idx];

Alternatives and cautions

  • If preserving a direct mapping from displayed text to enum is required, build a lookup map once (std::map or unordered_map) and look up FormatTypeComboBox->Text.
  • Storing enum values in Items->Objects is possible but requires careful pointer/integer casting; avoid this unless comfortable with platform pointer-size casts.
  • Seeing 3 for preset in the debugger is normal: C++ enums are integral underneath. That 3 can simply be the correct enum value VIDEO_FORMAT_QCIF.

If comparisons are done using raw char* use strcmp; for VCL String the overloaded operator== works fine. The priority is to confirm variable types and map by index or an explicit lookup table to remove ambiguity.

Recommended Answers

All 3 Replies

Shouldn't you be using strcmp instead of == for comparing strings? Or am I just as baffled as you? :eek:

Shouldn't you be using strcmp instead of == for comparing strings?

depends on how VideoFormat is declared.

>> VideoFormat = FormatTypeComboBox->Text();
Is this MS-Windows combo box? or something else, such as *nix? what compiler and os are you using?

I'm using Borland C++ builder.

the strcmp doesn't seem to make a difference either and i tried using ItemIndex instead of text to compare with and the result was still the same. the index infront of the preset value.
Really strange!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.