0

在我开始之前,如果有些事情可能不清楚,我想道歉,但我会尽力而为。我使用 vb net 制作了一个使用列表框(和文本框)和案例语句的程序。我试图创建一个类,稍后允许我将用户输入发送到访问数据库。我遇到的问题是我无法将包含 case 语句的变量值发送到数据库。

这是代码,可能有人可以帮助我解决问题:

这就是我接受用户输入的形式(它还包含文本框,但我已经弄清楚了):

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Fill Listbox with items
    Me.Controls.Add(ListBox1)
    ListBox1.Items.Add("Shoot in-laws")
    ListBox1.Items.Add("Become a paparazzi")
    ListBox1.Items.Add("Drive 5 mph over speed limit")
    ListBox1.Items.Add("Treat for rabies once a week")
    ListBox1.Items.Add("Cheat on income taxes")
    ListBox1.Items.Add("Breathe uder water")
    ListBox1.Items.Add("Recite the Gettysburg Address")
    ListBox1.Items.Add("Smile while being staked")
    ListBox1.Items.Add("Stay calm while getting help on C#")
    ListBox1.Items.Add("Day dream about playing chess")
    ListBox1.Items.Add("Take candy from a baby")

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Select Case ListBox1.SelectedIndex
        Case 0
            dblTestScore = 50
        Case 1
            dblTestScore = 15
        Case 2
            dblTestScore = 25
        Case 3
            dblTestScore = 55
        Case 4
            dblTestScore = 10
        Case 5
            dblTestScore = 20
        Case 6
            dblTestScore = 40
        Case 7
            dblTestScore = 65
        Case 8
            dblTestScore = 30
        Case 9
            dblTestScore = 35
        Case 10
            dblTestScore = 45
    End Select

End Sub

现在下面的代码在类中:

Public Class ClsWriteRecord
Public Sub addNewRecordMore(ByVal listTestScore As ListBox, ByVal listTEST As ListBox, 

'declare variables
Dim dblTestScore As Double
Dim strTEST As String

'Get values

strTEST = listTEST.GetItemText(listTEST.SelectedItem)
dblTestScore = listTEST.GetItemText(listTEST.SelectedIndex)

End Sub
End Class

strTEST 正常工作并获取列表框项目并将其发送到数据库(例如“Shoot in-laws”),但 dblTestScore 没有。取而代之的是值,例如 50,15, 25 等,它发送 0,1,2 这是案例编号。

谁能帮我弄清楚?我搜索互联网但找不到答案。提前致谢。

4

1 回答 1

0

这对您非常有用...请参见下文...这是您要从主窗体或其他任何需要它的地方调用的课程...

Public Class ClsWriteRecord

 Public Shared Function addNewRecordMore(ByVal listTestScore As Integer, ByVal listTEST As String)

    'Do your save routine here... and pass your information to it...'
    'Example...' Save(listTestScore, listTEST)
    'You would have to create the save function of course...'

 End Function

End Class

然后这是您的主要形式或您拥有的任何其他形式...

 Public Class Form1

'Our global variables...'
Private dblTestScore As Integer = 0
Private strTest As String = String.Empty

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    'Put your items into an array then add this to the listbox...'
    Dim strItems As String() = {"Shoot in-laws", "Become a paparazzi", "Drive 5 mph over speed limit", "Treat for rabies once a week"} 'Add more as needed...'
    'Add your items...'
    ListBox1.Items.AddRange(strItems)

End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    'Set your variable for your text here...'
    strTest = ListBox1.SelectedItem

    'Build your case statement...'
    Select Case ListBox1.SelectedItem

        Case "Shoot in-laws"
            dblTestScore = 50
        Case "Become a paparazzi"
            dblTestScore = 15
        Case "Drive 5 mph over speed limit"
            dblTestScore = 25
        Case "Treat for rabies once a week"
            dblTestScore = 55
            'Continue your case on down here...'
    End Select

    'Call our function from other class We have our value and we have our text already...
    ClsWriteRecord.addNewRecordMore(dblTestScore, strTest)

End Sub
End Class

谢谢,让我知道它对你有用...

于 2013-03-02T04:45:18.350 回答