使用包含多个树视图节点的复选框的单个面板(附加了图像和代码) [英] Using single panel containing checkboxes for multiple treeview nodes (Images & Code Attached)

查看:83
本文介绍了使用包含多个树视图节点的复选框的单个面板(附加了图像和代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VB.NET初学者.

I am a VB.NET beginner.

我想实现以下目标:

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

我该如何执行步骤3、4和5的第一部分?

How do I do the first part in steps 3, 4 and 5?

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

This is my first try which is not working:

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()

推荐答案

您明确需要的是一种将用户选择存储在内存中的方法,保存到电子表格之前.有几种方法可以执行此操作,但是鉴于您的经验不足,我建议您考虑最简单的方法,即定义一个基本类来表示用户对单个节点的选择,并定义一个

What you clearly need is a way to store the user's selections in memory, before saving them to the spreadsheet. There are several ways you could do this, but given your inexperience I suggest you consider the simplest, which is to define a basic class to represent the user's selections for a single node, and an array – where each item is an instance of the class – to store the entire set of user selections.

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

Define the node selection class – to represent selections for a single node:

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

定义变量 –在表单类中(而不是在子类中):

Define your variables – in your form class (not in a sub):

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 array items – in the form's Load event:

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

跟踪用户选择:

每当选择了新的节点,则需要更新的先前选择的节点然后重置控制对于当前节点的数组项.最好在树视图的AfterSelect事件中完成

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. This is best done in the treeview's AfterSelect event:

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子,然后遍历数组并写入值.您可以通过以下方法遍历值并更新电子表格:

Writing values to the spreadsheet:

Notice that I put the array item update routine in a separate procedure. This is because you will have to update the final selection before writing it all out to the spreadsheet. I presume you will have a button to save their selections, so you just need to call the UpdateNodeInfo sub from there before iterating over the array and writing the values. Here is how you might iterate over the values and update the spreadsheet:

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

我假设您已经知道如何打开,保存和关闭电子表格,所以我将其留给您.熟悉此处概述的方法,如果没有,请发布另一个问题.

I assume you already know how to open, save and close the spreadsheet, so I'll leave that side of it up to you. Get familiar with the methods outlined here and post another question if you don't.

以上是实现您要执行的操作的一种非常简单的方法.如果您认为可能需要为类添加更多功能,则应考虑用公共成员替换

The above is a fairly simple way to achieve what you're trying to do. If you think you may need to add more functionality to your class, you should look at substituting public members for properties – some would say you should do that anyway – and you may want to consider storing the complete set of user selections in a List(Of T) object rather than an array.

这篇关于使用包含多个树视图节点的复选框的单个面板(附加了图像和代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