Clear combobox contents but leave selected item

I have a dropdown combo box that is pre-populated.  I then expand the dropdown and select the item from the list.  At this point I want the list to be cleared but keep the item that I previously selected.

I've tried both these two methods below, but they only seem to work when a I used the arrow keys and ENTER to select the item.  If I used the mouse, it doesn't work.  Any ideas how do to this?

case IDC_COMBO1:
  if (HIWORD(wParam) == CBN_CLOSEUP)
  {
      wchar_t szBuf[256];
      //Store selection in buffer
      GetDlgItemText(hDlg,IDC_COMBO1,szBuf,256);
      //Clear everything
      SendMessage(GetDlgItem(hDlg,IDC_COMBO1),CB_RESETCONTENT,0,0);
      //place saved buffer back in combo
      SetDlgItemText(hDlg,IDC_COMBO1,szBuf);
  }
  break;

I've also tried:

case IDC_COMBO1:
      if (HIWORD(wParam) == CBN_CLOSEUP)
      {
            wchar_t szbuf[256];
            GetDlgItemText(hDlg,IDC_COMBO1,szbuf,256);
            int i=0,nCount=0;
            nCount = (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCOUNT , 0, 0);
            for (i=0;i<=nCount;i++)
            {      
                  SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_DELETESTRING , (WPARAM) i, 0);
            }
            SetDlgItemText(hDlg,IDC_COMBO1,szbuf);
      }
      break;
LVL 27
PberSolutions ArchitectAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Ooops, sorry, the index of the current selection is most likely to change while deleting the other ithems. Try

    int nCurSel = 0;
    int i=0,nCount=0;
    nCount = (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCOUNT , 0, 0);

    for (i=0;i<=nCount;i++)
    {    
            nCurSel =  (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCURSEL , 0, 0);

        if ( i == nCurSel) continue; // don't touch current selection

        SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_DELETESTRING , (WPARAM) i, 0);

            nCount = (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCOUNT , 0, 0);
    }
0
 
jkrCommented:
You could use

        int nCurSel =  (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCURSEL , 0, 0);

         int i=0,nCount=0;
         nCount = (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCOUNT , 0, 0);
         for (i=0;i<=nCount;i++)
         {    
               if ( i == nCurSel) continue; // don't touch current selection
               SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_DELETESTRING , (WPARAM) i, 0);
         }
0
 
PberSolutions ArchitectAuthor Commented:
I was close... Thanks, it works like a charm.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jkrCommented:
Um, which one of the two? :o)
0
 
PberSolutions ArchitectAuthor Commented:
woops, I thought I accepted the right answer.  I'll try that again.
0
 
jkrCommented:
Thanx, but out of curiosity - did the 1st one work or did you have to use the 2nd?
0
 
PberSolutions ArchitectAuthor Commented:
to be honest I seen the 'Ooops' and went to the 2nd answer.

but for fun I tried it.  Turns out it didn't work unless I make the for loop go backwards:
       int nCurSel =  (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCURSEL , 0, 0);

         int i=0,nCount=0;
         nCount = (int) SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_GETCOUNT , 0, 0);
         for (i=nCount;i>=0;i--)
         {    
               if ( i == nCurSel) continue; // don't touch current selection
               SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_DELETESTRING , (WPARAM) i, 0);
         }
0
All Courses

From novice to tech pro — start learning today.