Thread: tabs in listbox

  1. #1
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323

    tabs in listbox

    I was wondering if there was a way to format a string inside a listbox to have tabs. Like \t in dos. I am trying to format seperate text box entries into 1 list box entry with each one tabed apart so it will line up with the next entry below it. This way, if the first part of the string has more letters than the one above it, the second part of the strings will line up still. Here is how im putting together the text box entries into 1 string and passing that string. I am using the MSVC++ editor so it does not have the listview feature, just listbox. Here is what i have so far.
    Code:
    char *buf = (char *)GlobalAlloc(GPTR, len + 1);
    char *buf2 = (char *)GlobalAlloc(GPTR, len2 + 1);
    char *buf3 = (char *)GlobalAlloc(GPTR, len3 + 1);
    char *buf4 = (char *)GlobalAlloc(GPTR, len4 + 1);
    GetDlgItemText(hwnd, IDC_COURSE_NAME, buf, len + 1);
    GetDlgItemText(hwnd, IDC_SCORE, buf2, len2 + 1);
    GetDlgItemText(hwnd, IDC_SLOPE, buf3, len3 + 1);
    GetDlgItemText(hwnd, IDC_COURSE_RATING, buf4, len4 + 1);
    			
    char *fullString = (char *)GlobalAlloc(GPTR, (len + len2 + len3 + len4 + 1));
    
    strcat(fullString, buf);
    strcat(fullString, buf2);
    strcat(fullString, buf3);
    strcat(fullString, buf4);
    
    int index = SendDlgItemMessage(hwnd, IDC_SCORES_LIST, LB_ADDSTRING, 0, (LPARAM)fullString);
    The keyboard is the standard device used to cause computer errors!

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Have you looked at the LBS_USETABSTOPS style?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Ok, ive looked at it and tried to figure it out, trial and error sucks. I am not understanding the lParam, what is the array of integers for, how does it relate to how i would put those tabs between each item im putting into the listbox?
    Code:
    EX: 
    Stuart Little	10	1990	Blah
    J. P. 		24	3	Hey to you.
    
    Def:
    lResult = SendMessage(      // returns LRESULT in lResult
         (HWND) hWndControl,      // handle to destination control
         (UINT) LB_SETTABSTOPS,      // message ID
         (WPARAM) wParam,      // = (WPARAM) () wParam;
         (LPARAM) lParam      // = (LPARAM) () lParam; );   
    
    Parameters
    
    wParam
    Specifies the number of tab stops in the list box. 
    
    lParam
    Pointer to the first member of an array of integers containing the 
    tab stops. The integers represent the number of quarters of the 
    average character width for the font that is selected into the list 
    box. For example, a tab stop of 4 is placed at 1.0 character units, 
    and a tab stop of 6 is placed at 1.5 average character units. 
    However, if the list box is part of a dialog box, the integers are in 
    dialog template units. The tab stops must be sorted in ascending 
    order; backward tabs are not allowed.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I don't think I understand what it is you are having the problems with. The LBS_USETABSTOPS style allows the control to process/expand the tab characters in strings. To introduce the tab, put it in the string.

    The message you are using tells the control how far to move when a tab character is encountered.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    This may be an appropriate example. Note: I've never used tab stops.

    Code:
    int tabs[4]={12,12,12,12};
    
    SendMessage(hList,LB_SETTABSTOPS,4,&tabs);
    That is, I assume, how you would code it if you wanted all tab stops to be the same width.

    Personally, I like list view controls. It's my opinion that it organizes data better.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Thanks, i got it working.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Originally posted by stumon
    Thanks, i got it working.
    I was only half right, lol. The listbox will now show tabs, but the array does not seem to change those tab positions. I have tried numerous variations of integers and the tabs stay the same. I am looking to set the first tab position to say 24 because it is a Golf Course name that can be any size, the rest of the information will be integers of 2 or 3 digits. between 60 and 140. So its just the first position i want to set far out to the right, so any course name will fit. Here is what i have tried so far, and the listbox it set to use the LBS_USETABSTOPS style.
    Code:
    int tabStops[4] = {24, 4, 4, 4};
    
    int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_COURSE_NAME));	
    int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_SCORE));
    int len3 = GetWindowTextLength(GetDlgItem(hwnd, IDC_SLOPE));
    int len4 = GetWindowTextLength(GetDlgItem(hwnd, IDC_COURSE_RATING));
    
    // allocate memory for the strings (edit boxes)
    char *buf = (char *)GlobalAlloc(GPTR, len + 1);
    char *buf2 = (char *)GlobalAlloc(GPTR, len2 + 1);
    char *buf3 = (char *)GlobalAlloc(GPTR, len3 + 1);
    char *buf4 = (char *)GlobalAlloc(GPTR, len4 + 1);
    
    //get text from edit boxes and store in memory.
    GetDlgItemText(hwnd, IDC_COURSE_NAME, buf, len + 1);
    GetDlgItemText(hwnd, IDC_SCORE, buf2, len2 + 1);
    GetDlgItemText(hwnd, IDC_SLOPE, buf3, len3 + 1);
    GetDlgItemText(hwnd, IDC_COURSE_RATING, buf4, len4 + 1);
    	
    SendMessage(hwnd, LB_SETTABSTOPS, 1, (LPARAM)tabStops); 
    
    char *fullString = (char *)GlobalAlloc(GPTR, (len + len2 + len3 + len4 + 10));
    strcat(fullString, buf);
    strcat(fullString, "\t");
    strcat(fullString, buf2);
    strcat(fullString, "\t");
    strcat(fullString, buf3);
    strcat(fullString, "\t");
    strcat(fullString, buf4);
    
    SendDlgItemMessage(hwnd, IDC_SCORES_LIST, LB_ADDSTRING, 0, (LPARAM)fullString);
    The keyboard is the standard device used to cause computer errors!

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    It works for me, the attached shows it doing so. Your tabstop positions seem very small and you are only posting one of them although you declare 4, is this intentional?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    The array was 1 because i was testing if it mattered, which it didn't. I figured it out with help from your example. You created windows and set the handle of the listbox to hListbox, i how ever was trying to send the message to the window handle hwnd, instead of the listbox handle hList i created earlier. Thats why it would never change no matter what was in the int array. It works now. Just incase anyone else tries the same thing for future searches on the forums. I have bolded what small mistake there was.
    Code:
    //Previous code shows i was sending the message to hwnd which
    //is my main window handle.
    SendMessage(hwnd, LB_SETTABSTOPS, 4, (long)tabStops);
    
    //new code shows i switch to the correct list handle.
    HWND hList = GetDlgItem(hwnd, IDC_SCORES_LIST);
    SendMessage(hList, LB_SETTABSTOPS, 4, (long)tabStops);
    Thanks a lot adrian.
    The keyboard is the standard device used to cause computer errors!

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    We've all done that before.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

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. Listbox search
    By Elsindes in forum Windows Programming
    Replies: 4
    Last Post: 04-09-2007, 03:47 PM
  3. 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
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM