Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[VisualC++] combo box: récupérer nouvelle ET ancienne sèlection lors d'un CBN_SELCHANGE

2 réponses
Avatar
Nordnet News
Bonjour,

Pour mes études, je développe une application en C basée sur l'api win32
avec VisualC++ sans utiliser les MFC ( C++ non autorisé).

Je recherche le moyen de mémoriser les valeurs d'un combobox avant et aprés
sa modification (j'ai besoin de ces 2 valeurs pour recalculer une autre
valeur affichée ds un edit box). Je ne veux(peux) pas utiliser de variable
globale et j'aimerais ne pas passer par un edit box invisible...

Aprés de lonnnnnnnnnngue recherche, j'ai trouvé qqchose qui semblait
convenir parfaitement. Mais impossible à mettre en oeuvre, j'obtiens
toujours la nouvelle valeur.

voici l'article trouvé et la partie de code de mon programme:

****************************************************************************
*************************

Processing CBN_SELCHANGE Notification Message

Article ID: Q66365

----------------------------------------------------------------------

The information in this article applies to:

- Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1

- Microsoft Win32 SDK versions 3.1, 3.5, 3.51, and 4.0

----------------------------------------------------------------------

SUMMARY

=======

When a combo box receives a CBN_SELCHANGE notification message,

GetDlgItemText() will give the text of the previous selection and not

the text of the new selection.

To get the text of the new selection, send the CB_GETCURSEL message to

retrieve the index of the new selection and then send a CB_GETLBTEXT

message to obtain the text of that item.

MORE INFORMATION

================

When an application receives the CBN_SELCHANGE notification message,

the edit/static portion of the combo box has not been updated. To

obtain the new selection, send a CB_GETLBTEXT message to the combo box

control. This message places the text of the new selection in a

specified buffer. The following is a brief code fragment:

... /* other code */

case CBN_SELCHANGE:

hCombo = LOWORD(lParam); /* Get combo box window handle */

/* Get index of current selection and then the text of that selection

*/

index = SendMessage(hCombo, CB_GETCURSEL, (WORD)0, 0L);

SendMessage(hCombo, CB_GETLBTEXT, (WORD)index, (LONG)buffer);

break;

... /* other code */

NOTE: For Win32 applications, change the WORD and LONG casts to WPARAM and

LPARAM, respectively.

Additional reference words: 3.00 3.10 3.50 3.51 4.00 95 combobox

KBCategory: kbprg

KBSubcategory: UsrCtl

****************************************************************************
******************************************

Mon code:

(...)

switch (message)

{

case WM_INITDIALOG :


//*********************initialisation de la boite de
dialogue*******************


case WM_CLOSE:

EndDialog(hDlg,NULL);

break;


case WM_COMMAND :

switch(LOWORD(wParam))

{


case IDC_OSCILLO_CH1_PROBE :


if (HIWORD (wParam) == CBN_SELCHANGE)

{

//Recuperation de la valeur de Probe avant modification ----> eh bien
malheureusement, non...retourne la valeur de la nouvelle sélection !!!
Arrrrgh!!!

GetDlgItemText(hDlg, IDC_OSCILLO_CH1_PROBE,buffer, 10); // idem avec
hCtrl=GetDlgItem(hDlg,IDC_OSCILLO_CH1_PROBE);

GetWindowText(hCtrl,buffer,10);

sscanf(buffer,"%d",&OldProbe);

//Recuperation du Probe dans le combo box aprés modification par
l'utilisateur

hCtrl=GetDlgItem(hDlg,IDC_OSCILLO_CH1_PROBE);

Select=SendMessage(hCtrl,CB_GETCURSEL, (WORD)0, 0L);

SendMessage(hCtrl,CB_GETLBTEXT,(WPARAM)Select,(LPARAM)(LPCSTR)buffer);

sscanf(buffer,"%d",&NewProbe);

}

break;

case IDC_OSCILLO_CH_CANCEL:

EndDialog(hDlg,NULL);

return TRUE;

break;


case IDC_OSCILLO_CH_OK:

//********* ......etc...........**************//



J'ai essayé aussi comme ça

case WM_COMMAND :

switch(LOWORD(wParam))

{

case IDC_OSCILLO_CH1_PROBE:

//Recuperation du Probe dans le combo box avant modification par
l'utilisateur mais ça ne fonctionne tjrs pas! (retourne nouvelle valeur)

hCtrl=GetDlgItem(hDlg,IDC_OSCILLO_CH1_PROBE);

GetWindowText(hCtrl,buffer,10);

sscanf(buffer,"%d",&OldProbe);


if (HIWORD (wParam) == CBN_SELCHANGE)

{


//Recuperation du Probe dans le combo box aprés modification par
l'utilisateur

Select=SendMessage(GetDlgItem(hDlg,IDC_OSCILLO_CH1_PROBE),CB_GETCURSEL,
(WORD)0, 0L);

SendMessage(GetDlgItem(hDlg,IDC_OSCILLO_CH1_PROBE),CB_GETLBTEXT,(WPARAM)Sele
ct,(LPARAM)(LPCSTR)buffer);

sscanf(buffer,"%d",&NewProbe);



où est l'erreur?

Quelqu'un aurait-il une autre idée?

Merci d'avance pour votre aide.

Je suis vraiment dans la m... car le temps passe et je reste bloqué
la-dessus!

2 réponses

Avatar
Christian ASTOR
Nordnet News a écrit:

Processing CBN_SELCHANGE Notification Message


> Article ID: Q66365

Ne s'applique qu'à CBS_DROPDOWN, pas CBS_DROPDOWNLIST
Avatar
Nordnet News
"Christian ASTOR" a écrit dans le message de
news:3feeec08$0$6976$
Nordnet News a écrit:

> Processing CBN_SELCHANGE Notification Message
> Article ID: Q66365

Ne s'applique qu'à CBS_DROPDOWN, pas CBS_DROPDOWNLIST




j'ai juste modifié la propriété du combo et maintenant, ça marche !!! comme
souvent, il suffit d'un petit rien...
un énorme merci !!!!!