Thread: Validate Tree View Label Edit?

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Validate Tree View Label Edit?

    Hello,

    I've written code that creates a new tree view item with a "default label" that I immediately expect the user to change (much like when creating a new folder in Explorer). The text they enter must conform to C naming conventions. But if the user cancels editing the default label I want to change it to something safe anyway.

    My TVN_ENDLABELEDIT handler looks like thus:-
    Code:
    case TVN_ENDLABELEDIT:
    {
        LPNMTVDISPINFO lpTvdi = (LPNMTVDISPINFO)lParam;
    
        if (lpTvdi->item.pszText)
        {
            if (!lstrlen(lpTvdi->item.pszText))
            {
                // Nothing entered; Set to a safe name
                lpTvdi->item.pszText = "t_struct1";
                // This is how dialogs set the return value, in case you didn't know
                SetWindowLong(hDlg, DWL_MSGRESULT, TRUE);
            }
            else
            {
                // Validate
            }
    
        }
        else
        {
            // Canceled; Set to a safe name if necessary
            // HOW?!?
        }
    
        break;
    }
    I've tried changing szText and sending TVM_SETITEM to the tree view control, but it doesn't seem to work. Any ideas?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I meant to post this a couple of days ago.

    One solution you could consider is to post a custom message (with PostMessage) back to the same window. This message will then not be processed until after the TVN_ENDLABELEDIT message has completed, allowing you to use TVM_SETITEM.

    I think the behaviour of Explorer in rejecting invalid characters with a balloon tooltip is nice, although I'm not sure of the details of setting it up.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Don't worry about it. After much thrashing later on the same day I managed to sort it out relatively sweetly.
    For starters, I began using LPSTR_CALLBACK as the text for all the tree view items so it can easily tie in with my structures in memory (that all have "name" members). Then I wrote the C name validation function (smaller than it sounds ) and another function that traverses through my structure list and tries to match a given string with a name (if it does then I change the string and search again).

    Now all I have to do is do the same thing for a list view and make sure that I check where requests are coming from (tree view or list view) so I don't go changing the wrong thing.

    I suppose I could do the balloon tip thing, it would involve subclassing the edit control used by the tree view and handling WM_CHAR messages. But I prefer less direct validation (after, not during), less annoying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource manager tree
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 09-07-2007, 10:27 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Binary Search Tree, Inserting node
    By cheryl_li in forum C Programming
    Replies: 1
    Last Post: 09-13-2003, 03:53 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. How to view and edit grafic vector images like EPS?
    By Fender92 in forum C Programming
    Replies: 3
    Last Post: 10-25-2001, 08:34 AM