Yes, it is possible. Just change the value of the List
property in the index of the selected object. Here is an example code with two UserForms (one called "Test", which displays the ListBox, and another called "Edit" that displays the contents of the currently selected item in the list and allows you to edit it):
UserForm "Test"
Screenshot:
(TheListBoxiscalled"ListBox", the "Test" UserForm is created only once and reused)
Code:
Dim oEditForm As Editar
Private Sub ListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim sName As String
sName = Me.ListBox.List(Me.ListBox.ListIndex)
oEditForm.EditName.Text = sName
oEditForm.Show
If oEditForm.Tag = True Then
sName = oEditForm.EditName.Text
Me.ListBox.List(Me.ListBox.ListIndex) = sName
End If
End Sub
Private Sub UserForm_Initialize()
Set oEditForm = New Editar
Me.ListBox.AddItem "Tyrion Lannister"
Me.ListBox.AddItem "Jaime Lannister"
Me.ListBox.AddItem "Cersei Lannister"
Me.ListBox.AddItem "Daenerys Targaryen"
Me.ListBox.AddItem "Jon Snow"
Me.ListBox.AddItem "Petyr Baelish"
Me.ListBox.AddItem "Jorah Mormont"
End Sub
UserForm "Edit"
Screenshot:
(thebuttonsarecalled"Ok" and "Cancel", and the TextBox is called "EditName")
Code:
Private Sub Cancel_Click()
Me.Tag = False
Hide
End Sub
Private Sub Ok_Click()
Me.Tag = True
Hide
End Sub
Result
Screenshot:
Explanation of the code:
First you need to capture the appropriate double click event, called *_DblClick
.
Then get the text of the currently selected item in the list from ListBox.List
by passing its index that is in ListBox.ListIndex
.
Put this value in your other edit form and allow the user to edit it. When it clicks Ok, go to the next step.
Having the user edit the value, update the content of the same currently selected item in the list (that is, using ListBox.List
passing its index that is in ListBox.ListIndex
). >