raheel88 0 Newbie Poster

I have a dynamic form, which contains an empty listbox and a button at first. When the button is clicked, an item is added to the listbox and a new PropertyGrid is created associated with that item. What I want to be able to do is change the name of the item in the listbox depending on the user's input in one of the properties of the associated PropertyGrid.

Here's a simplified version of my code to give an idea of what's going on;

Imports System.ComponentModel

Public Class form1
    'declare dictionary of PropertyGrids with a string key
    Public ctrlArray As New Dictionary(Of String, PropertyGrid)

    'declare PropertyGrid
    Public currgrid As PropertyGrid

    'button click sub
    Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click

        'create new listbox item and associated property grid
        Dim ctrlkey As Integer = ctrlArray.Count + 1
        Dim key As String = "Element " & ctrlkey
        Dim newitem As New PropertyGrid

        newitem.Location = New System.Drawing.Point(276, 61)
        newitem.Size = New System.Drawing.Size(272, 188)
        newitem.SelectedObject = New SimpleProperties
        newitem.Visible = False

        'add the new property grid to the control array, then add new listitem

        Me.Controls.Add(newitem)
        ctrlArray.Add(key, newitem)
        listBox1.Items.Add(key)
        listBox1.SelectedIndex = ctrlArray.Count - 1
    End Sub
    
    'listBox index changed sub
    Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged

        'hide the current visible grid
        If Not currgrid Is Nothing Then
            currgrid.Visible = False
        End If

        'make the newly selected grid visible
        If listBox1.SelectedIndex <> -1 Then
            currgrid = ctrlArray(listBox1.SelectedItem)
            currgrid.BringToFront()
            currgrid.Visible = True
        End If
    End Sub

    'Class to outline properties contained in each PropertyGrid
    Public Class SimpleProperties
        Private _ElementName As String

        <CategoryAttribute("Basic properties"), _
           Browsable(True), _
           [ReadOnly](False), _
           BindableAttribute(False), _
           DefaultValueAttribute(""), _
           DisplayName("ElementName"), _
           DesignOnly(False), _
           DescriptionAttribute("Enter description of user element")> _
        Public Property ElementName() As String
            Get
                Return _ElementName
            End Get
            Set(ByVal Value As String)
                _ElementName = Value
            End Set
        End Property
    End Class

End Class

So in this code, a new list item and PropertyGrid is created every time the 'Add Item' button is clicked, and if an item in the listbox is selected, the associated PropertyGrid becomes visible.

What I want to do is have the listBox item's name change when I edit the 'ElementName' property in the associated PropertyGrid. So, for example, "Element 1" is selected in listBox1, if I change the 'ElementName' property in the PropertyGrid to "Numero uno", say, then "Element 1" should change to "Numero Uno" in the listBox. And so on for all the items/property grids.

Hope that's clear for everyone! Thanks in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.