Link to home
Start Free TrialLog in
Avatar of jsctechy
jsctechyFlag for United States of America

asked on

how to change the content of a listbox dinamically?

hi,
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

thanks,
jsctechy
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jsctechy

ASKER

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")
            Me.cmbStoreLocation.Focus()
            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.RemoveAt(index)
            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
                System.IO.Directory.CreateDirectory(DestinationPath)
            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

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.
yes, i wasnt sure why i was not getting the right click
Thanks for your advise
Not a problem, glad to help.  ;=)