从树形视图在两种表单之间传递信息

时间:2018-11-29 13:22:25

标签: vb.net visual-studio

我有一个子程序,它带来了第二个带有树视图的窗体,该窗体是从数组中填充的。我想单击数组上的一项,然后将键和文本传递回子项并关闭第二种形式。

我觉得这应该很容易,但是我无法弄清楚。数组按以下方式传递到树形视图。

    For j = 0 To NoFlowsheets - 1
        Form2.TreeView1.Nodes.Add("Flowsheet" & CStr(j), ColumnNames(j, 0))
        For k = 0 To j_max - 1
            If ColumnNames(j, k) <> "NAME_EMPTY DO_NOT_USE_THIS_NAME" Then
                Form2.TreeView1.Nodes(j).Nodes.Add("Flowsheet" & CStr(j), ColumnNames(j, k))
            End If
        Next k
    Next j
    Form2.ShowDialog()

此后,将弹出一个带有树视图的表单。我希望用户单击树状视图中的一项,然后将其传递回子

1 个答案:

答案 0 :(得分:2)

这是一个非常基本的示例,但是它显示了如何将某些内容传递到表单中,以及如何取回用户选择/输入的内容。您可以轻松地对此进行修改,以传入您的数组,然后传回所选的值。

在第一个表单(您在其中打开子表单)上,添加如下代码:

Public Class frmStart

    Private Sub btnAskUser_Click(sender As Object, e As EventArgs) Handles btnAskUser.Click
        Dim frmAskUserAboutThemselves As New frmQuestion(19, "John Doe")
        frmAskUserAboutThemselves.ShowDialog(Me)
        If frmAskUserAboutThemselves.WasRecordSaved = True Then
            lblStatus.Text = "Name: " & frmAskUserAboutThemselves.ValueThatUserSelectedOnTheFormName & vbCrLf & "Age: " & frmAskUserAboutThemselves.ValueThatUserSelectedOnTheFormAge
        Else
            lblStatus.Text = "The user did not enter/select any values."
        End If
        frmAskUserAboutThemselves.Dispose()
        Beep()
    End Sub

End Class

在子表单(您要求用户选择某项)上,您添加如下代码:

Public Class frmQuestion

#Region " Override Windows Form Designer Generated Code "

    Public Sub New(Optional ByVal iAge As Integer = 0, Optional ByVal sName As String = "")
        MyBase.New()

        m_iPassedInPersonAge = iAge
        m_sPassedInPersonName = sName

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
    End Sub

#End Region

#Region " Form Level Variables "

    Private m_iPassedInPersonAge As Integer = 0
    Private m_sPassedInPersonName As String = ""
    Private m_bWasRecordSaved As Boolean = False

#End Region

#Region " Form Level Functions "

    Public ReadOnly Property WasRecordSaved() As Boolean
        Get
            Return m_bWasRecordSaved
        End Get
    End Property

    Public ReadOnly Property ValueThatUserSelectedOnTheFormName() As String
        Get
            Return m_sPassedInPersonName
        End Get
    End Property

    Public ReadOnly Property ValueThatUserSelectedOnTheFormAge() As Integer
        Get
            Return m_iPassedInPersonAge
        End Get
    End Property

#End Region

#Region " Normal Page Code "

    Private Sub frmQuestion_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtName.Text = m_sPassedInPersonName
        NumericUpDownAge.Value = m_iPassedInPersonAge
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        m_bWasRecordSaved = False
        Me.Close()
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        m_iPassedInPersonAge = NumericUpDownAge.Value
        m_sPassedInPersonName = txtName.Text.Trim
        m_bWasRecordSaved = True
        Me.Close()
    End Sub

#End Region

End Class

如果您不确定我在每种表单上放置了哪些控件,请问一下,但这应该很容易弄清楚。