Thread: Access a Listbox

  1. #1
    Registered User
    Join Date
    Oct 2005

    Access a Listbox

    i have this little problem.
    In my code, i create a listbox, which is a child window from the main window.
    Now, the problem is: how can i access the listbox in the MainWindowProc function?

    How can i set a ID to the ListBox, which will be passed as a message to the WindowProc?

    I would be very thankful if you could give me a short example, because i really have no idea how this works.

    Thank you very mutch.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    When creating a child window, you can use the hMenu argument as a child window identifier. This example uses the identifier 0xED. In real code, you should use a constant, rather than a magic number.
    			CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT("Write here!"), WS_CHILD | WS_VISIBLE,
    			               0, 0, 0, 0,
    			               hwnd, (HMENU) 0xED, GetModuleHandle(NULL), NULL);
    Once, you have a child window with an identifier, you can use the SendDlgItemMessage, GetDlgItemText and SetDlgItemText functions to interact with it:
    SetDlgItemText(hwnd, 0xED, TEXT("Hello"));
    Alternatively, if you need its HWND, you can use the GetDlgItem function:
    MoveWindow(GetDlgItem(hwnd, 0xED), rc.left,, rc.right, rc.bottom, TRUE);
    The alternative method to interact with a child window is to store its HWND when you create it. However, using an identifier is generally preferable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. Adding Strings to ListBox in a Dialog
    By kevins963 in forum C Programming
    Replies: 5
    Last Post: 09-10-2008, 04:36 PM
  3. cant load dialog with listbox, help
    By terracota in forum Windows Programming
    Replies: 2
    Last Post: 11-22-2004, 07:11 PM
  4. How to cast a ListBox item to an int for a switch statment?
    By Swaine777 in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2004, 08:52 PM
  5. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM