在我开始之前,我想道歉,如果有些事情可能不清楚,但我会尽我所能。 我使用vb net制作一个使用列表框(和文本框)和case语句的程序。我试图创建一个类,以后允许我将用户输入发送到访问数据库。我遇到的问题是我无法获得包含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正常运行并获取并将列表框项目发送到数据库(例如"拍摄姻亲"),但dblTestScore没有。而是值,例如50,15,25等,它发送0,1,2,这是案例编号。
任何人都可以帮我搞清楚吗?我搜索互联网但无法找到答案。提前谢谢。
答案 0 :(得分: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
谢谢,让我知道它是如何为你工作的......