Results 1 to 8 of 8

Thread: Rename A Listbox's Selected Item

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Rename A Listbox's Selected Item

    Hi, how do I set the name of a listbox's selected item to a name of choice?
    Any help is greatly appreciated,
    Louix

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Rename A Listbox's Selected Item

    remove it
    vb Code:
    1. listbox1.items.removeat(index)
    , then insert modified item
    vb Code:
    1. listbox1.items.insert(index)

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Rename A Listbox's Selected Item

    Does your listbox, by any chance, contain the names of physical disk files that you need to rename?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Rename A Listbox's Selected Item

    Hack: It does actually

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Rename A Listbox's Selected Item

    Then you would want to use My.Computer.FileSystem.RenameFile

    The source file would the be selected item from your listbox.

    How will the new file name get fed in?

    Do you have textbox or something like that?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Rename A Listbox's Selected Item

    Yes, through a textbox.
    Last edited by Louix; Jul 25th, 2008 at 01:12 PM.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Rename A Listbox's Selected Item

    Then try something like this. Bear in mind, that you listbox items and your textbox would both have to have the complete path and filename displayed in order for this to work. If that is not the case, then the rename would need to be modified in order to append the path to the file name.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            For i = 0 To ListBox1.Items.Count - 1
                If ListBox1.GetSelected(i) = True Then
                    My.Computer.FileSystem.RenameFile(ListBox1.Text, TextBox1.Text)
                    Exit For
                End If
            Next
        End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Rename A Listbox's Selected Item

    I have code that renames the file (in a special way I need it to), but I need code that renames the listbox's selected item so everything matches up.
    I don't get back to my dev machine until tomorrow, but the code you posted doesn't change the selected items text (?).
    Thank you for taking the time to reply but is there a way to make the text change? If you need it, I'll show you what I mean by my "special way" tomorrow.
    Louix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width