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
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
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.F irstName & " " & 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_SelectedInde xChanged(B yVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexC hanged
Msgbox ("You Selected " & MyPeople.Item(PeopleTabCtr l.Selected Tab."Key") .FirstName ) '"Key" isn't a valid property...
End Sub
This isn't possible without a Key property???
Thanks....
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
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_SelectedInde
Msgbox ("You Selected " & MyPeople.Item(PeopleTabCtr
End Sub
This isn't possible without a Key property???
Thanks....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.ICon
'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.TabCo
Friend WithEvents TreeView1 As System.Windows.Forms.TreeV
Friend WithEvents ListBox1 As System.Windows.Forms.ListB
Friend WithEvents Button1 As System.Windows.Forms.Butto
Friend WithEvents Button2 As System.Windows.Forms.Butto
<System.Diagnostics.Debugg
Me.TabControl1 = New System.Windows.Forms.TabCo
Me.TreeView1 = New System.Windows.Forms.TreeV
Me.ListBox1 = New System.Windows.Forms.ListB
Me.Button1 = New System.Windows.Forms.Butto
Me.Button2 = New System.Windows.Forms.Butto
Me.SuspendLayout()
'
'TabControl1
'
Me.TabControl1.Location = New System.Drawing.Point(16, 120)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedInd
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.SelectedImage
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.ListBox
Me.Controls.Add(Me.TreeVie
Me.Controls.Add(Me.TabCont
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.Ad
Me.TreeView1.Nodes.Add(New
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
MsgBox("Selected tab: " & Me.TabControl1.SelectedInd
Else
MsgBox("No tab selected")
End If
If Not Me.TreeView1.SelectedNode Is Nothing Then
MsgBox("Selected tree node: " & Me.TreeView1.SelectedNode.
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