Solved

combobox un-selection

Posted on 2000-03-31
7
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 2672751
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
ID: 2672753
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
ID: 2673425
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 32

Accepted Solution

by:
jhance earned 20 total points
ID: 2673537
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
ID: 2673556
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
ID: 2675140
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
ID: 2675867
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

630 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