1

我是 VB.NET 初学者。

我想实现以下目标:

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

如何执行步骤 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()

在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

4

1 回答 1

1

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

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

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

跟踪用户选择:

Whenever a new node is selected, you need to update the array item for the previously selected node then reset the controls for the current node. 这最好在树视图的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在遍历数组并写入值之前从那里调用 sub。以下是您可以如何迭代这些值并更新电子表格:

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

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

最后

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

于 2012-07-25T16:21:21.853 回答