1 Reply Latest reply on Jul 18, 2016 12:00 PM by Michael Spens

    Properties Reading & Editing Listbox

    Jean-Baptiste Bouckaert

      Hi, currently I'm reading the custom properties from  a file and putting them in a ListBox. Propnames are the names of the properties and Propval are the values of these properties. I'm using VB.NET with the Document Manager API.


          Public Sub ReadWriteProps()


                  propNames = dmDoc.GetCustomPropertyNames()


              If Not propNames Is Nothing Then


                  For Each propName As String In propNames

                      propVal = dmDoc.GetCustomProperty(propName, Nothing)

                      clb_Properties.Items.Add(propName & ": " & propVal)


              End If


          End Sub


      I have put a textbox beneath this and would like 2 things.

      I'd like to display the value of the selected item in the listbox. Currently I use this code, but it gives me both the value and the name.


          Private Sub clb_Properties_SelectedIndexChanged(sender As Object, e As EventArgs) Handles clb_Properties.SelectedIndexChanged


              txt_PropertyEdit.Text = propVal(clb_Properties.SelectedItem)


          End Sub


      Secondly, I'd like to click a button and then this would trigger the replacement of the value in the listbox of the selected item with the given value in the textbox.


          Private Sub btn_Edit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Edit.Click


              propVal(clb_Properties.SelectedIndex) = txt_PropertyEdit.Text


          End Sub


      This code however, gives me an error. I've also tried the 'SelectedItem' function. Does someone know how I fix this?



        • Re: Properties Reading & Editing Listbox
          Michael Spens

          Since the values in your ListBox are single strings, you'll need to manually separate the name from the value.  The ListBox control has a few ways to get the selected text.  SelectedItem could return undesired results since ListBox items could be any object.  ListBox.Text will return the actively selected text if you limit your selections to one item. 


          To split your name from value, try Strings.Split(": ", clb_Properties.Text).  This will give you an array of two elements, your name and the value.


          If you'd like a more elegant solution, I might suggest using a DataGridView control.  It gives you the ability to add columns and create something that looks more like the Custom Properties UI in SOLIDWORKS.


          Try using the method from the link below to replace the selected item in your ListBox with the new text.  FYI, SelectedIndex returns the index number of the selected ListBox Item.  It isn't something you can replace.

          Replace Listbox text (vb .net) - vbCity - The .NET Developer Community


          Hope that helps!