Treeview Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Treeview Problem

    Treeview has a function on "Afterselec t" worked perfectly but when i implemented drag drop function on the treeview, it required a mousedown action. Now u have to click on the node and then click on the node again to get the afterselect.

    How do i make the afterselect function run with just 1 click?
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Hi

    To clarify if I understand what your code is doing:
    1. You have the ability to drag and drop a node - this works fine
    2. You also call some code only when the node is clicked - this is not working requires 2 clicks

    Is this correct?
    Could you also maybe post only the relevant code, please dont dump 400 lines of code unless all the code is relevant to this problem.

    If I had to guess I would say that the mouse down event is not firing on the first click, but rather that the DragStart event is being raised.

    Comment

    • Sl1ver
      New Member
      • Mar 2009
      • 196

      #3
      Correct
      After select did work perfectly before i implemented drag & drop.
      this is my Drag & Drop code

      I've tried putting the after select by Mouse down but then it doesn't work

      Code:
      private void tvDescriptions_MouseDown(object sender, MouseEventArgs e)
              {
                  TreeView tree = (TreeView)sender;
      
                  // Get the node underneath the mouse.
                  TreeNode node = tree.GetNodeAt(e.X, e.Y);
      
                  // Start the drag-and-drop operation with a cloned copy of the node.
                  if (node != null)
                  {
                      tree.DoDragDrop(node, DragDropEffects.Copy);
                   }
                        
      
              }
      
              private void tvDescriptions_DragOver(object sender, DragEventArgs e)
              {
                  // Get the tree.
                  TreeView tree = (TreeView)sender;
      
      
                  // Drag and drop denied by default.
                  e.Effect = DragDropEffects.None;
      
                  // Is it a valid format?
                  if (e.Data.GetData(typeof(TreeNode)) != null)
                  {
                      // Get the screen point.
                      Point pt = new Point(e.X, e.Y);
      
                      // Convert to a point in the TreeView's coordinate system.
                      pt = tree.PointToClient(pt);
      
                      // Is the mouse over a valid node?
                      TreeNode node = tree.GetNodeAt(pt);
                      if (node != null)
                      {
                          e.Effect = DragDropEffects.Copy;
                          tree.SelectedNode = node;
      
                      }
                  }
              }
      
              private void tvDescriptions_DragDrop(object sender, DragEventArgs e)
              {
                  try
                  {
                      // Get the tree.
                      TreeView tree = (TreeView)sender;
      
                      tree.BeginUpdate();
      
                      // Get the screen point.
                      Point pt = new Point(e.X, e.Y);
      
                      // Convert to a point in the TreeView's coordinate system.
                      pt = tree.PointToClient(pt);
      
                      // Get the node underneath the mouse.
                      TreeNode node = tree.GetNodeAt(pt);
      
                      TreeNode childNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
      
                      // Remove this node
                      tree.Nodes.Remove(childNode);
      
                      // Add the node to the new parent
                      node.Nodes.Add(childNode);
      
                      // Show the newly added node if it is not already visible.
                      node.Expand();
      
                      tree.EndUpdate();
      
                      UpdateDB(childNode.Tag, node.Tag);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("Error: " + ex.Message);
                  }
              }
              private void UpdateDB(object Id, object parentId)
              {
                  OleDbConnection mConn = new OleDbConnection(strCon);
      
                  mConn.Open();
      
                  string UpdateStr = @"UPDATE [qx_AssetDescription] SET ads__ads_id = @parentId where ads__ID = @Id";
      
                  OleDbCommand myCmd = mConn.CreateCommand();
                  myCmd.CommandText = UpdateStr;
                  try
                  {
                      myCmd.Parameters.AddWithValue("parentId", parentId);
                      myCmd.Parameters.AddWithValue("Id", Id);
      
                      myCmd.ExecuteNonQuery();
                      //MessageBox.Show("Data added to database");
      
                  }
                  catch (Exception ed)
                  {
                      MessageBox.Show("Error: " + ed.Message, "Error");
                  }
                  finally
                  {
                      myCmd.Dispose();
                      mConn.Close();
                      mConn.Dispose();
                      
                      
                  }
      
              }
      and here is my after select code

      Code:
                  string ConnString = Properties.Settings.Default.GriffenConnectionString.ToString();
      
                  OleDbConnection Conn = new OleDbConnection(ConnString);
                  int ParentID = 0;
      
                  LoadDescBoxes();
      
                  string sql = "";
                  DataSet dsFillDesc = new DataSet();
                  string ChkTicked = "";
      
                  try
                  {
                      sql = "Select * from qx_AssetDescription Where ADS__ID = " + tvDescriptions.SelectedNode.Tag.ToString();
                      OleDbDataAdapter daDescFill = new OleDbDataAdapter(sql.ToString(), Conn);
                      daDescFill.Fill(dsFillDesc, "qx_AssetDescription");
      
                      if (dsFillDesc != null)
                      {
                          txtDescShort.Text = Convert.ToString(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__SHORT"]);
                          txtDescLong.Text = Convert.ToString(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__LONG"]);
                          ChkTicked = Convert.ToString(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__Common"]);
                          cboDescParent.SelectedValue = Convert.ToInt32(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__ADS_ID"].ToString());
      
                          if (ChkTicked == "True")
                          {
                              chkDescCommon.Checked = true;
                          }
                          else
                          {
                              chkDescCommon.Checked = false;
                          }
      
                          if (dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__ADS_ID"].ToString() == "0")
                          {
                              cboDescParent.Visible = false;
                              chkDescSelRoot.Checked = true;
                              lblDescParent.Visible = false;
                          }
                          else
                          {
                              ParentID = Convert.ToInt32(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__ADS_ID"].ToString());
                              cboDescParent.Visible = true;
                              chkDescSelRoot.Checked = false;
                              lblDescParent.Visible = true;
                          }
                      }
                  }
      
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message.ToString());
                  }
                  finally
                  {
                      dsFillDesc.Clear();
                      dsFillDesc.Dispose();
                      Conn.Close();
                      txtDescShort.Visible = true;
                      txtDescLong.Visible = true;
                      chkDescCommon.Visible = true;
                      chkDescSelRoot.Visible = true;
                      btnDescriptionCancel.Visible = false;
                      lblDescLong.Visible = true;
                      lblDescription.Visible = true;
                      lblDescShort.Visible = true;
                      btnDescriptionSubmit.Visible = false;
                      btnDescriptionUpdate.Enabled = true;
                      btnDescriptionDelete.Enabled = true;
                  }
              }

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        It sounds like "afterselec t" still works fine. After all it is working when you just click on a node. Dropping onto a node just isn't a selection so it doesn't fire the event.

        You could call the method used from "AfterSelec t" at the end of the DragDrop operation. You already know the node since it just received the Drop. (Or initiated the drag. I'm not sure which node you want selected at the end of DnD operation) But use that node as the Object sender parameter when calling the AfterSelect method

        Comment

        • cloud255
          Recognized Expert Contributor
          • Jun 2008
          • 427

          #5
          Another solution would be to check which mouse button was pressed in the MouseDown event handler. Then you could say that if the user used the left mouse button, select the node. If the right mouse button was used then start the DragDrop operation.

          Comment

          • Sl1ver
            New Member
            • Mar 2009
            • 196

            #6
            Thanx for all the help guys, heres how i fixed it
            Code:
            TreeView tree = (TreeView)sender;
            
                            // Get the node underneath the mouse.
                            TreeNode node = tree.GetNodeAt(e.X, e.Y);
                                   
                            tvDescriptions.SelectedNode = node;
            
                            if (MouseButtons.Right.Equals(MouseButtons))
                            {
                                // Start the drag-and-drop operation with a cloned copy of the node.
                                if (node != null)
                                {
                                    tree.DoDragDrop(node, DragDropEffects.Copy);
                                }
                            }

            Comment

            Working...