I have a combobox in a dialog which I populate in the resource editor with this string:

-12;-11;-10;-9;-8;-7;-6;-5;-4;-3;-2;-1;0;1;2;3;4;5;6;7;8;9;10;11;12

If the user chooses a new number from the combobox lots of other things happen in the dialog, then I select the number chosen again, this time programatically. (Some numbers are not allowed and the calculation is not simple.) The combo is a DropList type.

Anyway the problem is that if the user selects "-1", when I use

const int ikCurSel = m_cbInclinePcent.SelectString (-1,szIncline) ;

it all works, the correct string is selected, as long as I do not use szIncline = "-1". If I try to select "-1" the combo selects the first string, ikCurSel becomes 0, and the actual string selected is "-12". So the user selects "-1" then sees his selection change to "-12". If he selects any other value, "-2" for example, or "11" then all works as it should.

The first -1 in the SelectString call is called nStartAfter and means "search for a match from and including the first string". The second parameter is a string, with, in this case "-1" in it, as a string.

Summarising: Why does

 SelectString (-1,"-1") ;

not work, selecting the first string in the list rather than the 12th ?

SelectString() does not search for whole strings, only for the number of characters that are in the second parameter. So if you pass "-1" then it will consider "-12" as a match because it only looks at the first two characters. To solve you problem you should call SelctString() again with the first paremter set to the value returned by the first call to SeleectString().

Another way to solve the problem, which might be even better solution, is to capture the WM_COMPAREITEM event so that you can control how the strings are compared. See the Remarks section here In order to do that you will have to subclass the CComboBox control (see tutorial here)

Actually FindStringExact is what I should have used, together with SetCurSel.

I hope I remember that in the future...!

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.