Solved

combobox un-selection

Posted on 2000-03-31
7
252 Views
Last Modified: 2013-12-03
I created a combobox in C++, and used CB_ADDSTRING to fill it.  Even after this, there is no initial selection in the box.  This is what I want.  I'm using CBS_DROPDOWNLIST, so the user can only pick what I've added to the list.

My problem is that I want to somehow 'reset' the dropdown to no initial selection.  I'd like to do this without resetting the content and readding stuff.
0
Comment
Question by:FatalAppExit
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
A combobox is just that a combo.  A combination of am edit box and a list box.  You fill the list box with the CB_ADDSTRING but you must set the "default" selection by setting the edit box portion with a string.  It can be one of the values in the list or it can be something else.  It's what the user will see.  If you don't set this, there will be no text displayed in the box until the user selection happens.

Use the WM_SETTEXT message to set this text.
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
Comment Utility
that's easy:

Just send a CB_SETCURSEL message to your Combobox. The wParam you send will be the index of your strings. Thus if you send SendMessage (control, CB_SETCURSEL, 0, 0) String number zero (the first one) will be selected. If you want to clear the selection just send -1  and the selection disapears.

to make it short:

SendMessage (control, CB_SETCURSEL, -1, 0); will remove the selection. That is the default setting after you created your combobox btw.

Hope this helps,
  Nils Pipenbrinck






0
 

Author Comment

by:FatalAppExit
Comment Utility
Sorry, but I've tried both of those solutions.  This combobox doesn't have the style that gives it an edit -- the user can only pick what I have inserted into the dropdown.  After creation of the dropdown, there is no initial selection.  I don't see it as possible to return it to that state after the user has chosen something.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 32

Accepted Solution

by:
jhance earned 20 total points
Comment Utility
Sorry, I misunderstood your question.

Try it like this.

Create the combobox, fill it, and select the item you want:

            case WM_CREATE:
                  hWndCombo = CreateWindow("combobox", NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
                        0, 0, 300, 200,
                        hWnd, (HMENU)1000, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);

                  SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)"Test1");
                  SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)"Test2");
                  SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)"Test3");
                  SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)"Test4");
                  SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM)"Test5");

                  SendMessage(hWndCombo, CB_SETCURSEL, 3, 0);

                  ShowWindow(hWndCombo, SW_SHOW);
                  break;


Then when the user selects an item, read it out and reselect the original item:

            case WM_COMMAND:
                  wmId    = LOWORD(wParam);
                  wmEvent = HIWORD(wParam);

                  if(LOWORD(wParam) == 1000 && HIWORD(wParam) == CBN_SELCHANGE){
                        int cursel = SendMessage(hWndCombo, CB_GETCURSEL, 0, 0);
                        char lpszText[32];
                        SendMessage(hWndCombo, CB_GETLBTEXT, cursel, (LPARAM)lpszText);

                        MessageBox(hWnd, lpszText, "SELECTION", MB_OK);

                        SendMessage(hWndCombo, CB_SETCURSEL, 3, 0);

                        break;
                  }
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Again I'm not 100% clear on your question but if you want NO selection displayed in the combobox, change the lines that read:

SendMessage(hWndCombo, CB_SETCURSEL, 3, 0);

to be:

SendMessage(hWndCombo, CB_SETCURSEL, -1, 0);

0
 

Author Comment

by:FatalAppExit
Comment Utility
I don't get it.  I swear I tried LB_SETCURSEL, and it didn't work.  Argh.  Anyway... thanks.
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
Comment Utility
here is a complete test program..

tell me if it works please.. if you press the button, it should de-select the content of the dropdown combobox.


nils

<--- cut here <-------


#define WIN32_LEAN_AND_MEAN
#include <windows.h>

HINSTANCE    Instance;
HWND         window;
HWND         ComboBox;

LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
/////////////////////////////////////////////////////////////////////////////
{
  switch (uMsg)
  {
    case WM_COMMAND:
    {
      switch (HIWORD(wParam))
      {
        case 0:
        {
          SendMessage (ComboBox, CB_SETCURSEL, -1, 0);
        } break;
      }
    } break;


    case WM_CLOSE:
    {
      PostQuitMessage (0);
    } break;
  }
  return DefWindowProc (hwnd, uMsg, wParam, lParam);
}




void InitWindow (void)
//////////////////////
{
  WNDCLASS wclass;
  wclass.cbClsExtra    = 0;
  wclass.cbWndExtra    = 0;
  wclass.hbrBackground = (HBRUSH) COLOR_WINDOW;
  wclass.hCursor       = 0;
  wclass.hIcon         = 0;
  wclass.hInstance     = Instance;
  wclass.lpfnWndProc   = WindowProc;
  wclass.lpszClassName = "NilsTestApp";
  wclass.lpszMenuName  = 0;
  wclass.style         = 0;
  RegisterClass (&wclass);
  window = CreateWindow ("NilsTestApp", "NilsTestApp", WS_CAPTION|WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_CLIPCHILDREN, 10, 10, 200, 150, 0, 0, Instance, 0);


  {
    HWND control = CreateWindow ("COMBOBOX", "", WS_BORDER|WS_TABSTOP|WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST, 10, 40, 100, 100, window, 0, Instance, 0);
    SendMessage (control, CB_ADDSTRING, 0, (LPARAM) "16 Bit");
    SendMessage (control, CB_ADDSTRING, 0, (LPARAM) "8 Bit");
    SendMessage (control, CB_ADDSTRING, 0, (LPARAM) "4 Bit");
    SendMessage (control, CB_SETCURSEL, 0, 0);
    ShowWindow (control, SW_SHOW);
    ComboBox = control;
  }

  {
    HWND control = CreateWindow ("BUTTON", "Unmark All", BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE, 10, 10, 100, 22, window, 0, Instance, 0);
    ShowWindow (control, SW_SHOW);
  }

  ShowWindow (window, SW_SHOW);
}



int WINAPI WinMain (HINSTANCE inst, HINSTANCE, LPSTR, int)
//////////////////////////////////////////////////////////
{
  Instance = inst;
  InitWindow();

  MSG msg;
  while (GetMessage(&msg, 0, 0, 0))
  {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
  return 0;
}
















0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now