-
Aug 8th, 2022, 10:25 AM
#1
Thread Starter
Addicted Member
VB.NET Drag Drop Error
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
Last edited by mikeg71; Aug 8th, 2022 at 10:29 AM.
-
Aug 8th, 2022, 10:32 AM
#2
Re: VB.NET Drag Drop Error
Debug your code. You'll be able to see where it's using Nothing where a value is expected. Then, if you still need help, you'll be able to tell us.
-
Aug 8th, 2022, 11:18 AM
#3
Re: VB.NET Drag Drop Error
Why do you feel it needs more efficiency added? Is the performance too slow?
InputBox is a bad idea. That function has a rather fatal flaw that you can't always live with for long. The problem is that you can't tell whether the user pressed Cancel or OK (with no entry). Because of that, the user ends up kind of trapped. Even if they press Cancel...things continue. In this case, either one will result in something kind of bad.
My usual boring signature: Nothing
-
Aug 8th, 2022, 07:29 PM
#4
Re: VB.NET Drag Drop Error
Please don't edit existing posts after others have replied unless it is to correct a mistake. If you do edit a previous post, it's a good idea to add a new post that points that out. Otherwise, there's every chance that your edit won't be seen.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|