Solved

combobox un-selection

Posted on 2000-03-31
7
259 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Winform not working on 64 bit machine 31 98
commandline.codeplex.com required argumen except when displaying help 2 43
Windows 10 Scheduled Tasks 11 84
PL SQL Developer 7 32
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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 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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

9 Experts available now in Live!

Get 1:1 Help Now