如何输出与所选索引关联的字符串...我的输出是选定的索引整数(我不想要这个)

时间:2012-04-13 00:34:55

标签: vb.net

我是学生,请耐心等待......以下是我的代码:

Private Sub cboYear_SelectedIndexChanged(ByVal sender As System.Object, 
 ByVal e As System.EventArgs) Handles cboYear.SelectedIndexChanged

  'declare variables
  Dim intSelectedYear As Integer
  Dim intNumhUUR As Integer

  'select year from combobox
  intSelectedYear = cboYear.SelectedIndex

  ' get number of hurricanes for selected year from number 
  ' of hurricanes array 
  intNumhUUR = _intNumberOfHurricanes(intSelectedYear)

  'and display in label
  lblNumberOfHurricanes.Visible = True
  lblNumberOfHurricanes.Text = "The number of Hurricanes " &
    "in the year " & intSelectedYear.ToString & "  is " & 
    intNumhUUR.ToString()
End Sub

2 个答案:

答案 0 :(得分:2)

您需要使用组合框的SelectedItem属性来获取当前在SelectedIndex位置选择的项目。即:

Private Sub cboYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboYear.SelectedIndexChanged
  'declare variables
  Dim intSelectedIndex As Integer
  Dim intNumhUUR As Integer
  Dim strSelectedYear As String

  'select year from combobox
  intSelectedIndex = cboYear.SelectedIndex
  strSelectedYear = cboYear.SelectedItem.ToString()
  ' get number of hurricanes for selected year from number 
  ' of hurricanes array 
  intNumhUUR = _intNumberOfHurricanes(intSelectedIndex)

  'and display in label
  lblNumberOfHurricanes.Visible = True
  lblNumberOfHurricanes.Text = string.Format("The number of Hurricanes in the year {0} is {1}", strSelectedYEar, intNumhUUR)
End Sub

答案 1 :(得分:0)

cboYear是一个下拉列表。我认为cboYear.SelectedValue会给你字符串,或者你可以尝试cboYear.Items [cboYear.SelectedIndex] .Text