Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extend a TreeNode to contain a reference to an object 2

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Can you Extend a TreeNode to contain a reference to an object/class...?

such as:
Code:
Public Class MQLTreeNode
    Inherits TreeNode
    Private pMX As mqlObj
    Public Property MX() As mqlObj
        Get
            Return pMX
        End Get
        Set(ByVal value As mqlObj)
            pMX = value
        End Set
    End Property
End Class

then implement it in the TreeView (or a modified treeview)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
You can do that. You are declaring it ByVal, however, which is true in that you can't declare the property ByRef. But you can use your inherited class in the Nodes() collection of your TreeView, and iterate through it. But, you will have to cast to your custom type to use the property. It's similar to the .Controls() collection of a form. You can iterate through the whole thing, but if you find say a TextBox and want to use a Text-Box specific property, you have to cast to TextBox.
 
example...??? ;-)

----------------

I have a structure set up using a class with a collection of the class...

using this, I can build a treelike structure...

I would like to be able to tie each node back to the object that it is displaying...

This way, if you right click on it, you can lock it, unlock it, check files into it, ect...

the Class Object has methods in it such as lock, unlock...

So I would like to be able to say something like...

TreeView1.Nodes.Item(0).MX.Unlock()

Right now, I can say:
Dim X As MQLTreeNode

Then X.MX shows up in the autosuggest list along with all of the other properties...

But do I need to clone the TreeView and overload the Nodes collection with one made for the new ....

Or are you saying that you can just plug the new node into the existing TreeView?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
I would just leave the TreeView as is, and either do something like this

Code:
Dim X As MQLTreeNode
X = CType(TreeView1.Nodes.Item(0), MQLTreeNode)
X.UnLock()

-OR- create a function and do something like this:
Code:
Function GetMQLNode(ByVal Value As TreeNode) AS MQLTreeNode
     Return CType(Value, MQLTreeNode)
End Function

GetMQLNode(TreeView1.Nodes.Item(0)).Unlock()
 
BUT...

if you use CType to cast from a regular Node to MQL Node...
there would be no information to use for the unlock...
So this would probably return an error:
Code:
Dim X As MQLTreeNode
X = CType(TreeView1.Nodes.Item(0), MQLTreeNode)
X.UnLock()

because the the properties (Type, Name, Revision, ID, etc...) were never set prior to the conversion, because they would not exist... unless the node retained the information when cast to a Node to be placed in the TreeView...

So, do I need to extend TreeView and override the Nodes() as TreeNode with Nodes() as MQLTreeNode...

The mql structure will exist before the TreeView is created, I want to then traverse through the structure and display a tree to represent the structure, then be able to right click on any of the Nodes, and modify the object that they represent...

Previously, in VB6, I used the TAG to store info about the object...

But I am trying to see if I can save some time and code in the long run by taking advantage of inheritence...

So... the target code would look more like this...
Code:
Dim MyMQL as MQLIO = New MQLIO
Dim MyMQLObj as MQLObj = MyMQL.Find("Part", "123", "A")
MyMQLObj.Expand()  'This creates an collection under the object that represents the BOM for the part
TreeView1.Nodes.Clear()
Dim MyObj as [b]MQLTreeNode[/b] = TreeView1.Nodes.Add(MyMQLObj.Name)
[b]MyObj.MX = MyMQLObj[/b]
Dim MyObj2 as [b]MQLTreeNode[/b]
For Each Child As MQLObj In MyMQLObj.Children
  MyObj2 = MyObj.Nodes.Add(Child.Name)
  [b]MyObj2.MX = Child[/b]
Next

Is something like this possible, and if so, what all do I need to extend and/or override, Or is it as simple as using CType to Cast the Type...?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
because the the properties (Type, Name, Revision, ID, etc...) were never set prior to the conversion, because they would not exist... unless the node retained the information when cast to a Node to be placed in the TreeView

I don't know what you mean there.

I know what you mean about the VB6 tag, as I used to do similar things.

This is the solution that I have used before. Its hard to understand your code, because you have many objects with similar names, and I'm not sure which type they all are.

 
Sorry...

