Hi,

I am trying to use a tree view control, I use a button from a form to open a newform which the treeview is on, I can do this once, and select a file, but when I try to reopen the form with the treeview on a second time I get this message:

'Object Reference not set ta an instance of an object'

This is the code I am using:

VB Code:
  1. #Region " Form Load "
  2.  
  3.     Private Sub frmBrowser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         lbl_SelectedFile.Text = "Selected File:"
  5.  
  6.         LoadTreeView()
  7.  
  8.     End Sub
  9. #End Region
  10.  
  11.     Private Const DUMMY As String = "DUMMY"
  12.  
  13.     Private Enum ItemType
  14.         Directory = 1
  15.         File = 2
  16.     End Enum
  17.  
  18.     Private Sub tvw_Files_Root_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvw_Files_Root.AfterSelect
  19.         Try
  20.             With e.Node
  21.                 Select Case .Tag
  22.                     Case ItemType.File
  23.                         Dim fi As New FileInfo(.FullPath)
  24.                         DisplayFSIProperties(fi)
  25.                     Case Else
  26.                 End Select
  27.             End With
  28.         Catch exp As Exception
  29.             MsgBox(exp.Message, , "after")
  30.             MsgBox(exp.TargetSite, , "after")
  31.         End Try
  32.     End Sub
  33.  
  34.     Private Sub tvw_Files_Root_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvw_Files_Root.BeforeExpand
  35.         Try
  36.             e.Node.Nodes.Clear()
  37.             AddFolders(e.Node)
  38.             AddFiles(e.Node)
  39.         Catch exp As IOException
  40.             MsgBox(exp.Message, , "before IO")
  41.         Catch exp As Exception
  42.             MsgBox(exp.Message, , "before exp")
  43.         End Try
  44.     End Sub
  45.  
  46.     Private Sub AddFiles(ByVal nod As TreeNode)
  47.         Dim strPath As String = nod.FullPath
  48.         Dim strFile As String
  49.         With nod
  50.             For Each strFile In Directory.GetFiles(strPath)
  51.                 With nod.Nodes.Add(Path.GetFileName(strFile))
  52.                     .Tag = ItemType.File
  53.                 End With
  54.             Next
  55.         End With
  56.     End Sub
  57.  
  58.     Private Sub AddFolders(ByVal nod As TreeNode)
  59.         Dim strPath As String = nod.FullPath
  60.         Dim strDir As String
  61.         With nod
  62.             For Each strDir In Directory.GetDirectories(strPath)
  63.                 With nod.Nodes.Add(Path.GetFileName(strDir))
  64.                     .Tag = ItemType.Directory
  65.                     .Nodes.Add(DUMMY)
  66.                 End With
  67.             Next
  68.         End With
  69.     End Sub
  70.  
  71.     Private Sub LoadTreeView()
  72.         Dim strDrive As String
  73.         tvw_Files_Root.Nodes.Clear()
  74.         For Each strDrive In Directory.GetLogicalDrives()
  75.             With tvw_Files_Root.Nodes.Add(strDrive)
  76.                 .Nodes.Add(DUMMY)
  77.             End With
  78.         Next
  79.     End Sub
  80.  
  81.     Private Sub DisplayFSIProperties(ByVal fsi As FileSystemInfo)
  82.         Me.lbl_SelectedFile.Text = "Selected File: " & fsi.Name
  83.         FilePath = fsi.FullName
  84.         FileName = fsi.Name
  85.     End Sub
  86.  
  87.     Private Sub cmd_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cancel.Click
  88.         Me.Hide()
  89.     End Sub
  90.  
  91.     Private Sub Cmd_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd_ok.Click
  92.         If FilePath = "" Or FileName = "" Then
  93.             MsgBox("A file was not selected" & vbCrLf & "Please Select a file", MsgBoxStyle.OKOnly, "No File Selected")
  94.         Else
  95.             If Male = True Then
  96.                 MSPRDir = FilePath
  97.                 MainForm.txt_MaleSPRFile.Text = FileName
  98.                 MSPRName = FileName
  99.             Else
  100.                 If Female = True Then
  101.                     FSPRDir = FilePath
  102.                     MainForm.txt_FemaleSPRFile.Text = FileName
  103.                     FSPRName = FileName
  104.                 Else
  105.                     If Male = False And Male = False Then
  106.                         GENDir = FilePath
  107.                         MainForm.txt_Genfile.Text = FileName
  108.                         GENName = FileName
  109.                     End If
  110.                 End If
  111.             End If
  112.             Me.Hide()
  113.         End If
  114.  
  115.     End Sub

Any help?

Thanx