SelectedItem未传递给Property

时间:2016-10-19 10:25:13

标签: vb.net winforms

我正在尝试将.SelectedItem分配给我的主表单可以使用的辅助表单上的Property,尽管MessageBox.Show明确概述了该变量已被设置,我的Select Case切换为Case Else,另一个MessageBox.Show显示相同的变量,但显示为Nothing

(中学表格)

Public Class Setup

    Public Property HomePage As String
    Public Property WindowSize As String

    Private Sub Setup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim SizePoss() As String = {"Desktop", "Tablet", "Mobile"}

        cmbSizeSetting.MaxDropDownItems = SizePoss.Length
        For Each column As String In SizePoss
            cmbSizeSetting.Items.Add(column)
        Next

        ' Initialize Appropriate Settings

        lblUnsaved.Hide()
        cmbSizeSetting.SelectedIndex = cmbSizeSetting.FindStringExact("Tablet")
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        HomePage = txtHomepageSetting.Text
        WindowSize = cmbSizeSetting.SelectedItem
        MessageBox.Show("Size chosen was " + WindowSize + ", which is a " + TypeName(WindowSize))

        Close()
    End Sub
End Class

(主要表格)

Public Class Croweb

    Private Sub btnSetup_Click(sender As Object, e As EventArgs) Handles btnSetup.Click
        Dim setupForm As New Setup
        setupForm.ShowDialog()

        If Not String.IsNullOrEmpty(setupForm.HomePage) Then
            MessageBox.Show(setupForm.HomePage)
            WebBrowser1.Url = New Uri(setupForm.HomePage)
        End If

        Select Case Setup.WindowSize
            Case "Desktop"
                Me.Size = New Size(1280, 730)
            Case "Tablet"
                Me.Size = New Size(891, 564)
            Case "Phone"
                Me.Size = New Size(400, 900)
            Case Else
                MessageBox.Show("Error: Chosen Size is " + Setup.WindowSize + ", which is a " + TypeName(Setup.WindowSize))
        End Select

    End Sub

End Class

我传不正确吗?

1 个答案:

答案 0 :(得分:1)

默认表单实例再次出现。这并不奇怪。首先明确创建一个实例并显示:

Dim setupForm As New Setup
setupForm.ShowDialog()

稍后,您忽略该实例并使用默认实例:

Select Case Setup.WindowSize

您从未显示默认实例,因此您永远不会在默认实例中设置该属性。该行应该引用您实际显示的实例:

Select Case setupForm.WindowSize