mqlIO is my input/output interface class to MQL (Matrix Query Language)
(this object is created in the form constructor and removed in the destructor)

mqlObj is my class that works with mqlIO to interface with individual objects...
this object includes a property (Children) that can contain multiple mqlObj objects to build a tree structure...
This object also has properties such as:
Type - object type in matrix
Name - object name in matrix
Rev - object revision in matrix
ID - unique object id in matrix


I do appriciate the help so far, this just seems kind of hard to explain...

we have previously used the Tag property of Node in VB6 to place a string of text seperated by "|" characters... but any time you wanted to mess with the object, you had to split the Tag string, find the ID#, then make calls to mql with that id #...

Say like, a certain object has the following attributes:
Code:
[b]Type - "Part"
Name - "12345"
Rev  - "A"
ID   - "123.456.789.123"[/b]

we would take that info, join it into a string, and place it in the Tag property of each node for each object:
Code:
Node.Tag = "Part|12345|A|123.456.789.123"

then extract the data like:
Code:
Temp = Split(Node.Tag,"|")
Type - Temp(0)
Name - Temp(1)
Rev - Temp(2)
ID - Temp(3)

so instead of going through all that mess, I would rather get the name like this...
Node.MX.Name
where MX is the mqlObj

Please tell me if you need more info...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
heh, I guess I'm not 100% clear on the casting thing yet...
or inherit... or extend... or... ummm... .NET ;-)

That looks exactly like what I was looking for though.

Thanks A LOT for your help (and patience)
Have A Star!!!

Thanks for the articals too

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
It's easy to get a hold of this stuff. Take the Controls example again.

1. TextBox inherits from Control
2. To create and add a new TextBox to your form, you do something like:
Code:
Dim Txt As New TextBox()
Me.[b]Controls[/b].Add(Txt)
3. To find all Textboxes in your form's controls collection, you do something like
Code:
Dim T As TextBox
For Each C As Control In Me.Controls
     If TypeOf(C) Is TextBox Then
          T = Ctype(C, TextBox)
          'Now you have a TextBox to do things with
     End 
Next

So you are going to be doing the same thing. You are going to create a class that inherits from TreeNode, but you are going to add instances of your class to the Nodes() collection(s). You are NOT going to be adding them to a special CustomTreeNodes() collection. So when you want to look at your Nodes() collection as your custom type, you will cast them to your custom type. In the example, your Custom Node is the TextBox Class, and the regular TreeNode is the Control. The Nodes() Collection is the Controls() Collection.
 
Yeah, I think I got it now...

I was thinking of casting like using int() or str() back in the day to where it made a straight conversion over to another type, and lost any information that was unique to the previous type...

I guess I get a big Welcome To the REAL WORLD of Objects ;-)

After reading through the second artical you posted above, I got exactly what I wanted...

This was all of the code I used that relates to this...
Code:
Public Class Form1
    Public MyMQL As mqlio

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyMQL = New mqlio
    End Sub

    Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
        MyMQL = Nothing
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Temp As mqlObj = MyMQL.Find("Part", "101386984").Item(0)

        [b]Dim Parent As MQLTreeNode = New MQLTreeNode
        TreeView1.Nodes.Add(Parent)[/b]
        Parent.MX = Temp
        Parent.Text = Temp.Name

        Temp.Expand()
        Dim MyChild As MQLTreeNode
        For Each obj As mqlObj In Temp.Children
            [b]MyChild = New MQLTreeNode
            Parent.Nodes.Add(MyChild)[/b]
            MyChild.MX = obj
            MyChild.Text = obj.Name
        Next
    End Sub

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
        [b]MessageBox.Show(CType(e.Node, MQLTreeNode).MX.FindNum)[/b]
    End Sub
End Class

Public Class MQLTreeNode
    Inherits TreeNode
    Private pMX As mqlObj
    Public Property MX() As mqlObj
        Get
            Return pMX
        End Get
        Set(ByVal value As mqlObj)
            pMX = value
        End Set
    End Property
End Class

And it works perfectly :)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top