none
TreeView - Unable to cancel NodeMouseClick() event and restore previous node selection. RRS feed

  • Question

  • Hi,

    Consider a TreeView control containing multiple nodes. On selecting a node, certain information is displayed on a text box. This information can be modified and saved. Accidentally, if user navigates to a different node without saving, a pop is displayed asking user to save or cancel the action. If cancel is clicked, selection should remain on the previously selected node instead of a new node. The logic to display pop up is written in NodeMouseClick() event of TreeView. Is there any way to achieve this ?. Below is the sample code snippet.

      private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
      {
           DialogResult dr = MessageBox.Show("Cancel Clicked......!!!","Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
           if(dr == DialogResult.Cancel)
           {
                    //Need to handle previous node selection
           }
      }


    NodeMouseClick() event doesn't seems to be providing any information to cancel the event. I have already tried BeforeSelect() event of TreeView to cancel the event using below code snippet.

     private void TreeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
     {
            e.Cancel = true;
     }

    But the above code removes the selection on all the nodes and doesn't suffice the need. Is there any way we can retain previous selection?

    Thanks 

    Abishek

    Monday, June 10, 2019 10:58 AM

All replies

  • Found resolution. Adding pop up logic in BeforSelect event and then setting e.Cancel = true will resolve this issue.
    Monday, June 10, 2019 2:56 PM
  • Hi Abishek Alva,

    Thank you for sharing.

    Please mark the solution as answer. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 11, 2019 2:14 AM
    Moderator