CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2004
    Posts
    75

    Lvn_itemchanging

    Ok, this drives me nuts...

    I've got a listview where the user may select an item from. In response to the item selection, controls on the dialog are set to item-specific values. If an item was previously selected, the entered data will be validated and stored if it is valid. If the data is invalid, an error box is displayed and the original list-control selection is restored.

    The LVN_ITEMCHANGING handler is just like this:
    Code:
    void CMyDlg::OnItemChangingLC(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    	NM_LISTVIEW* pNMListView =reinterpret_cast<NM_LISTVIEW*>(pNMHDR);
    
    	// Allow item change by default.
    	*pResult = static_cast<LRESULT>(FALSE);
    
    	// If an item loses focus.
    	if ( ((pNMListView->uChanged&LVIF_STATE) == LVIF_STATE) &&
    	     ((pNMListView->uOldState&LVIS_FOCUSED)==LVIS_FOCUSED) &&
    	     ((pNMListView->uNewState&LVIS_FOCUSED)==0) )
    	{
    		AfxMessageBox(_T("NO NO NO!"));
    
    		// Prevent the change.
    		*pResult = static_cast<LRESULT>(TRUE);
    	}
    But what should I say: The damned Msgbox will be hit twice with the same **** parameters of pNMListView: The same iItem, the same uChanged, the same uOldState, the same uNewState.

    Why?

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Lvn_itemchanging

    Is it possible that the messagebox itself causes the item to lose focus?

    Try writing debug messages to a text file instead! Just to make sure...

  3. #3
    Join Date
    May 2004
    Posts
    75

    Re: Lvn_itemchanging

    Hi zerver

    Gotcha! If I do not use the MsgBox, I get there only once...
    Ok, then I'll code a workaround.

    Thank you very much!

  4. #4
    Join Date
    Sep 1999
    Location
    France
    Posts
    393

    Re: Lvn_itemchanging

    Hi,

    I have exactly the same problem than you please can you help me for the workaround

    Regards

  5. #5
    Join Date
    May 2004
    Posts
    75

    Re: Lvn_itemchanging

    A possible solution would be

    1) add a string member variable to the dialog, e.g. m_sError
    2) store the change deny message in the member variable instead of displaying it in OnItemChangingLC
    3) In OnItemChangingLC, *post* a user-defined message to your dialog and still return TRUE.
    4) Handle the user-defined message in your dialog class and display the error text stored in the member variable.

    Not genuine, but it works.

  6. #6
    Join Date
    Oct 2012
    Posts
    9

    Re: Lvn_itemchanging

    I found what appears to be you asking the same question again in 2003. I used your possible solution and built on it because it was losing the selection. See my example.

    void CCommentsTabDlg::OnItemchangingCmntsCommentList(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

    if ( pNMListView->uChanged == LVIF_STATE &&
    ( pNMListView->uOldState == LVIS_FOCUSED ||
    pNMListView->uOldState == LVIS_SELECTED ) &&
    ( m_nMode == MODE_ADD || m_nMode == MODE_UPDATE ) ) {
    // Only show message once to prevent the message when focus and selection change.
    if ( pNMListView->uOldState == LVIS_FOCUSED ) {
    if ( m_nMode == MODE_ADD ) {
    m_sUpdateAddMessage = "You cannot select another comment while adding.\n\nYou must finish adding first.";
    }
    else if ( m_nMode == MODE_UPDATE ) {
    m_sUpdateAddMessage = "You cannot select another comment while updating.\n\nYou must finish updating first.";
    }
    PostMessage(WM_UPDATE_ADD_MESSAGE);
    }

    // Do not allow ItemChange
    *pResult = -1;
    }
    else {
    *pResult = 0;
    }
    }

    2003 Thread: http://forums.codeguru.com/showthrea...N_ITEMCHANGING

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Lvn_itemchanging

    Oliver hasn't visited this site for over 6 years.

  8. #8
    Join Date
    Oct 2012
    Posts
    9

    Re: Lvn_itemchanging

    That is not why I posted. I posted because I had the same problem that he did. I just wanted to clarify for anyone like me that finds this post.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Lvn_itemchanging

    Quote Originally Posted by TroyKroening View Post
    That is not why I posted. I posted because I had the same problem that he did. I just wanted to clarify for anyone like me that finds this post.
    Before posting anything you should first have read the Announcement: Before you post....
    Victor Nijegorodov

  10. #10
    Join Date
    Oct 2012
    Posts
    9

    Re: Lvn_itemchanging

    Victor, I looked at the Announcement link that you posted. I read it fully and do not understand how it applies. Can you clarify?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Lvn_itemchanging

    Quote Originally Posted by TroyKroening View Post
    Victor, I looked at the Announcement link that you posted. I read it fully and do not understand how it applies. Can you clarify?
    You seem to be a writer, not a reader...
    Including Code
    If you include listings within your messages, please use the # button or code tags from within the message editors. This will format your code using a non-proportional font and it will preserve spacing. Most importantly, will make your code easier to read.
    Victor Nijegorodov

  12. #12
    Join Date
    Oct 2012
    Posts
    9

    Re: Lvn_itemchanging

    That clarifies. I thought that you were talking about the post of mine that you quoted. I did not know that you were talking about my prior post with code.

  13. #13
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Lvn_itemchanging

    Nobody cares how it works as long as it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured