Link to home
Start Free TrialLog in
Avatar of David_Stone
David_Stone

asked on

SelectedItem "Key" is no more in .NET

Hi, when using a TreeView, ListView, ListBox, TabControl in VB6 there used to be a Key property associated with each sub-item such a a node, listitem, tab etc. This seems to have been lost in .NET. For example, if I have a TabControl, the collection of TabPages don't appear to have a unique property such as a key whereby they are accesible. I've done a little research on this and I've read of a few utilising the Tag Property of such items but this doesn't feel right. Surely they haven't overlooked this feature? Also, when adding a "new" item, again a new TabPage to a TabControl for example, the old syntax used to allow you to define all arguments in a single line i.e

MyTabCtrl.Tabs.Add "Text", "Key", "Image" or similar to that effect. This also seems to have gone. Items can't be added to a parent without writing the routine yourself.

Am I overlooking something here?

ThankU
Avatar of malharone
malharone

the vb .net has taken the object oriented approach to the vb6 "problems".

hope this helps


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TabControl1
        '
        Me.TabControl1.Location = New System.Drawing.Point(16, 120)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(248, 72)
        Me.TabControl1.TabIndex = 0
        '
        'TreeView1
        '
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(136, 24)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(136, 96)
        Me.TreeView1.TabIndex = 1
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(8, 24)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(120, 95)
        Me.ListBox1.TabIndex = 2
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(24, 208)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Add Items"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(128, 208)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(112, 24)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Get Selected Items"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.TreeView1)
        Me.Controls.Add(Me.TabControl1)
        Me.Controls.Add(Me.Button2)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim totalAdded As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        totalAdded += 1
        Dim st As String
        st = "Item # " & totalAdded
        Me.TabControl1.TabPages.Add(New TabPage(st))
        Me.TreeView1.Nodes.Add(New TreeNode(st))
        Me.ListBox1.Items.Add(st)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim st As String
        Dim idx As Short
        If Not Me.TabControl1.SelectedTab Is Nothing Then
            MsgBox("Selected tab: " & Me.TabControl1.SelectedIndex & " -- tab name -- " & Me.TabControl1.SelectedTab.Text)
        Else
            MsgBox("No tab selected")
        End If

        If Not Me.TreeView1.SelectedNode Is Nothing Then
            MsgBox("Selected tree node: " & Me.TreeView1.SelectedNode.Text)
        Else
            MsgBox("No tree node selected")
        End If

        If Not Me.ListBox1.SelectedItem Is Nothing Then
            MsgBox("List box item: " & Me.ListBox1.SelectedItem)
        Else
            MsgBox("No list box item selected")
        End If
    End Sub
End Class
Avatar of David_Stone

ASKER

Thanks Malharone, I see what you mean but this doesn't really address my problem. I'll try to explain via an example:

Imagine  a created a Strongly Typed Collection Class called "People", to which "Person"'s can be added, removed accessed etc. The Person class has three properties, "First Name", "Second Name" and "ID".

Dim MyPeople As New Persons 'Declare new People Collection...

MyPeople.Add(New Person("Micheal", "Jackson", "000001")) 'Add some People with ID (Possibly SSN or something)...
MyPeople.Add(New Person("Jane", "Austen", "000002"))

Imagine a tabbed dialog which represents the People in the collection.

Dim iPerson As Person, PeopleTabCtrl As New TabControl

For Each iPerson In MyPeople
  PeopleTabCtrl.TabPages.Add(iPerson.FirstName & " " & iPerson.SecondName)
Next

So adding Two Tabs with the captions "Micheal Jackson" and "Jane Austen". Ideally, I would like to add as the TabPage "key" the values of the person ID - 000001 and 000002 so they can be accessed directly like this on the Tab click event:

Private Sub PeopleTabCtrl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

   Msgbox ("You Selected " & MyPeople.Item(PeopleTabCtrl.SelectedTab."Key").FirstName) '"Key" isn't a valid property...

End Sub

This isn't possible without a Key property???

Thanks....
ASKER CERTIFIED SOLUTION
Avatar of malharone
malharone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial