我正在尝试制作一个程序,将文件从一个文件夹复制到另一个文件夹,并且我还在使用进度条查看正在复制的文件。当我运行该程序时,在folderbrowserdialog1上收到错误消息。错误是System.NullReferenceException。当我运行程序时,我可以打开应用程序,但是当我选择按钮时,它给我一个错误。
Public Class Form1
Dim my As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess
Dim directoryTargetLocation As String 'Selected file path
Dim Destinydirectory As String 'Selected dest directory path
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As Object = Nothing
FolderBrowserDialog1.Description = "Select directory" **// Gettig error in this Line**
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
directoryTargetLocation = .SelectedPath
TextBox1.Text = directoryTargetLocation.ToString
Button2.Enabled = True
End If
End With
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
FolderBrowserDialog2.Description = "Select destiny directory"
With FolderBrowserDialog2
If .ShowDialog() = DialogResult.OK Then
Destinydirectory = .SelectedPath
TextBox2.Text = Destinydirectory.ToString
TextBox2.Text = TextBox2.Text.Remove(TextBox2.Text.Length - 1) &
TextBox1.Text.Substring(TextBox1.Text.LastIndexOf("\"))
Button3.Enabled = True
End If
End With
End Sub
答案 0 :(得分:1)
您尚未创建FolderBrowserDialog1对象。
修改
您接近了!
您需要的是:
Dim FolderBrowserDialog1 As New FolderBrowserDialog
这将创建一个新的FolderBrowserDialog对象。
您拥有的是:
Dim FolderBrowserDialog1 As Object = Nothing
哪个将创建新的通用对象名称FolderBrowserDialog1,并将其显式设置为“ nothing”
这意味着它不具有您所引用的任何属性或方法,因此会出现错误。
答案 1 :(得分:1)
您已将FolderBrowserDialog1声明为对象。
您要做的是将其声明为FolderBrowserDialog。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As New FolderBrowserDialog
FolderBrowserDialog1.Description = "Select directory"
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
directoryTargetLocation = .SelectedPath
TextBox1.Text = directoryTargetLocation.ToString
Button2.Enabled = True
End If
End With
End Sub