15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Initial selection of a ComboBox

Mats Mеnhav -- manhav@connectum.skurup.se
Friday, June 28, 1996

-- [ From: Mats Manhav * EMC.Ver #2.5.02 ] --

ENV: MSVC 4.1 Win NT 4.0 beta 1/Win 95

In my application I have a combobox on a propertypage.
The style of the combobox is CBS_DROPDOWNLIST, i.e a 
dropdown list with a static text control.
In the OnInitDialog function I fill this list with strings.
I want to have one of the strings initially selected.
The problem is I don't seem to be able to initially select
a string from the list. (both on NT and Win 95)

Here is the code I use for filling the list (which works fine):

   CComboBox *combo = (CComboBox*)GetDlgItem(IDC_ACCESSGROUP_TIMESCHEDULE);
   combo->AddString("Mats");
   combo->AddString("Karin");
   combo->AddString("Anton");
   combo->AddString("Matilda");
   combo->AddString("Maja");

I have tried to select the second item in the list by doing both of the
following
(neither of them works):
   combo->SetCurSel(1);
   combo->SelectString(-1, "Karin");

The static control remains empty and when I click on the down arrow, nothing
in the list
is selected.

So please tell me the correct way of doing this.

Mats

--
==========================================================================
Mats Mеnhav (Mats Manhav for 7-bit people)
email:manhav@connectum.skurup.se   WWW: http://connectum.skurup.se/~manhav
FAX:  (int) 46 (0) 414 243 05      Phone: (int) 46 (0) 414 243 05         
==========================================================================




MORIN Jean-Marc -- jmc21@planetb.fr
Tuesday, July 02, 1996

[Mini-digest: 5 responses]

Mats Manhav wrote:
> 
> I have tried to select the second item in the list by doing both of the
> following
> (neither of them works):
>    combo->SetCurSel(1);
>    combo->SelectString(-1, "Karin");
> 

I suppose that you have create a variable using class Wizard to handle
the return values of your ComboBox.

The problem is that you put your code into OnInitDialog(), and/or
into the DoDataExchange(), but before the DDX lines included by class wizard

My solution is:
Create a variable of type index to handle the result
In your DoDataExchange set this index with values of 0 before DDX lines
and don't either use SetCurSel()
By default class wizard init your index variable with the -1 values (no selection)
even when you say that minimum is 0, but that's an other misfunction of wizard...

                             Hope I help you...

                                    mjm  :-)
-----From: beriksen@cda.com

     
     I have experienced this behavior when I bind a member variable to the 
     combo box via DDV/DDX in class wizard.  Remove this binding if you've 
     got one and then retry.  If you haven't got a var bound to the combo, 
     disregard this message.
     
     Brian Eriksen
     CDA/Wiesenberger
     beriksen@cda.com

-----From: David Woodward 

In similar situations, I subclass my CB's to CString, set the 
string to my initial value, and DoDataExchange handles the rest.
-----From: Dean Grimm 

Did you define a member variable for the listbox value?  If so, are you
calling UpdateData(FALSE) somewhere after your call to SelectString?  This
can be a problem if the member variable gets out of sync with the selection,
since the call to UpdateData resets the selection to the contents of the
variable.

Hope this helps,
Dean

Dean Grimm           Software Engineer / Cortron corp.

-----From: Vincent Mascart <100425.1337@CompuServe.COM>

Did you verified if you haven't any data bound via DDX to your combobox, like a
CString for the edit item or a int for the current selection.

Any subsequent UpdateData(FALSE)  in your OnInitDialog() will reset the
selection if you have not set these variables.

Vincent Mascart
100425.1337@compuserve.com




Roger Onslow -- Roger_Onslow@compsys.com.au
Monday, July 08, 1996

>In my application I have a combobox on a propertypage.
>...
>Here is the code I use for filling the list (which works fine):
>
>   CComboBox *combo = (CComboBox*)GetDlgItem(IDC_ACCESSGROUP_TIMESCHEDULE);
>...

I never do this "manually" like this, I always do it the easy way using DDX/DDV 
(which ClassWizard even adds the couple of lines of code required for you).

Even if you don't use class wizard, they are heaps easier than stuffing about 
yourself.  Also avoids the need for doing subclassing of controls, which has 
resulted in many messages on the mfc-l list (this is all handled by the 
DDX_Control routine).

Give it a try...

           /|\        Roger Onslow
      ____|_|.\       ============
    _/.........\Senior Software Engineer
   /CCC.SSS..A..\
  /CC..SS...A.A..\   Computer
 /.CC...SS..AAA...\       Systems
/\.CC....SSAA.AA../            Australia
\ \.CCCSSS.AA.AA_/
 \ \...........//      Ph: +61 49 577155
  \ \...._____//      Fax: +61 49 675554
   \ \__|_/\_//    RogerO@compsys.com.au
    \/_/  \//






| Вернуться в корень Архива |