drag and drop to checkedlistbox - vb.net

I have a checkedlistbox and I want to drag and drop only image extensions and not text files.
so how do I get it done.
I am able to drag and drop all file formats but I need only image files.
Here is my code:
Private Sub CheckedListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CheckedListBox1.DragDrop
Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each FileName As String In Files
CheckedListBox1.Items.Add(FileName, CheckState.Checked)
Thumbcontrol1.AddThumbnail(FileName)
Next
End Sub
Private Sub CheckedListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CheckedListBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub

Just check each file name's extension.
Private Shared ReadOnly SupportedExtensions As String() = {".jpg", ".jpeg", ".gif"}
Private Sub CheckedListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CheckedListBox1.DragDrop
Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each FileName As String In Files
Dim Extension As String = Path.GetExtension(FileName).ToLower
If Array.IndexOf(SupportedExtensions, Extension) <> -1 Then
CheckedListBox1.Items.Add(FileName, CheckState.Checked)
Thumbcontrol1.AddThumbnail(FileName)
End If
Next
End Sub
You may want to add similar code to the DragEnter method to show DragDropEffects.None if there are no picture files in the dragged file list.

Something like this (you'll need to add in more file extensions):
Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each FileName As String In Files
If FileName.Contains(".jpg") Or FileName.Contains(".bmp") Then
CheckedListBox1.Items.Add(FileName, CheckState.Checked)
Thumbcontrol1.AddThumbnail(FileName)
End If
Next
You will also need to account for the case of the filenames.

Related

writing content of listbox to a txt file and then opening it

I am trying to save the contents of one listbox into a txt file and then open that up and printing it to a second listbox. i have done most of this, but my actual file is not a txt file. Can u show me how i make the file to be a txt file using my code? if this is not possible can u show me a code that can?
Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
If OpenFD.ShowDialog() = DialogResult.OK Then
Dim lines = File.ReadAllLines(OpenFD.FileName)
ListBox2.Items.Clear()
ListBox2.Items.AddRange(lines)
End If
End Sub
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If SaveFD.ShowDialog() = DialogResult.OK Then
Using Writer = New StreamWriter(SaveFD.FileName)
For Each o As Object In ListBox1.Items
Writer.WriteLine(o)
Next
End Using
End If
End Sub
You can just use the File.WriteAllText method (along with a string builder). My opinion, it's cleaner code.
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If SaveFD.ShowDialog() = DialogResult.OK Then
Dim sb As New System.Text.StringBuilder()
For Each o As Object In ListBox1.Items
sb.AppendLine(o)
Next
System.IO.File.WriteAllText("c:\mypath\output.txt", sb.ToString())
End If
End Sub
System.IO.File.WriteAllText(Application.StartupPath & "\output.txt", sb.ToString())

how to drag a file and get path of it

How to implement a Panel in Winform , when a user drags a file on it (a simple .txt file) , it should accept it and stores its path into some variable called filepathname etc. which can be used earlier. I could find examples on how to implement drag and drop but not on how to get the path and store it for use later in the program.
Using : Visual Studio 2008 - Vb.net
Thanks!
I found this in this MSDN page
The code below is the modificated ones
Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim MyFiles() As String
Dim i As Integer
' Assign the files to an array.
MyFiles = e.Data.GetData(DataFormats.FileDrop)
'If there are more than one file, set first only
'If you want another restrictment, please edit this.
filepathname = MyFiles(0)
End If
End Sub

Converting drag and drop application that shows directory into showing hash

I am having trouble converting this code into allowing me to drop files into my application and my application create a message box saying its md5 hash code. Currently, I have the code to give the directory of the file I dropped in.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
Private Sub Form1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
MsgBox(path)
Next
End Sub
Private Sub Form1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
End Class
Seems like you have most of the code you need, only needs a function to calculate the md5, and then you can plug that into your message box:
MsgBox(ComputeMD5Hash(path))
Here is the function, taken mostly from this Microsoft Support Article
Private Function ComputeMD5Hash(ByVal path As String) As String
Dim tmpSource() As Byte
Dim tmpHash() As Byte
'Create a byte array from source data.
tmpSource = My.Computer.FileSystem.ReadAllBytes(path)
'Compute hash based on source data.
tmpHash = New Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(tmpSource)
Dim i As Integer
Dim sOutput As New System.Text.StringBuilder(tmpHash.Length)
For i = 0 To tmpHash.Length - 1
sOutput.Append(tmpHash(i).ToString("X2"))
Next
Return sOutput.ToString()
End Function
In-case you want to drop a directory and get all the hashes of the files inside you first need to check if you're dealing with a file or a folder, if its a folder you loop over all the files inside and call the function for each one, which would make your DragDrop event handler look something like this, and have the added benefit both files and folders can be taken care of.
Private Sub Form1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
If IO.File.GetAttributes(path) = FileAttribute.Directory Then
'Dealing with a Directory
Dim di As New IO.DirectoryInfo(path)
Dim fiArr As IO.FileInfo() = di.GetFiles()
Dim fri As IO.FileInfo
For Each fri In fiArr
MessageBox.Show(fri.FullName & " " & ComputeMD5Hash(fri.FullName))
Next fri
Else
'Dealing with a File
MessageBox.Show(path & " " & ComputeMD5Hash(path))
End If
Next
End Sub

Copy folders and exclude folder

I have a code that copy a folder to another location through textboxes. Textbox1 where the user can specify which folder to copy and textbox2 that the user can browse for a destination folder.
If Textbox1 is the path to "My documents" an error occur saying:
Access to path C:\Users\%USERNAME%\Documents\My Music is denied.
"My Music" is a hidden folder in "My document" that is checked as "hide protected operating system files" by windows 7. I am using Visual Studio 2005 and new in VB.net, can anybody take a look at this code and tell me a way to exclude folders to copy?
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim fs As Object = CreateObject("Scripting.FileSystemObject")
Dim sFolderpath As String = TextBox1.Text
Dim sourceFolderName As String = System.IO.Path.GetFileName(sFolderpath)
Dim strDate As String = DateTime.Now.ToString("yyyy-MM-dd")
Dim dFolderpath As String = System.IO.Path.Combine(TextBox6.Text, strDate)
fs.createfolder(dFolderpath)
dFolderpath = System.IO.Path.Combine(dFolderpath, sourceFolderName)
fs.createfolder(dFolderpath)
fs.copyfolder(sFolderpath, dFolderpath)
End Sub
I'd suggest splitting the problem into 4 subs. Firstly two subs to allow users to select a folder they desire for source and destination. The a button click event that starts the copying and finally a sub that actually handles the copying.
Try this on for size:
Dim CopyFromPath As String
Dim CopyToPath As String
Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.MouseClick
Dim fldbroser1 As New FolderBrowserDialog
fldbroser1.RootFolder = Environment.SpecialFolder.MyMusic
fldbroser1.ShowDialog()
CopyFromPath = fldbroser1.SelectedPath
End Sub
Private Sub TextBox2_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.MouseClick
Dim fldbroser1 As New FolderBrowserDialog
fldbroser1.RootFolder = Environment.SpecialFolder.MyComputer
fldbroser1.ShowDialog()
CopyToPath = fldbroser1.SelectedPath
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CopyAllFiles(CopyFromPath, CopyToPath)
End Sub
Private Sub CopyAllFiles(ByVal CopyFromPath As String, ByVal CopyToPath As String)
If Not Directory.Exists(CopyToPath) Then
Directory.CreateDirectory(CopyToPath)
End If
For Each filee As String In Directory.GetFiles(Path.GetDirectoryName(CopyFromPath))
Dim dest As String = Path.Combine(CopyToPath, Path.GetFileName(filee))
File.Copy(filee, dest)
Next
For Each folder As String In Directory.GetDirectories(Path.GetDirectoryName(CopyFromPath))
Dim dest As String = Path.Combine(CopyToPath, Path.GetFileName(folder))
CopyAllFiles(folder, dest)
Next
End Sub
You will need to import System.IO for this to work. Fell free to ask any questions if you have trouble with the code. Good luck learning VB, it can be annoying at times but it's pretty useful.

How do you read a file and display in listbox in VB.NET?

I was trying to display data from file in a list saved on the hard drive by clicking on a button, however I'm not sure on haw to do it properly:
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Dim TextLine As String
If System.IO.File.Exists(Filename) = True Then
Dim RecipeReader As New System.IO.StreamReader(Filename)
Do While RecipeReader.Peek() <> -1
TextLine = TextLine & RecipeReader.ReadLine() & vbNewLine
Loop
lstRecipes.Text = TextLine.Text
Else
MsgBox("File Does Not Exist")
End If
End Sub
I would be really grateful for assistance :D
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Try
lstRecipes.AddRange(File.ReadAllLines(FileName))
Catch
MsgBox("Unable to read file")
End Try
End Sub
Use:
lstRecipes.Items.Add(TextLine.Text)
More specifically before it jumps to the next item in the list, so this would go right after your assignment of TextLine.
you can do this:
Imports System
Imports System.IO
Public Class Form1
Public Shared listTXT, listOfTxt, listOfTxtFile As New List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "C:\myDirectory\"
Dim parentinfo As New DirectoryInfo(path)
' Store Text file name in a list
For Each txtFile As FileSystemInfo In parentinfo.GetFileSystemInfos()
listTXT.Add(txtFile.Name)
Next
' Store Path of Text file in a list
For noOfTxtFile = 0 To listTXT.Count - 1
Dim pathOfFile As String = path & listTXT(noOfTxtFile)
listOfTxtFile.Add(pathOfFile)
Dim obj As System.IO.StreamReader
obj = System.IO.File.OpenText(pathOfFile)
While Not obj.EndOfStream
listOfTxt.Add(obj.ReadLine)
End While
Next
Dim lineOfTxt As Integer
Dim txt As String
For lineOfTxt = 0 To listOfTxt.Count - 1
txt = listOfTxt(lineOfTxt)
ListBox1.Items.Add(txt)
Next
End Sub
End Class