Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SelectedItem "Key" is no more in .NET

Posted on 2003-11-26
3
460 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:David_Stone
  • 2
3 Comments
 
LVL 9

Expert Comment

by:malharone
ID: 9825719
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
0
 

Author Comment

by:David_Stone
ID: 9826918
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....
0
 
LVL 9

Accepted Solution

by:
malharone earned 150 total points
ID: 9826966
actually ... most controls in .net have the .tag property.
So
when adding a new tab page.. you can do the following...
i'd also recommend adding a toString method to your iPerson object so you don't manually have to concatenate the strings
e.g.
in your Person class
public overrides function toString as String
  return myFName & " " & myLName
end function


        ' adding tab pages
        For Each iPerson In myPeople
            Dim newPage As TabPage = New TabPage(iPerson)
            newPage.Tag = iperson
            Me.TabControl1.TabPages.Add(newPage)
        Next

retriving the object/key
dim selectedPerson as Person = nothing
if not myPeople.selectedTab is nothing then
   selectedPerson=myPeople.selectedTab.tag
end if

if not selectedPerson is nothing then
  msgbox selectedPerson.toString()
  msgbox selectedPerson.id
  ... and so on ...
end if
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question