We help IT Professionals succeed at work.

how to change the content of a listbox dinamically?

jsctechy asked
Last Modified: 2013-11-26
Is there a way that i can change the contents of a listbox? i have a listbox that not always contains the same amount of items and i need to select an item an right click and have the name be enable to change. how can i do this?

i have the event for the right click like this:

    Private Sub ListBox2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then

        End If

Watch Question

Distinguished Expert 2017
This one is on us!
(Get your first solution completely free - no credit card required)
jsctechyInfrastructure Team Lead


hi fernando, that wasnt quite what i'm looking for but i got something like this now:

Private Sub ListBox2_MouseDown(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.menuFilters.Show(sender, e.X, e.Y)
        End If
    End Sub
Private Sub Rename_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rename.Click
        If cmbStoreLocation.Text = "" Then
            MsgBox("You haven selected the path", MsgBoxStyle.Information, "Tittle")
            Exit Sub
        End If
        Dim NewFileName As String
        NewFileName = InputBox("enter New Name", "Tittle", ListBox2.SelectedItem.ToString)
        If NewFileName <> "" Then
            Dim index As Integer = ListBox2.SelectedIndex
            ListBox2.Items.Insert(index, NewFileName)
            Dim OldFileName As String = ListBox1.Items.Item(index).ToString
            Dim source As String = Me.txtPath.Text & OldFileName
            'copy file
            Dim DestinationPath As String = System.AppDomain.CurrentDomain.BaseDirectory().ToString & Me.cmbStoreLocation.Text
            'check for the directory
            If Not System.IO.Directory.Exists(DestinationPath) Then
            End If
            If Not IO.File.Exists(DestinationPath & "\" & OldFileName) Then
                'copy file
                IO.File.Copy(source, DestinationPath & "\" & NewFileName, True)
            End If
        End If
    End Sub

Open in new window

Fernando SotoRetired
Distinguished Expert 2017

So is this providing what you need? I also see that you switched from trying to use the  MouseClick to using MouseDown event as I showed in my sample code, good choise.
jsctechyInfrastructure Team Lead


yes, i wasnt sure why i was not getting the right click
Thanks for your advise
Fernando SotoRetired
Distinguished Expert 2017

Not a problem, glad to help.  ;=)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.