【问题标题】:system.string[] /value of type 'String' cannot be converted to 'String()' in selected Index in listbox vb.netsystem.string[] / 'String' 类型的值不能在列表框 vb.net 的选定索引中转换为 'String()'
【发布时间】:2017-12-06 06:04:30
【问题描述】:

这是我在 Visual Basic 窗口窗体应用程序中创建的一个程序,它使用两个列表框,一个用于月份,另一个用于生日石。当用户单击 Birthstone 时,相应的月份会显示在 lblDescription 控件中,或者当用户单击 _strMonths 列表框中的月份时,相应的 Birthstone 会显示在 lblDescription 中。该程序正在运行,但我不小心删除了它,现在我不记得确切的代码了。我已经用了一个星期来重新创建它,但无济于事。我研究了 SelectedIndex 属性,但到目前为止我所看到的一切都是关于 SelectedIndex 属性是一个整数,但我的是一个字符串。所以我来论坛寻求帮助。代码很简单。我添加了一个显示所有生日石的 MsgBox(_intFill)。我快到了,但还没有雪茄。

Option Strict On

Public Class frmBirthstones
    'Declare class variables
    Private _strStones(11) As String
    Private _strMonths(11) As String
    Public Shared _intFill As String
    Public Shared _selectedIndex As String

    Private Sub frmBirthstones_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Array items for Brith Stones
        _strStones(0) = "Garnet"
        _strStones(1) = "Amethyst"
        _strStones(2) = "Aquamarine"
        _strStones(3) = "Diamond"
        _strStones(4) = "Emerald"
        _strStones(5) = "Pearl"
        _strStones(6) = "Ruby"
        _strStones(7) = "Peridot"
        _strStones(8) = "Sapphire"
        _strStones(9) = "Opal"
        _strStones(10) = "Topaz"
        _strStones(11) = "Turquoise"
        'Array items for Months
        _strMonths(0) = "January"
        _strMonths(1) = "February"
        _strMonths(2) = "March"
        _strMonths(3) = "April"
        _strMonths(4) = "May"
        _strMonths(5) = "June"
        _strMonths(6) = "July"
        _strMonths(7) = "August"
        _strMonths(8) = "September"
        _strMonths(9) = "October"
        _strMonths(10) = "November"
        _strMonths(11) = "December"

        'Makes label Description visible
        lblDescription.Visible = True

        For Each _intFill In _strStones
            'fills listbox with Birthstones
            lstStones.Items.Add(_intFill)
         MsgBox(_intFill)

        Next
        For Each _intFill In _strMonths
            'fills listbox with Months
            lstMonths.Items.Add(_intFill)
        Next

    End Sub

    Private Sub lstStones_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstStones.SelectedIndexChanged
        'When user click or selects a Birthstone in this listbox a corresponding Month for the Birthstone is selected

        '_strStones = _intFill
        'Doesn't work
        lblDescription.Text = _strStones.ToString() & "is the Birthstone for the month of " & _strMonths.ToString()

    End Sub

    Private Sub lstMonths_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstMonths.SelectedIndexChanged
        'When user click or selects a Month in this listbox a corresponding Birthstone for that Month is selected

        '_strMonths = 
        lblDescription.Text = _strMonths.ToString() & "is the Month for the Birthstone " & _strStones.ToString()
    End Sub
End Class

【问题讨论】:

  • 不,SelectedIndex 属性肯定是一个整数。您没有使用它,使用数组的 ToString() 方法没有任何意义。你需要 strsStones(lstStones.SelectedIndex).ToString(),很简单。

标签: arrays vb.net listbox selectedindexchanged


【解决方案1】:
Option Strict On

Public Class frmBirthstones
    Private _strStones(11) As String
    Private _strMonths(11) As String

    Private Sub frmBirthstones_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Array items for Brith Stones
        _strStones(0) = "Garnet"
        _strStones(1) = "Amethyst"
        _strStones(2) = "Aquamarine"
        _strStones(3) = "Diamond"
        _strStones(4) = "Emerald"
        _strStones(5) = "Pearl"
        _strStones(6) = "Ruby"
        _strStones(7) = "Peridot"
        _strStones(8) = "Sapphire"
        _strStones(9) = "Opal"
        _strStones(10) = "Topaz"
        _strStones(11) = "Turquoise"
        'Array items for Months
        _strMonths(0) = "January"
        _strMonths(1) = "February"
        _strMonths(2) = "March"
        _strMonths(3) = "April"
        _strMonths(4) = "May"
        _strMonths(5) = "June"
        _strMonths(6) = "July"
        _strMonths(7) = "August"
        _strMonths(8) = "September"
        _strMonths(9) = "October"
        _strMonths(10) = "November"
        _strMonths(11) = "December"



        'Makes label Description visible
        lblDescription.Visible = True


        'fills listbox with Birthstones
        lstStones.Items.AddRange(_strStones)


        'fills listbox with Months
        lstMonths.Items.AddRange(_strMonths)




    End Sub

    Private Sub lstStones_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lstStones.SelectedIndexChanged
        lblDescription.Text = _strStones(lstStones.SelectedIndex).ToString() & " is the Birthstone for the month of " & _strMonths(lstStones.SelectedIndex).ToString()
    End Sub

    Private Sub lstMonths_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lstMonths.SelectedIndexChanged
        lblDescription.Text = _strMonths(lstMonths.SelectedIndex).ToString() & " is the Month for the Birthstone " & _strStones(lstMonths.SelectedIndex).ToString()
    End Sub
End Class

也许这就是你想要做的。

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多