Visual Basics,将comboBox值存储在变量中

时间:2017-07-26 04:41:00

标签: vb.net combobox

我想将comboBox值保存在变量中。但每当我更改comboBox值时,设置的值为null,所选索引显示为&#39; -1&#39; .Below是我的代码。< / p>

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

有人可以建议一个解决方案吗?

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

 Dim form As CreateEvalForm = New CreateEvalForm //windows Form

使用此行创建一个新表单,因此新表单上的组合框也将是新的,并且没有选定的索引。

你可以这样使用组合框:

Dim str As String = ComboBox1.SelectedIndex

Dim str As String = Me.ComboBox1.SelectedIndex