Cut,copy and paste

annir

Active member
Joined
Sep 1, 2008
Messages
32
Programming Experience
Beginner
Hi guys,

Can I ask if you know how to cut,copy and paste in vb.net. I'm using a DirListbox and FileListbox.

For example. whenever I choose a folder from DirListbox or file from the FileListbox and rightclick it, then the contextmenu will appear and choose the copy/cut. and then I will choose again a folder from the DirListbox and then paste it to it.

file - then copy - then paste.

I'm asking if someone knows how to copy/cut/paste from a filelistbox and drilistbox?

thank you!

annir
 
This should get you started. First add a ContextMenuStrip to form and add "Copy" and "Paste" items to it. Use properties window for both Listbox controls to select the ContextMenuStrip. Use code similar to this for the Copy and Paste menu items:
VB.NET:
Enum FileOperation
    Copy
    Cut
End Enum

Dim items As List(Of String)
Dim operation As FileOperation

Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
    If Me.ContextMenuStrip1.SourceControl Is Me.FileListBox1 Then
        items = New List(Of String)
        For Each filename As String In Me.FileListBox1.SelectedItems
            items.Add(IO.Path.Combine(Me.FileListBox1.Path, filename))
        Next
        Me.operation = FileOperation.Copy
    End If
End Sub

Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
    If Me.ContextMenuStrip1.SourceControl Is Me.DirListBox1 Then
        For Each filepath As String In items
            Dim newfilepath As String = IO.Path.Combine(Me.DirListBox1.Path, IO.Path.GetFileName(filepath))
            If Me.operation = FileOperation.Copy Then
                IO.File.Copy(filepath, newfilepath)
            End If
        Next
    End If
End Sub
 
Hi Johnh,

thanks for the reply, it really works!

Can I ask one more thing, how can I refresh the FiliListbox every time I will paste something on it? Because, every time I will paste, the FileListbox is not automatically refreshing it needs to be re-clicked the directory then choose the same folder then that's the time the file that I paste will appear.

Do you know how to refresh the FileListbox?

And is it possible if I want to copy the whole folder from the DirListbox? If I select a folder then copy it and paste from the other folder is it possible?

Thanks again!

annir
 
Last edited:
Do you know how to refresh the FileListbox?
I can't see it has such functionality, actually it is a VB6 compability control that has not been further refined in .Net. You can set Path to a different valid path and then back again to make it load the file names again.
If I select a folder then copy it and paste from the other folder is it possible?
Yes. Use for example IO.Directory.Getfiles method.
 
How to copy a Folder in Dirlistbox?

Hi Guys,

This is the process I have a Dirlistbox then if the user select a folder in the Dirlistbox and then copy (all the files inside the folder) it and the user has an option to paste it whenever they wants.

My question is how can I copy the folder from the Dirlistbox? I have a code but it has an error.

PHP:
 items = New List(Of String)
        Dim TxtNames As String() = IO.Directory.GetFiles(DirListBox1.SelectedItem)
        For Each TxtNames As String In Me.DirListBox1.SelectedItems
            items.Add(IO.Path.Combine(Me.DirListBox1.Path, TxtNames))
        Next

        Me.operation = FileOperation.Copy

this is the error : Could not find a part of the path 'C:\Documents and Settings\cpunor\Desktop\FMMS\FMMS\FMMS\bin\Debug\ACS'.

ACS- the folder that I want to copy.

someone could have an idea on this one?

thanks for the help.

annir
 
Last edited:
I solved the problem but there is something wrong, I'm getting all the files in the folder that copied. But the problem was I get the file only. I'm not getting the the folder.

The only thing that will paste is the files. I want to copy the folder then inside that folder is the files contain to it.

someone have an idea on this?

thanks

annir
 
Back
Top