Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

combobox un-selection

Posted on 2000-03-31
7
Medium Priority
?
271 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 32

Accepted Solution

by:
jhance earned 80 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 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…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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