7 Replies - 10329 Views - Last Post: 11 February 2009 - 07:29 AM Rate Topic: ***** 1 Votes

#1 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

TreeView Nodes and Context Menus

Posted 10 February 2009 - 11:20 AM

I know there is a problem with these stupid treeview nodes and getting the ACTUAL node that user clicks on, so I am not surprised by this problem. Basically what is happening is this:

I have a context menu, and it is assigned to only a certain level of nodes. Also the functions in the context menu execute correctly, the problem is they don't execute on the correct node. It is always the previously selected node, so if I want it to work correctly I have to left click on the node, then right click on it and it everything is peachy-keen.

Alright, here is the code I have for one of the context menu strips:

Private Sub DuplicateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DuplicateToolStripMenuItem.Click
	   

		Dim node As TreeNode

		node = Me.TreeView1.SelectedNode  <--- I am pretty sure THIS is the problem

		'More unrelated code here

	End Sub




As I said I already knew there was a problem so here is how I fixed it before:

Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
		Dim item As String
		Dim selected As String = ""
		Dim i As Integer = 0

		selected = TreeView1.GetNodeAt(New Point(e.X, e.Y)).Text

		'More unrelated code here


	End Sub



However that is not a viable option as i am not getting the variable e as a TreeNodeMouseClickEvenArgs

Alright, if I was not clear enough (which I am sure that is the case) just post any further questions and I will answer them as quickly as I can. Thanks for any future help (and you can be sure you will get a thanks :P)

This post has been edited by MrWobbles: 11 February 2009 - 07:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: TreeView Nodes and Context Menus

#2 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: TreeView Nodes and Context Menus

Posted 10 February 2009 - 11:27 AM

i'm just curious have you added the context menu to the node? because there is a property value on the individual node for a context menu
Public Class Form1

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim node1 As New TreeNode("new node1")
		Dim node2 As New TreeNode("new node2")
		node1.ContextMenuStrip = Me.ContextMenuStrip1
		node2.ContextMenuStrip = Me.ContextMenuStrip2
		Me.TreeView1.Nodes.Add(node1)
		Me.TreeView1.Nodes.Add(node2)
	End Sub
End Class


This post has been edited by dklingman: 10 February 2009 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: TreeView Nodes and Context Menus

Posted 10 February 2009 - 11:29 AM

Yes, the context menu is added to the nodes that I want it to be on. The problem is not that it doesn't show up, or that it doesn't perform the functions that I tell it to. The problem is that it does it on the wrong node.

This post has been edited by MrWobbles: 10 February 2009 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: TreeView Nodes and Context Menus

Posted 10 February 2009 - 11:40 AM

give this a try. just drop this into a plan old form to see if this is kinda what you are looking for

Public Class Form1

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim tv As New TreeView
		Dim ctxm1 As New ContextMenuStrip
		Dim ctxm2 As New ContextMenuStrip
		Dim ts1 As New ToolStripMenuItem("test1")
		Dim ts2 As New ToolStripMenuItem("test2")
		ctxm1.Items.Add(ts1)
		ctxm2.Items.Add(ts2)

		AddHandler ts1.Click, AddressOf ts1_click
		AddHandler ts2.Click, AddressOf ts2_click

		Me.Controls.Add(tv)
		Dim node1 As New TreeNode("new node1")
		Dim node2 As New TreeNode("new node2")
		node1.ContextMenuStrip = ctxm1
		node2.ContextMenuStrip = ctxm2
		tv.Nodes.Add(node1)
		tv.Nodes.Add(node2)
	End Sub

	Private Sub ts1_click(ByVal sender As Object, ByVal e As EventArgs)
		MsgBox("test1")
	End Sub

	Private Sub ts2_click(ByVal sender As Object, ByVal e As EventArgs)
		MsgBox("test2")
	End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

#5 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: TreeView Nodes and Context Menus

Posted 10 February 2009 - 11:51 AM

That would work, but the problem is If i tried to use a different context menu for every node that was in the treeview, it would be quite inundated with them. I would like to use the same context menu for each different node (which is how I have it set up now).

Also with yours if you'll notice, it does work, but if you were to left click on test 1, then right click on test2 and call the function - it works, but test 1 is still the selected node. I want test 2 to become the selected node, and that would correct any problems I am having.
Was This Post Helpful? 0
  • +
  • -

#6 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: TreeView Nodes and Context Menus

Posted 10 February 2009 - 12:25 PM

i think i know what you want. hopefully i'm right now.

