Getting text from listbox

This is a discussion on Getting text from listbox within the Windows Programming forums, part of the Platform Specific Boards category; I have small problem...I have a listbox in a dialog, and I managed to control double clicks - but how ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115

    Getting text from listbox

    I have small problem...I have a listbox in a dialog, and I managed to control double clicks - but how can I get the value of string that was double-clicked?
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Possibly by using SetDlgItemMessage() And LB_GETCURSEL, LB_GETTEXT options. For example:
    Code:
    int sel;
    
    sel = SendDlgItemMessage( hWnd, IDC_LIST, LB_GETCURSEL, 0, 0 );
    SendDlgItemMessage( hWnd, IDC_LIST, LB_GETTEXT, sel, (LPARAM)selection );
    Where IDC_LIST represents your ListBox.


    - Stack Overflow
    Last edited by Stack Overflow; 02-12-2005 at 11:28 AM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    It just doesn't work (well, it works but it fills the buffer with some strange text...)

    Code:

    Code:
     
    	 char *buf;
    	 int sel; 
    	 if(HIWORD(wParam) == LBN_DBLCLK)
    	 {
    	 sel = SendDlgItemMessage(hwnd, IDC_LST1, LB_GETCURSEL, 0, 0);
    	 SendDlgItemMessage(hwnd, IDC_LST1, LB_GETTEXT, sel, (LPARAM)buf);	
    	 MessageBox(hwnd, buf, "Nekaj", MB_OK | MB_ICONINFORMATION); 
    	 }
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    char *buf; either needs to be:

    Code:
    char buf[512];
    or

    Code:
    char *buf=new char[512];

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    thanks
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You need to allocate memory for the LB_GETTEXT message to use:
    Code:
    char buf[SOME_SUITABLE_SIZE];
    int sel; 
    if(HIWORD(wParam) == LBN_DBLCLK)
      {
      sel = SendDlgItemMessage(hwnd, IDC_LST1, LB_GETCURSEL, 0, 0);
      SendDlgItemMessage(hwnd, IDC_LST1, LB_GETTEXT, sel, (LPARAM)buf);    
      MessageBox(hwnd, buf, "Nekaj", MB_OK | MB_ICONINFORMATION);
      }
    If you know the maximum length of any item in the listbox in question then use that value as SOME_SUITABLE_SIZE. If you prefer to dynamically allocate memory for the buffer then LB_GETTEXTLEN will return the length of a listbox item which you can use to new or malloc a suitable sized buffer.

    edit: well biffed by jverkoey.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 01:23 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 1
    Last Post: 07-13-2002, 06:45 PM
  5. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21