The problem with running the context menu of the TreeView object on the form. After the user has left-clicked an item in the tree, he needs to double-click on the right mouse button so that the context menu appears. The customer is very annoyed.
The problem appeared after the treeView on the form was used as a data source in the Drag-Drop drag-and-drop procedure implemented by standard MicroSoft tools.
The following code has been added to the click handler:
private void treeView_MouseDown(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { TreeNode node = treeView.GetNodeAt(eX, eY); if (node != null) { // ... некоторые предварительные вычисления, // не влияющие на элементы формы, а меняющие Tag у node //Проблемы появились после добавления этой стандартной функции от MicroSoft treeView.DoDragDrop(node, DragDropEffects.Copy); //Это старый код, который должен остаться и при котором все работало treeView.SelectedNode = node; } } } Drag-Drop itself is processed without problems.
If it is important, then on treeView_AfterSelect a handler is implemented that fills the form elements with information
private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode currentNode = treeView.SelectedNode; if (currentNode != null) { FillAll(currentNode); } } Please tell me how to solve this problem without implementing your own Drag-Drop, or how to explain to the client that this is not a bug in the implementation of DoDragDrop() MicroSoft, but a feature.
A detailed study of this rare phenomenon showed that to stop the procedure of DoDragDrop() not enough just to release the left mouse button. The treeView_MouseUp() handler does not respond to it if the standard DoDragDrop() is used.
UpDate from 12/26/2016: On request, add the code that handles the events of the DragDrop procedure:
1) At the DragEnter event for the ListBox lstBxForSearch; The drag handler is hanged by the following handler:
private void lstBxForSearch_DragEnter(object sender, DragEventArgs e) { // Предварительно сбрасываем флаг допустимости использования контрола lstBxForSearch, // как приемника процедуры DragDop e.Effect = DragDropEffects.None; // Устанавливаем этот флаг, если объект, который тянем, может быть принят // контролом lstBxForSearch, то есть источником служит контрол treeView if (e.Data.GetDataPresent(typeof(TreeNode))) { TreeNode node = e.Data.GetData(typeof(TreeNode)) as TreeNode; if (node != null && node.TreeView == treeView) { //Допустимый источник e.Effect = DragDropEffects.Copy; } } } 2) At the DragDrop event for ListBox lstBxForSearch; such handler is hanged:
private void lstBxForSearch_DragDrop(object sender, DragEventArgs e) { //Анализируем, что принесли на мыше TreeNode node = e.Data.GetData(typeof(TreeNode)) as TreeNode; if (node == null) return; addPlantToSearchList(node); } and correspondingly
private void addPlantToSearchList(TreeNode currentNode) { PlantExtendTag tg = currentNode.Tag as PlantExtendTag; if (tg == null) return; MyDS.PlantShort_ViewRow plantRow = tg.PlantRow; if ((TacsonomyLeve)plantRow.LevelNum == TacsonomyLeve.Kind) { lstBxPlantsForSearch.Items.Add(new NameId(plantRow.Id, currentNode.Text)); } else { toolStripStatusLabel.Text = "Расширенный поиск предусмотрен только по пересечению видов"; } }