form designer code
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		Me.components = New System.ComponentModel.Container
		Dim TreeNode1 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Node0")
		Dim TreeNode2 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Node1")
		Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
		Me.TestToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
		Me.TreeView1 = New System.Windows.Forms.TreeView
		Me.ContextMenuStrip1.SuspendLayout()
		Me.SuspendLayout()
		'
		'ContextMenuStrip1
		'
		Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.TestToolStripMenuItem})
		Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
		Me.ContextMenuStrip1.Size = New System.Drawing.Size(105, 26)
		'
		'TestToolStripMenuItem
		'
		Me.TestToolStripMenuItem.Name = "TestToolStripMenuItem"
		Me.TestToolStripMenuItem.Size = New System.Drawing.Size(104, 22)
		Me.TestToolStripMenuItem.Text = "test"
		'
		'TreeView1
		'
		Me.TreeView1.Location = New System.Drawing.Point(54, 66)
		Me.TreeView1.Name = "TreeView1"
		TreeNode1.Name = "Node0"
		TreeNode1.Text = "Node0"
		TreeNode2.Name = "Node1"
		TreeNode2.Text = "Node1"
		Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {TreeNode1, TreeNode2})
		Me.TreeView1.Size = New System.Drawing.Size(121, 97)
		Me.TreeView1.TabIndex = 1
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(292, 271)
		Me.Controls.Add(Me.TreeView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ContextMenuStrip1.ResumeLayout(False)
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents ContextMenuStrip1 As System.Windows.Forms.ContextMenuStrip
	Friend WithEvents TestToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
	Friend WithEvents TreeView1 As System.Windows.Forms.TreeView

End Class





form code
Public Class Form1
	

	Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
		MsgBox(TreeView1.SelectedNode.Text)
	End Sub

	Private Sub TreeView1_NodeMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
		TreeView1.SelectedNode = e.Node
		e.Node.ContextMenuStrip = ContextMenuStrip1
	End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

#7 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: TreeView Nodes and Context Menus

Posted 11 February 2009 - 07:21 AM

Alright, that works great - one problem though, I can't see what you did different?

The only difference I see is yours are added at development and mine are added at run-time.. that shouldn't make a difference right? Here is where I am declaring the nodes and adding them to the treeview.

Private Sub addDbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addDbl.Click
		Dim defaultValue As Double

		Dim characters As String = checkField(Me.dblTXT.Text.ToString)

				If characters = ". " Then
					getNode()
					defaultValue = CType(Me.dblTXT.Text.ToString, Double)

					childNode = New TreeNode()
					childNode.Name = Me.fieldName.Text
					childNode.Text = Me.fieldName.Text

					typeNode = New TreeNode()
					typeNode.Name = Me.fieldType.Text
					typeNode.Text = Me.fieldType.Text

					defNode = New TreeNode()
					defNode.Name = defaultValue
					defNode.Text = defaultValue

					TreeView1.SelectedNode.Nodes.Add(childNode)
					TreeView1.SelectedNode = childNode

					childNode.ContextMenuStrip = Me.ContextMenuStrip1

					TreeView1.SelectedNode.Nodes.Add(typeNode)
					TreeView1.SelectedNode.Nodes.Add(defNode)

					clearForm()
				Else
					MsgBox("Your field contains illegal characters: " & vbCr & characters)
				End If

		Catch ex As Exception
			'MsgBox("Exception: " & ex.ToString)
			Me.headerSelect.SelectedItem = "----Select One----"
		End Try
	End Sub



checkField() makes sure there are no illegal characters for database insertion, getNode() gets the correct parent node to put the child under, and clearForm() just clears the form the user filled out to input the new node.

Edit to say:

This is just one function that adds nodes to the treeview, in this case the node default value should be formatted as a double, the others are all identical except they add different datatypes to the treeview. This is basically an outline for creating a database.

This post has been edited by MrWobbles: 11 February 2009 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#8 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: TreeView Nodes and Context Menus

Posted 11 February 2009 - 07:29 AM

no it shouldn't matter, but i believe your problem was in then NodeMouseClick event

you had:
Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
		Dim item As String
		Dim selected As String = ""
		Dim i As Integer = 0

		selected = TreeView1.GetNodeAt(New Point(e.X, e.Y)).Text

		'More unrelated code here


	End Sub




where i had, but you shouldn't need to add the context at this point it's a matter of telling the treeview control that you want that node that you right-clicked on to be selected. so i'm commenting out the e.Node.ContextMenuStrip piece
	Private Sub TreeView1_NodeMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
		TreeView1.SelectedNode = e.Node
		'e.Node.ContextMenuStrip = ContextMenuStrip1
	End Sub


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1