Results 1 to 8 of 8

Thread: treenode tag returning strange value.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    316

    treenode tag returning strange value.

    Hi all,

    I´ve finally managed to assign an array to a treenode tag, displaying the tag(0) as tooltip.

    However, on the nodedoubleclick event, I want to get the tag(0) value to navigate a bindingsource, and the tag always returns 8, no matter which node I click. 8 is the number of treenodes if that has anything to do with it.

    this is the code:
    Code:
        Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
            Dim pos As Integer
            pos = Me.TreeView1.SelectedNode.Tag(0)
            Evaluate.BindingSource3.Position = pos
            Evaluate.updat()
        End Sub
    Thanks!

    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: treenode tag returning strange value.

    The Tag property is type Object and you can't index an Object, so you obviously have Option Strict turned Off for a start. I suggest you remedy that first up. If you have an Integer array stored in the Tag then you should first cast it as that type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    316

    Re: treenode tag returning strange value.

    -edited-
    Last edited by Fuga; Dec 3rd, 2007 at 02:40 PM.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    316

    Re: treenode tag returning strange value.

    Thanks for your help.

    I turned on option strict, and that gave me an error that it disallowes "late binding".

    I did a search on the forum and I found some posts, but I just don´t understand it. What does late binding mean?

    I think I need to give the whole code.

    In one module, the treeview is populated:
    Code:
        Sub createtreeview()
            Dim tvds As New DataSet
            Dim tvda As New OleDb.OleDbDataAdapter
            Dim tvrw As DataRow
            Dim kval As Integer
            Dim tvselcom As New OleDb.OleDbCommand
            Dim tv As New TreeView
            Dim tvnode As New System.Windows.Forms.TreeNode
            Dim tagcontent(1) As Integer
    
            tors.TreeView1.Nodes.Clear()
            tors.TreeView1.Nodes.Add("rootnode1")
            tors.TreeView1.Nodes.Add("rootnode2")
            tors.TreeView1.Nodes.Add("rootnode3")
            tors.TreeView1.Nodes.Add("rootnode4")
            tvselcom.CommandText = "....."
            tvselcom.Connection = torcon
            tvda.SelectCommand = tvselcom
            tvda.Fill(tvds, "tor")
            tv = tors.TreeView1
            kval = 0
            For Each tvrw In tvds.Tables(0).Rows
                tvnode = New TreeNode
                tagcontent.SetValue(kval, 0)
                tagcontent.SetValue(Evaluate.BindingSource3.Position, 1)
                tvnode.Tag = tagcontent
                tvnode.Text = tvrw.Item("....").ToString
                tvnode.ToolTipText = tvnode.Tag(0).ToString
                tors.TreeView1.Nodes(0).Nodes.Add(tvnode)
                If tvnode.Tag(0).ToString = Evaluate.TextBox7.Text Then
                    tors.TreeView1.SelectedNode = tvnode
                End If
                kval = kval + 1
            Next
            tvds.Clear()
            tvda.SelectCommand.CommandText = "....."
            tvda.Fill(tvds, "tor")
            For Each tvrw In tvds.Tables(0).Rows
                tvnode = New TreeNode
                tagcontent.SetValue(kval, 0)
                tagcontent.SetValue(Evaluate.BindingSource3.Position, 1)
                tvnode.Tag = tagcontent
                tvnode.Text = tvrw.Item("rubrik").ToString
                tvnode.ToolTipText = tvnode.Tag(0).ToString
                tors.TreeView1.Nodes(1).Nodes.Add(tvnode)
                If tvnode.Tag(0).ToString = Evaluate.TextBox7.Text Then
                    tors.TreeView1.SelectedNode = tvnode
                End If
                kval = kval + 1
            Next
            tvds.Clear()
            tvda.SelectCommand.CommandText = "...."
            tvda.Fill(tvds, "tor")
            For Each tvrw In tvds.Tables(0).Rows
                tvnode = New TreeNode
                tagcontent.SetValue(kval, 0)
                tagcontent.SetValue(Evaluate.BindingSource3.Position, 1)
                tvnode.Tag = tagcontent
                tvnode.Text = tvrw.Item("rubrik").ToString
                tvnode.ToolTipText = tvnode.Tag(0).ToString
                Faktors.TreeView1.Nodes(2).Nodes.Add(tvnode)
                If tvnode.Tag(0).ToString = Evaluate.TextBox7.Text Then
                    tors.TreeView1.SelectedNode = tvnode
                End If
                kval = kval + 1
            Next
            tvds.Clear()
            tvda.SelectCommand.CommandText = "...."
            tvda.Fill(tvds, "tor")
            For Each tvrw In tvds.Tables(0).Rows
                tvnode = New TreeNode
                tagcontent.SetValue(kval, 0)
                tagcontent.SetValue(Evaluate.BindingSource3.Position, 1)
                tvnode.Tag = tagcontent
                tvnode.Text = tvrw.Item("rubrik").ToString
                tvnode.ToolTipText = tvnode.Tag(0).ToString
                tors.TreeView1.Nodes(3).Nodes.Add(tvnode)
                If tvnode.Tag(0).ToString = Evaluate.TextBox7.Text Then
                   tors.TreeView1.SelectedNode = tvnode
                End If
                kval = kval + 1
            Next
            tvds.Clear()
            tors.TreeView1.ExpandAll()
            tors.Refresh()
        End Sub
    This makes four rootnodes that are always the same, and then childnodes varying over the rootnodes. This works.

    Just to check it, I set the tooltiptext to display the tag(0), which is the thing I want to use for navigation on the bindingsource:

    Code:
     Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    
            Evaluate.BindingSource3.Position = Me.TreeView1.SelectedNode.Tag(0)
            Evaluate.updat()
        End Sub
    Now if the tooltiptext displays 4, how come the me.treeview1.selectednode.tag(0) gives 8?


    Greatful for all help!
    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: treenode tag returning strange value.

    Option Strict will mean that you will explicitly have to tell the compiler what types you are using. In your first post, JM noted that you were treating an object as if it were an array. You felt this was safe because you KNEW it was an array. However, the compiler doesn't know that. With Option Strict OFF, you are telling the compiler to go ahead and assume that you know what you are talking about (and if you don't, you pay for it). That may seem fine to you, but you pay for that freedom, and the price is not worth it. Setting Option Strict ON means that you will have to explicitly tell the compiler that, "Yes! This IS an array" before it will be willing to use an object as an array. The result of this is that you will write better code, but you will also write faster code. More typing for you, but less effort at runtime, hence faster performance. Insignificant in this case, but still something that needs to be done.

    As for the question, I don't have a precise answer, as Treeviews can behave in some unexpected fashions in my experience. However, I would suggest that at the position where the oddity occurs, put a breakpoint on the line, and have a look. I think the line might be this one:

    Evaluate.BindingSource3.Position = Me.TreeView1.SelectedNode.Tag(0)

    if I read the question right. I would want to look at two things:

    1) What is in SelectedNode.Tag?
    2) What is SelectedNode?

    My first thought, without seeing the code in action, is that SelectedNode might not be what you think it is. Failing that, then the Tag doesn't hold what you expect it to hold.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    316

    Re: treenode tag returning strange value.

    Thanks

    Yes you´re right about the failing line.

    1. the tag holds two values: 8 and 0.
    2. VB says the me.treenode1.selectednode.tag(0) is a system.array

    It´s all very strange. When I hover the mouse, the value displayed is the right index of the treenode, but when I doubleclick, suddenly it´s index 8.

    Actually it is the number of treenodes. When I added a post the index is 9.

    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: treenode tag returning strange value.

    The next step I would take would be to set a breakpoint back where you load the Treeview so that you can watch each and every one getting filled. If that shows the right thing being filled, then put the breakpoint on the line in question and step through from there. If the value is right at the time the breakpoint is hit, but it still displays wrong then the options are these:

    1) Some code is changing the value, stepping through the code should show that. The most likely possibility, from my semi-informed perspective, would be that the one event is triggering another event that you are not expecting.
    2) You are not displaying what you think you are (the tooltip, or whatever is set wrong. Not likely, but it IS one of the options).
    3) The node you think you are working with is not the node you are actually working with. I think you may have covered this one.
    My usual boring signature: Nothing

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: treenode tag returning strange value.

    I wouldn't say "Option Strict will mean that you will explicitly have to tell the compiler what types you are using."
    To make the point more clear I'd say turning on Option Strict will cause VB to disallow you from making implicit narrowing conversions that might subsequently fail; or worse, yield invalid and yet unknown erroneous results. Then, with Option Strict on, you'll have to cast explicitly as previously noted in post #5.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width