No title comes in when I create my tree-view item.

This is a discussion on No title comes in when I create my tree-view item. within the Windows Programming forums, part of the Platform Specific Boards category; When I call the function: Code: void AddToTreeView(char cName[MAX_PATH], int nArea) { MessageBox(0,cName,0,0); tvi.pszText=TEXT(cName); //item label tvi.cchTextMax=lstrlen(tvi.pszText); //length of item ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question No title comes in when I create my tree-view item.

    When I call the function:

    Code:
     
    void AddToTreeView(char cName[MAX_PATH], int nArea)
    {
    	MessageBox(0,cName,0,0);
    		tvi.pszText=TEXT(cName);			//item label 
    		tvi.cchTextMax=lstrlen(tvi.pszText);	//length of item label
    		tvis.item=tvi;
    		tvis.hParent=hPrev;					 //parent item of item about to be inserted 
    		hPrev=(HTREEITEM)SendMessage(g_hwndTreeView,TVM_INSERTITEM,0,(LPARAM)&tvis);
    }
    Like this:

    Code:
    AddToTreeView(cFile,1);
    When I run it the MessageBox displays the text that I want. And a item is added to the tree-view. But with no text/title. Why is this? And how can I fix it?

    Thanks, August.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I don't know about the specific problem, but you should declare the first parameter as a pointer so that it can be passed a string of any length
    Code:
    void AddToTreeView(const char* cName, int nArea)
    {
    or using c++ std::string
    Code:
    void AddToTreeView(std::string cName, int nArea)
    {
    Make sure the strings are in a global array of strings (or as a member of a c++ class), such as std::vector<std::string>, so that they stay in scope throughout the duration of the program. If you are using the same buffer to hold the string in each call of the AddToTreeView() function, the value of the string in the current function call overwrites the value from the previous call.
    Last edited by Ancient Dragon; 09-23-2005 at 08:32 AM.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well
    Code:
    void func ( char arr[] )
    is the same as
    Code:
    void func (char *arr)
    putting a size in there does not change that fact.

    Now it has no title because you didn't give it one:
    Code:
    int MessageBox(
    
        HWND hWnd,	// handle of owner window
        LPCTSTR lpText,	// address of text in message box
        LPCTSTR lpCaption,	// address of title of message box  
        UINT uType 	// style of message box
       );

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    The compiler says:

    [QUOTE]invalid conversion from `const char*' to `CHAR*' /[QUOTE]

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    And we say "Show us the new code"

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    tvi.pszText=TEXT(cName);

    What do you think the TEXT macro does for you here?
    Why not look at your preprocessed file and see for yourself.

    Maybe you should reread the chapter in petzold on unicode.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    then remove const keyword or either in the function declaration of typecast it out
    Code:
    tvi.pszText=const_cast<char*>(cName);
    BTW: TEXT macro is good for string literals, not character arrays. So even though the macro might compile, it is useless the way you are using it. The macro does NOT convert char arrays to wchar_t arrays (depending on whether UNICODE is defined or not). If you want to support unicode, then you need to declare the array as TEXT in the functkion prototype

    Code:
    void AddToTreeView(const TCHAR* cName, int nArea)
    {
    
    or
    void AddToTreeView(const TCHAR cName[], int nArea)
    {

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I got this now:

    Code:
    void AddToTreeView(char* cName, int nArea)
    {
            tvi.pszText=const_cast<char*>(cName);           //item label 
            tvi.cchTextMax=lstrlen(tvi.pszText);    //length of item label
            tvis.item=tvi;
            tvis.hParent=hPrev;                     //parent item of item about to be inserted 
            hPrev=(HTREEITEM)SendMessage(g_hwndTreeView,TVM_INSERTITEM,0,(LPARAM)&tvis);
    }
    Still, nothing.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you don't need the const cast if the item isn't const....

    Also where is tvi, tvis, hPrev declared. Oh also gonna move this to the windows board

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    This is really wierd!

    I found this at msdn:
    Code:
    typedef struct tagTVITEM {
    UINT mask;
    HTREEITEM hItem;
    UINT state;
    UINT stateMask;
    LPTSTR pszText;
    int cchTextMax;
    int iImage;
    int iSelectedImage;
    int cChildren;
    LPARAM lParam;
    } TVITEM, *LPTVITEM;
    So I tried this:
    Code:
    LPTSTR cName = "Testing... 1..2..3...";
    tvi.pszText=cName; //item label 
    tvi.cchTextMax=lstrlen(tvi.pszText); //length of item label
    tvis.item=tvi;
    tvis.hParent=hPrev; //parent item of item about to be inserted 
    hPrev=(HTREEITEM)SendMessage(g_hwndTreeView,TVM_IN SERTITEM,0,(LPARAM)&tvis);
    And it didn't work.

    But I see no reason why. My var is the exact same type as the one I am assigning it to.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think the only thing you are doing wrong is not completing the state value.
    Code:
    void AddToTreeView(char* cName, int nArea)
    {
    	TVINSERTSTRUCT tvis = { 0 }; /* Zero out our struct before use. */
    
    	tvis.hParent      = hPrev;
    	tvis.hInsertAfter = TVI_LAST;
    	tvis.item.mask    = TVIF_TEXT; /* Set mask of which members we are supplying. */
    	tvis.item.pszText = cName;
    
    	TreeView_InsertItem(g_hwndTreeView, &tvis); /* Using macro is neater. */
    }

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Wow! Thanks anoytmouse!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 02:17 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM
  5. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 09:02 PM

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