Home > Blockchain >  Trying to migrate a Xaml designed project to Vb.net project with Windows form application
Trying to migrate a Xaml designed project to Vb.net project with Windows form application

Time:12-08

I am trying to migrate a project designed with Xaml to Vb.net project with windows form application. I am using TreeNode class in windows form application while System.Windows.Controllar is TreeView in Xaml project. So I am getting an error in my code below, here is how best to fix the error. The line where I get the error is: "AddHandler treeViewItem.Expanded, AddressOf tvi_Expanded" where "Expression does not produce a value." I get an error

       Private Function AddDirectoryNode(parentItemCollection As TreeNode, directoryDetail As DirectoryDetail) As Boolean
        Dim treeViewItem As TreeNode

        treeViewItem = New TreeNode()
        treeViewItem.Tag = directoryDetail



        AddHandler treeViewItem.Expand, AddressOf tvi_Expanded
        'If this Then directory contains subdirectories, add a placeholder
        If (directoryDetail.SubDirectoryDetails.Count() > 0) Then
            treeViewItem.Nodes.Add(New TreeNode() With {.Name = "placeholder"})
        End If

        ' Add the treeview item into the items collection
        parentItemCollection.Nodes.Add(treeViewItem)

        Return True
    End Function`` 


    Sub tvi_Expanded(sender As Object, e As EventArgs)
            Me.ExpandNode(CType(sender, TreeNode))
        End Sub

    Private Sub ExpandNode(tvi As TreeNode)

        If (tvi.Nodes.Count = 1 And CType(tvi.Nodes(0), TreeNode).Name = "placeholder") Then
            tvi.Nodes.Clear()
            For Each directoryDetail As DirectoryDetail In (CType(tvi.Tag, DirectoryDetail).SubDirectoryDetails.OrderBy(Function(x) x.Path))
                Me.AddDirectoryNode(tvi.Nodes.Add(directoryDetail.Path), directoryDetail)
            Next directoryDetail
        End If
    End Sub

    Below is the original version;
    
       Private Sub ExpandNode(tvi As System.Windows.Controls.TreeViewItem)
          If (tvi.Items.Count = 1 And CType(tvi.Items(0), System.Windows.Controls.TreeViewItem).Name = "placeholder") Then
             tvi.Items.Clear()
             For Each directoryDetail As DirectoryDetail In (CType(tvi.Tag, DirectoryDetail).SubDirectoryDetails.OrderBy(Function(x) x.Path))
                Me.AddDirectoryNode(tvi.Items, directoryDetail)
             Next directoryDetail
          End If
       End Sub
    
       Private Function AddDirectoryNode(parentItemCollection As System.Windows.Controls.ItemCollection, directoryDetail As DirectoryDetail) As Boolean
          Dim treeViewItem As System.Windows.Controls.TreeViewItem
          Dim stackPanel As System.Windows.Controls.StackPanel
    
          ' Create the stackpanel and it's content
          stackPanel = New System.Windows.Controls.StackPanel()
          stackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal
          ' Content
          stackPanel.Children.Add(Me.CreateProgressBar("Cumulative percentage from total used space {0}% ({1}))", directoryDetail.CumulativeSizePercentage, directoryDetail.FormattedCumulativeBytes))
          stackPanel.Children.Add(New System.Windows.Controls.TextBlock() With {.Text = directoryDetail.DirectoryName})
    
          ' Create the treeview item
          treeViewItem = New System.Windows.Controls.TreeViewItem()
          treeViewItem.Tag = directoryDetail
          treeViewItem.Header = stackPanel
          AddHandler treeViewItem.Expanded, AddressOf tvi_Expanded
    
          ' If this directory contains subdirectories, add a placeholder
          If (directoryDetail.SubDirectoryDetails.Count() > 0) Then
             treeViewItem.Items.Add(New System.Windows.Controls.TreeViewItem() With {.Name = "placeholder"})
          End If
    
          ' Add the treeview item into the items collection
          parentItemCollection.Add(treeViewItem)
    
          Return True
       End Function

  Sub tvi_Expanded(sender As Object, e As System.Windows.RoutedEventArgs)
      System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait
      Me.ExpandNode(CType(sender, System.Windows.Controls.TreeViewItem))
      System.Windows.Input.Mouse.OverrideCursor = Nothing
   End Sub

CodePudding user response:

The TreeView control does not have one event matching Expanded. It has events for BeforeExpand and AfterExpand. It sounds like you want AfterExpand.

The arguments (types) that are sent to your handler tvi_Expanded will probably not match, so you need to change your handler sub and your event binder.

Example:

Private Function AddDirectoryNode()
'... change the binding for your event handler
   AddHandler treeViewItem.AfterExpand, AddressOf tvi_Expanded
'... etc
End Function

Private Sub tvi_Expanded(sender As Object, e As TreeViewEventArgs) '
   Me.ExpandNode(e.Node) 'e.Node is the expanded TreeNode
End Sub

For more info, check the MS docs for TreeView.AfterExpand event https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.afterexpand?view=windowsdesktop-7.0 and the TreeViewEventArgs obj https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treevieweventargs.node?view=windowsdesktop-7.0

CodePudding user response:

I'm very close to the end. I have a little mistake, but I couldn't fix it. Where am I doing wrong? https://ibb.co/bXn0QYR not working stable. It shows 1 subdirectory after a space and when I click on the other empty directory I get an error.

Private Sub ExpandNode(tvi As TreeNode)

    If (tvi.Nodes.Count = 1 And CType(tvi.Nodes(0), TreeNode).Name = "placeholder") Then
        tvi.Nodes.Clear()
        For Each directoryDetail As DirectoryDetail In (CType(tvi.Tag, DirectoryDetail).SubDirectoryDetails.OrderBy(Function(x) x.Path))
            Me.AddDirectoryNode(tvi.Nodes.Add(directoryDetail.Path), directoryDetail)
        Next directoryDetail
    End If

End Sub

Private Function AddDirectoryNode(parentItemCollection As TreeNode, directoryDetail As DirectoryDetail) As Boolean
    Dim treeViewItem As TreeNode

    treeViewItem = New TreeNode()
    treeViewItem.Tag = directoryDetail
    If (directoryDetail.SubDirectoryDetails.Count() > 0) Then
        treeViewItem.Nodes.Add(New TreeNode() With {.Name = "placeholder"})
    End If

    parentItemCollection.Nodes.Add(treeViewItem)

    Return True
End Function
Private Sub tvi_Expanded(sender As Object, e As TreeViewEventArgs) '
    Me.ExpandNode(e.Node) 'e.Node is the expanded TreeNode
End Sub

Private Sub DirectoryTree_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles DirectoryTree.BeforeExpand
    Dim treeViewItem As TreeNode

    treeViewItem = New TreeNode()
    treeViewItem.Tag = e.Node

    Me.ExpandNode(e.Node)
    If (e.Node.Nodes.Count() > 0) Then
        treeViewItem.Nodes.Add(New TreeNode() With {.Name = "placeholder"})
    End If

    e.Node.Nodes.Add(treeViewItem)
End Sub
  • Related