0
無法將異常連接到文本框輸入 - 程序只應接受「IL」,「MO」或「WI」 - 但它接受每個輸入。我試過的每一件事我都碰到了一堵磚牆 - 我知道解決方案就在那裏,我只需要一些幫助。由於文本框和類
表代碼
公共類Form1中
Dim packages(4) As Packages
Dim pkg As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
packages(0) = New Packages("Z111", "Eau Claire", "WI")
packages(1) = New Packages("Z121", "Vicki Vale", "IL")
packages(2) = New Packages("Z131", "Sammy Davis", "MO")
packages(3) = New Packages("Z141", "Jon Smith", "IL")
packages(4) = New Packages("Z151", "Suzie Cassidy", "WI")
DisplayInfo()
End Sub
Sub DisplayInfo()
txtID.Text = packages(pkg).PackageID
txtCity.Text = packages(pkg).DestinationCity
txtState.Text = packages(pkg).DestinationState
lblPackageCount.Text = "Package # " & pkg + 1
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
pkg = 0
DisplayInfo()
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If pkg > 0 Then
pkg -= 1
DisplayInfo()
End If
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If pkg < packages.Length - 1 Then
pkg += 1
DisplayInfo()
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
pkg = packages.Length - 1
DisplayInfo()
End Sub
Private Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click
Try
packages(pkg).PackageID = txtID.Text
packages(pkg).DestinationCity = txtCity.Text
packages(pkg).DestinationState = txtState.Text
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
末級
類代碼
公共類包
Private packID As String
Private desCity As String
Private desState As String
Sub New(id As String, city As String, state As String)
packID = id
desCity = city
desState = state
End Sub
Public Property PackageID As String
Get
Return packID
End Get
Set(value As String)
packID = value
End Set
End Property
Public Property DestinationCity As String
Get
Return desCity
End Get
Set(value As String)
desCity = value
End Set
End Property
Public Property DestinationState As String
Get
Return desState
End Get
Set(value As String)
If desState = "IL" Then
desState = value
ElseIf desState = "WI" Then
desState = value
ElseIf desState = "MO" Then
desState = value
Else
Throw New Exception("Can not deliver to this state")
End If
End Set
End Property
末級
我不確定「接受每一個輸入」是什麼意思。我看到,一旦你點擊「btnSaveChanges」,你的「txtState」就會有「PA」,你應該有一個消息框 –
如果我輸入AZ到狀態文本框,它將它發佈到數組中,它不會引發異常。 – Gendude
你可能只是在DestinationState屬性的setter處設置一個斷點,並找出這裏設置的值 - 這可能有點奇怪......從那裏你可以深入到原因。 – Rex