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
Solved

combobox un-selection

Posted on 2000-03-31
7
262 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clicking on Print  property changes format of printing result 1 82
Issue testing the Class Library 4 58
Example of WebAPI 7 33
Recommendation vb6 to vb.net or others 14 175
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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