1

我是学生,请多多包涵……以下是我的代码:

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
4

2 回答 2

2

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

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
于 2012-04-13T00:44:20.663 回答
0

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

于 2012-04-13T00:46:10.040 回答