Hi All... I am using the code below to drag&drop files between listboxes. For some reason I am now getting an exception 'Value Cannot be Null. Parameter Name: item'. When I use: 'If result = DialogResult.Yes' and attempt to SPLIT the job (which is only renaming the file and moving a copy) this is where the error happens and I am not certain why. The file does move, but does not get to the append to file portion. I am stuck here on this as I don't really understand the error.
*** EDIT: Just realized the .Tag property was incorrect in that portion of the code. I still think this needs more efficiency added. Please feel free to add any comments.
Code:Private Sub ListBoxes_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox6.DragDrop, ListBox5.DragDrop, ListBox4.DragDrop, ListBox3.DragDrop, ListBox2.DragDrop, ListBox1.DragDrop, ListBox7.DragDrop Try If e.Data.GetDataPresent(GetType(FileInfo)) Then With DirectCast(sender, ListBox) Dim NewFile As String Dim fileDropped As FileInfo = DirectCast(e.Data.GetData(GetType(FileInfo)), FileInfo) Dim FileToMove As String = fileDropped.FullName Dim MoveLocation As String = strPath & "\" & CStr(.Tag) & "\" & fileDropped.Name If sender IsNot ListBox7 OrElse (sender Is ListBox7 AndAlso .Items.Count = 0) Then blocked = False Else MsgBox("Only one job at a time can be running!" & vbCrLf & vbCrLf & "Move first job from Queue before continuing.", vbCritical, "") blocked = True End If If CStr(.Tag) = "SMT-Complete" Then If CmbMoveTo.SelectedIndex = -1 Then MsgBox("Select a 'Move To' location from the drop down list!", vbExclamation, "Error") Exit Sub Else MoveLocation = strPath & "\" & CmbMoveTo.SelectedItem & "\" & fileDropped.Name End If Dim result As DialogResult = MessageBox.Show("Do you want to split this job?", "Job Split", MessageBoxButtons.YesNoCancel) If result = DialogResult.Yes Then NewFile = InputBox("Enter the quantity you are moving to: " & vbCrLf & vbCrLf & CStr(.Tag), "Update", fileDropped.Name) If NewFile.Contains("__SPLIT") Then MoveLocation = strPath & "\" & CStr(CmbMoveTo.SelectedValue) & "\" & NewFile Else MoveLocation = strPath & "\" & CStr(CmbMoveTo.SelectedValue) & "\" & NewFile & "__SPLIT" End If File.Copy(FileToMove, MoveLocation) DirectCast(sender, ListBox).Items.Add(New DirectoryInfo(strPath & "\" & CStr(.Tag)).GetFiles.Where(Function(fi) fi.FullName = MoveLocation).FirstOrDefault) Try Using sw As New StreamWriter(File.Open(MoveLocation, FileMode.OpenOrCreate)) sw.WriteLine("Moved To," & CStr(CmbMoveTo.SelectedValue) & "," & DateTime.Now & "," & NewFile & ",Job Split") End Using Catch ex As Exception MsgBox("File Update Error!" & vbCrLf & ex.Message) End Try ElseIf result = DialogResult.Cancel OrElse result = DialogResult.No Then File.Move(FileToMove, MoveLocation) .Items.Add(New DirectoryInfo(strPath & "\" & CmbMoveTo.SelectedValue).GetFiles.Where(Function(fi) fi.FullName = MoveLocation).FirstOrDefault) Try Using sw As New StreamWriter(File.Open(MoveLocation, FileMode.Append)) sw.WriteLine("Moved To," & CmbMoveTo.SelectedValue & "," & DateTime.Now & "," & fileDropped.Name) End Using Catch ex As Exception MsgBox("File Update Error!" & vbCrLf & ex.Message) End Try End If Else File.Move(FileToMove, MoveLocation) .Items.Add(New DirectoryInfo(strPath & "\" & CStr(.Tag)).GetFiles.Where(Function(fi) fi.FullName = MoveLocation).FirstOrDefault) Try Using sw As New StreamWriter(File.Open(MoveLocation, FileMode.Append)) sw.WriteLine("Moved To," & CStr(.Tag) & "," & DateTime.Now & "," & fileDropped.Name) End Using Catch ex As Exception MsgBox("File Update Error!" & vbCrLf & ex.Message) End Try End If End With End If Catch ex As Exception MsgBox("Duplicate Job Detected... " & ex.Message, vbExclamation, "File Error") End Try End Sub




Reply With Quote
