2017-07-03 46 views
0

這是我在Visual Basic窗體窗體應用程序中使用兩個列表框創建的程序,一個用於月份,另一個用於生日石。當用戶點擊幸運石時,相應的月份顯示在lblDescription控件中,或者當用戶在_strMonths列表框中單擊一個月時,相應的誕生石會顯示在lblDescription中。該程序正在工作,但我不小心刪除了它,現在我不記得確切的代碼了。我已經有一週的時間來重新創建它,但無濟於事。我研究過SelectedIndex屬性,但到目前爲止我看到的所有內容都是關於SelectedIndex屬性是一個整數,但我的是一個字符串。所以我來到論壇尋求幫助。代碼很簡單。我添加了一個MsgBox(_intFill),它顯示了所有的生日石。我快到了,但沒有雪茄。system.string [] /類型'String'的值不能在列表框的選定索引中轉換爲'String()'vb.net

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 
+0

不,SelectedIndex屬性肯定是一個Integer。你沒有使用它,使用數組的ToString()方法是沒有意義的。你需要strsStones(lstStones.SelectedIndex).ToString(),很容易。 –

回答

0
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 

也許這是你想要做什麼。

相關問題