473,480 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Treeview Problem

Sl1ver
196 New Member
Treeview has a function on "Afterselect" 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?
May 5 '09 #1
5 3023
cloud255
427 Recognized Expert Contributor
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.
May 5 '09 #2
Sl1ver
196 New Member
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

Expand|Select|Wrap|Line Numbers
  1. private void tvDescriptions_MouseDown(object sender, MouseEventArgs e)
  2.         {
  3.             TreeView tree = (TreeView)sender;
  4.  
  5.             // Get the node underneath the mouse.
  6.             TreeNode node = tree.GetNodeAt(e.X, e.Y);
  7.  
  8.             // Start the drag-and-drop operation with a cloned copy of the node.
  9.             if (node != null)
  10.             {
  11.                 tree.DoDragDrop(node, DragDropEffects.Copy);
  12.              }
  13.  
  14.  
  15.         }
  16.  
  17.         private void tvDescriptions_DragOver(object sender, DragEventArgs e)
  18.         {
  19.             // Get the tree.
  20.             TreeView tree = (TreeView)sender;
  21.  
  22.  
  23.             // Drag and drop denied by default.
  24.             e.Effect = DragDropEffects.None;
  25.  
  26.             // Is it a valid format?
  27.             if (e.Data.GetData(typeof(TreeNode)) != null)
  28.             {
  29.                 // Get the screen point.
  30.                 Point pt = new Point(e.X, e.Y);
  31.  
  32.                 // Convert to a point in the TreeView's coordinate system.
  33.                 pt = tree.PointToClient(pt);
  34.  
  35.                 // Is the mouse over a valid node?
  36.                 TreeNode node = tree.GetNodeAt(pt);
  37.                 if (node != null)
  38.                 {
  39.                     e.Effect = DragDropEffects.Copy;
  40.                     tree.SelectedNode = node;
  41.  
  42.                 }
  43.             }
  44.         }
  45.  
  46.         private void tvDescriptions_DragDrop(object sender, DragEventArgs e)
  47.         {
  48.             try
  49.             {
  50.                 // Get the tree.
  51.                 TreeView tree = (TreeView)sender;
  52.  
  53.                 tree.BeginUpdate();
  54.  
  55.                 // Get the screen point.
  56.                 Point pt = new Point(e.X, e.Y);
  57.  
  58.                 // Convert to a point in the TreeView's coordinate system.
  59.                 pt = tree.PointToClient(pt);
  60.  
  61.                 // Get the node underneath the mouse.
  62.                 TreeNode node = tree.GetNodeAt(pt);
  63.  
  64.                 TreeNode childNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
  65.  
  66.                 // Remove this node
  67.                 tree.Nodes.Remove(childNode);
  68.  
  69.                 // Add the node to the new parent
  70.                 node.Nodes.Add(childNode);
  71.  
  72.                 // Show the newly added node if it is not already visible.
  73.                 node.Expand();
  74.  
  75.                 tree.EndUpdate();
  76.  
  77.                 UpdateDB(childNode.Tag, node.Tag);
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 MessageBox.Show("Error: " + ex.Message);
  82.             }
  83.         }
  84.         private void UpdateDB(object Id, object parentId)
  85.         {
  86.             OleDbConnection mConn = new OleDbConnection(strCon);
  87.  
  88.             mConn.Open();
  89.  
  90.             string UpdateStr = @"UPDATE [qx_AssetDescription] SET ads__ads_id = @parentId where ads__ID = @Id";
  91.  
  92.             OleDbCommand myCmd = mConn.CreateCommand();
  93.             myCmd.CommandText = UpdateStr;
  94.             try
  95.             {
  96.                 myCmd.Parameters.AddWithValue("parentId", parentId);
  97.                 myCmd.Parameters.AddWithValue("Id", Id);
  98.  
  99.                 myCmd.ExecuteNonQuery();
  100.                 //MessageBox.Show("Data added to database");
  101.  
  102.             }
  103.             catch (Exception ed)
  104.             {
  105.                 MessageBox.Show("Error: " + ed.Message, "Error");
  106.             }
  107.             finally
  108.             {
  109.                 myCmd.Dispose();
  110.                 mConn.Close();
  111.                 mConn.Dispose();
  112.  
  113.  
  114.             }
  115.  
  116.         }
  117.  
and here is my after select code

