使用包含多个树视图节点复选框的单个面板(图像和附加代码)

时间:2012-07-24 20:02:48

标签: vb.net treeview panel

我是VB.NET的初学者。

我想实现以下目标:

  1. 单击Node1,打开包含复选框的面板。
  2. 用户将点击几个复选框。
  3. 用户单击node2,它将复选框信息导出到Excel工作表列,然后重置面板。
  4. 在面板上输入的新信息将导出到步骤3中使用的同一Excel工作表中的相邻列。
  5. 上述过程将持续90个节点。
  6. 如何在第3,4和5步中完成第一部分?

    这是我第一次尝试不起作用:

    Dim oXL As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    oWB = oXL.Workbooks.Open("F:\open.xlsx")
    oSheet = oWB.Worksheets("Sheet1")
    
    'I am not able to think clearly on following loop
    
    For i = 1 To 3
        For j = 1 To 90
            If CheckBox1.Checked Then
                oSheet.Cells(i, j).value = "1"
            Else : oSheet.Cells(i, j).value = "0"
            End If
            If CheckBox2.Checked Then
                oSheet.Cells(i, j).value = "1"
            Else : oSheet.Cells(i, j).value = "0"
            End If
            If CheckBox3.Checked Then
                oSheet.Cells(i, j).value = "1"
            Else : oSheet.Cells(i, j).value = "0"
            End If
        Next
    Next
    
    'Following works
    
    CheckBox1.Checked() = False
    CheckBox2.Checked() = False
    CheckBox3.Checked() = False
    ComboBox1.ResetText()
    

    enter image description here enter image description here

    enter image description here

1 个答案:

答案 0 :(得分:1)

您明确需要的是一种将用户选择存储在内存中的方法, 之前将其保存到电子表格中。有几种方法可以做到这一点,但鉴于你的经验不足,我建议你考虑最简单的,即定义一个基本类来表示用户对单个节点的选择,以及array - 其中每个项目是一个类的实例 - 存储整个用户选择集。

定义节点选择类 - 表示单个节点的选择:

Public Class NodeSelection
    Public CheckA As Boolean
    Public PickAIndex As Integer = -1
    Public CheckB As Boolean
    Public PickBIndex As Integer = -1
    Public CheckC As Boolean
    Public PickCIndex As Integer = -1
    Public ItemProcessed As Boolean
End Class

定义变量 - 在表单类中(不在子表单中):

Private _userPicks(89) As NodeSelection 'Array of user's selections
Private _previousIndex As Integer = -1  'Used to record the previously selected node

实例化数组项 - 在表单的Load事件中:

'Instantiate the class for each element of the array 
For i As Integer = 0 To _userPicks.Count - 1
    _userPicks(i) = New NodeSelection
Next

跟踪用户选择:

每当选择新节点时,您需要更新以前所选节点的数组项,然后重置当前节点的控件。最好在树视图的AfterSelect事件中完成:

Private Sub TreeView1_AfterSelect(sender As Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

    'Exit if the click is on the parent node (Node0)
    If e.Node.GetNodeCount(False) > 0 Then Return

    UpdateNodeInfo()

    'If the currently selected node has already been processed, 
    'restore user selection values to the controls,
    'otherwise reset the controls
    With _userPicks(e.Node.Index)
        CheckBox1.Checked = If(.ItemProcessed, .CheckA, False)
        ComboBox1.SelectedIndex = If(.ItemProcessed, .PickAIndex, -1)
        CheckBox2.Checked = If(.ItemProcessed, .checkB, False)
        ComboBox2.SelectedIndex = If(.ItemProcessed, .PickBIndex, -1)
        CheckBox3.Checked = If(.ItemProcessed, .checkC, False)
        ComboBox3.SelectedIndex = If(.ItemProcessed, .PickCIndex, -1)
    End With

    'Color the previous selection so the user can see it's been processed
    If _previousIndex >= 0 Then TreeView1.Nodes(0).Nodes(_previousIndex).BackColor = Color.AntiqueWhite

    'Record this (selected) node's index for updating when the next node is selected
    _previousIndex = e.Node.Index
End Sub

Private Sub UpdateNodeInfo()
    If _previousIndex < 0 Then Return 'No item has been set yet
    With _userPicks(_previousIndex)
        .CheckA = CheckBox1.Checked
        .PickAIndex = If(.CheckA, ComboBox1.SelectedIndex, -1)
        .CheckB = CheckBox2.Checked
        .PickBIndex = If(.CheckB, ComboBox2.SelectedIndex, -1)
        .checkC = CheckBox3.Checked
        .PickCIndex = If(.checkC, ComboBox3.SelectedIndex, -1)
        .ItemProcessed = True 'Record the fact the item has already been processed
    End With
End Sub

将值写入电子表格:

请注意,我将数组项更新例程放在单独的过程中。这是因为在将所有选择全部写入电子表格之前,您必须更新最终选择。我假设您将有一个按钮来保存他们的选择,因此您只需要从那里调用UpdateNodeInfo子,然后迭代数组并写入值。以下是您可以迭代值并更新电子表格的方法:

For i As Integer = 0 To _userPicks.Count - 1
    With _userPicks(i)
        oSheet.Cells(3, i + 2) = "Node" & (i + 1).ToString
        oSheet.Cells(9, i + 2) = "Node" & (i + 1).ToString
        oSheet.Cells(4, i + 2) = If(.ItemProcessed AndAlso .CheckA, 1, 0)
        oSheet.Cells(5, i + 2) = If(.ItemProcessed AndAlso .CheckB, 1, 0)
        oSheet.Cells(6, i + 2) = If(.ItemProcessed AndAlso .checkC, 1, 0)
        oSheet.Cells(10, i + 2) = If(.ItemProcessed AndAlso .CheckA, ComboBox1.Items(.PickAIndex).ToString, "")
        oSheet.Cells(11, i + 2) = If(.ItemProcessed AndAlso .CheckB, ComboBox1.Items(.PickBIndex).ToString, "")
        oSheet.Cells(12, i + 2) = If(.ItemProcessed AndAlso .checkC, ComboBox1.Items(.PickCIndex).ToString, "")
    End With
Next

我假设您已经知道如何打开,保存和关闭电子表格,因此我将把它留给您。熟悉这里列出的方法,如果不这样做,可以发布另一个问题。

最后

以上是实现您想要做的事情的一种相当简单的方法。如果您认为您可能需要为您的课程添加更多功能,您应该考虑将公共成员替换为properties - 有些人会说您应该这样做 - 并且您可能想要考虑存储完整的用户选择集在List(Of T)对象而不是数组中。