Josh Crowe Josh Crowe - 1 month ago 8
Vb.net Question

SelectedItem not Passing to Property

I'm trying to assign the

.SelectedItem
to a
Property
on a secondary form that can be used by my main form, though despite a
MessageBox.Show
clearly outlining the variable has been set, my
Select Case
switches to
Case Else
and another
MessageBox.Show
shows the same variable but as
Nothing
.

(Secondary Form)

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


(Primary Form)

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


Am I passing it incorrectly?

Answer

Default form instances strike again. No surprise there. You start by explicitly creating an instance and displaying that:

Dim setupForm As New Setup
setupForm.ShowDialog()

Later though, you ignore that instance and use the default instance:

Select Case Setup.WindowSize

You never displayed the default instance so you never set that property in the default instance. That line should have referred to the instance you actually did display:

Select Case setupForm.WindowSize