TreeView - 无法取消NodeMouseClick()事件并恢复以前的节点选择。 [英] TreeView - Unable to cancel NodeMouseClick() event and restore previous node selection.

查看:236
本文介绍了TreeView - 无法取消NodeMouseClick()事件并恢复以前的节点选择。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个包含多个节点的TreeView控件。在选择节点时,某些信息显示在文本框中。可以修改和保存此信息。意外地,如果用户导航到另一个节点而没有保存,则会显示一个弹出窗口
,要求用户保存或取消该操作。如果单击取消,则选择应保留在先前选定的节点上而不是新节点上。显示弹出窗口的逻辑是在TreeView的NodeMouseClick()事件中编写的。有没有办法实现这个目标?
下面是示例代码段。

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()事件似乎没有提供取消事件的任何信息。我已经尝试使用TreeView的BeforeSelect()事件来使用下面的代码片段取消事件。

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?

谢谢 

Abishek

推荐答案

找到解决方案。在BeforSelect事件中添加弹出逻辑然后设置e.Cancel = true将解决此问题。
Found resolution. Adding pop up logic in BeforSelect event and then setting e.Cancel = true will resolve this issue.


这篇关于TreeView - 无法取消NodeMouseClick()事件并恢复以前的节点选择。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