我想将组合框值保存在变量中。但是每当我更改 comboBox 值时,值设置为空,并且所选索引显示为"-1"。下面是我的代码。
Private Sub SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim form As CreateEvalForm = New CreateEvalForm //windows Form
Dim str As String = form.ComboBox1.SelectedIndex
MessageBox.Show(str) //shows null
Dim openingId As Integer = Val(form.ComboBox1.Text)
End Sub
任何人都可以提出解决方案吗?
Private Sub SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim str As String = ComboBox1.SelectedIndex
MessageBox.Show(str)
Dim openingId As Integer = Val(ComboBox1.Text)
End Sub
这行得通吗?但是,如果要从组合框的选定值中获得值,则应尝试以下操作:
dim openingId as Integer = Val(comboBox1.SelectedValue)
Dim form As CreateEvalForm = New CreateEvalForm //windows Form
使用此行,您将创建一个新表单,因此该新表单上的组合框也将是新的,并且不会有选定的索引。
您可以通过这种方式使用组合框:
Dim str As String = ComboBox1.SelectedIndex
或
Dim str As String = Me.ComboBox1.SelectedIndex