Expand|Select|Wrap|Line Numbers
  1.  
  2.             string ConnString = Properties.Settings.Default.GriffenConnectionString.ToString();
  3.  
  4.             OleDbConnection Conn = new OleDbConnection(ConnString);
  5.             int ParentID = 0;
  6.  
  7.             LoadDescBoxes();
  8.  
  9.             string sql = "";
  10.             DataSet dsFillDesc = new DataSet();
  11.             string ChkTicked = "";
  12.  
  13.             try
  14.             {
  15.                 sql = "Select * from qx_AssetDescription Where ADS__ID = " + tvDescriptions.SelectedNode.Tag.ToString();
  16.                 OleDbDataAdapter daDescFill = new OleDbDataAdapter(sql.ToString(), Conn);
  17.                 daDescFill.Fill(dsFillDesc, "qx_AssetDescription");
  18.  
  19.                 if (dsFillDesc != null)
  20.                 {
  21.                     txtDescShort.Text = Convert.ToString(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__SHORT"]);
  22.                     txtDescLong.Text = Convert.ToString(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__LONG"]);
  23.                     ChkTicked = Convert.ToString(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__Common"]);
  24.                     cboDescParent.SelectedValue = Convert.ToInt32(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__ADS_ID"].ToString());
  25.  
  26.                     if (ChkTicked == "True")
  27.                     {
  28.                         chkDescCommon.Checked = true;
  29.                     }
  30.                     else
  31.                     {
  32.                         chkDescCommon.Checked = false;
  33.                     }
  34.  
  35.                     if (dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__ADS_ID"].ToString() == "0")
  36.                     {
  37.                         cboDescParent.Visible = false;
  38.                         chkDescSelRoot.Checked = true;
  39.                         lblDescParent.Visible = false;
  40.                     }
  41.                     else
  42.                     {
  43.                         ParentID = Convert.ToInt32(dsFillDesc.Tables["qx_AssetDescription"].Rows[0]["ADS__ADS_ID"].ToString());
  44.                         cboDescParent.Visible = true;
  45.                         chkDescSelRoot.Checked = false;
  46.                         lblDescParent.Visible = true;
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             catch (Exception ex)
  52.             {
  53.                 MessageBox.Show(ex.Message.ToString());
  54.             }
  55.             finally
  56.             {
  57.                 dsFillDesc.Clear();
  58.                 dsFillDesc.Dispose();
  59.                 Conn.Close();
  60.                 txtDescShort.Visible = true;
  61.                 txtDescLong.Visible = true;
  62.                 chkDescCommon.Visible = true;
  63.                 chkDescSelRoot.Visible = true;
  64.                 btnDescriptionCancel.Visible = false;
  65.                 lblDescLong.Visible = true;
  66.                 lblDescription.Visible = true;
  67.                 lblDescShort.Visible = true;
  68.                 btnDescriptionSubmit.Visible = false;
  69.                 btnDescriptionUpdate.Enabled = true;
  70.                 btnDescriptionDelete.Enabled = true;
  71.             }
  72.         }
  73.  
May 5 '09 #3
tlhintoq
3,525 Recognized Expert Specialist
It sounds like "afterselect" 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 "AfterSelect" 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
May 5 '09 #4
cloud255
427 Recognized Expert Contributor
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.
May 5 '09 #5
Sl1ver
196 New Member
Thanx for all the help guys, heres how i fixed it
Expand|Select|Wrap|Line Numbers
  1. TreeView tree = (TreeView)sender;
  2.  
  3.                 // Get the node underneath the mouse.
  4.                 TreeNode node = tree.GetNodeAt(e.X, e.Y);
  5.  
  6.                 tvDescriptions.SelectedNode = node;
  7.  
  8.                 if (MouseButtons.Right.Equals(MouseButtons))
  9.                 {
  10.                     // Start the drag-and-drop operation with a cloned copy of the node.
  11.                     if (node != null)
  12.                     {
  13.                         tree.DoDragDrop(node, DragDropEffects.Copy);
  14.                     }
  15.                 }
  16.  
May 5 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
7571
by: Ian Powell | last post by:
Hi I've got objects in an sorted ArrayList like: P:\ P:\\DOCS P:\\i386 P:\\i386\ASMS P:\\i386\ASMS\1000 P:\\i386\ASMS\1000\MSFT
0
1754
by: Björn Bengtsson | last post by:
Hello! I have an urgent problem concerning ASP.NET, ADO.NET, SQL Server, XML and the TreeView control. I have two tables; one describing the products and one describing their relationships. A...
4
10116
by: Karim El Jed | last post by:
Hi, I'm trying to expand a special Node of my TreeView from Codebehind. I have a TreeView on a page for navigating to another site. On the other tsite here is the same TreeView more precisely a...
14
6783
by: Evan Kontos | last post by:
I am trying to implement a Treeview w/an XML file and I even copied and pasted examples from MSDN but can't get them to work. Any suggestions welcome. XML File <TREENODES> <TREENODE...
4
2849
by: Jeff Beem | last post by:
I have a class that inherits from TreeView. The treeview has tooltips for the nodes but they're showing up behind the form. Has anyone seen this? Are there any known fixes? Thanks in advance,...
3
3798
by: christof | last post by:
I've got a really easy problem, please help me: There are two pages in one I'm creating a TreeView like that: TreeView dbTree = new TreeView(); TreeNode dbTreeRoot = new TreeNode("Root");...
3
1615
by: William Sullivan | last post by:
I desperately want to replace my hand-made ajax treeview with the 2.0 TreeView. The webpage I'm designing uses the postbacks to dynamically fill the treeview. The reason why I'm doing this is...
8
12724
by: Matt MacDonald | last post by:
Hi All, I have a form that displays hierarchical categories in a treeview. Ok so far so good. What I was to do is have users be able to select a node in the treeview as part of filling out the...
1
3572
by: Nikron | last post by:
Hi, I'm having an issue with the ASP.NET 2.0 Treeview control and persisting its' state accross requests. My Control is embedded within a master page and is used for site navigation. My problem...
0
2159
by: bsturg21 | last post by:
Hello, I have an app that has a custom treeview which inherits the base treeview class and I am having a problem with the way the treeview is being redrawn. Each node in the treeview represents a...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.