vb.net 列表框和 case 语句 - 将其存储在数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15170479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listbox and case statement - store it in a database
提问by marion
Before i begin i'd like to apologize if some things may be unclear but i try my best. Im using vb net to make a program that uses a listbox(and textboxes) and a case statement. Im trying to make a class that will later allow me to to send the user input into an access database. The problem im having is that i can't get the variable value that contains the case statement send to the database.
在我开始之前,如果有些事情可能不清楚,我想道歉,但我会尽力而为。我使用 vb net 制作一个使用列表框(和文本框)和 case 语句的程序。我正在尝试创建一个稍后允许我将用户输入发送到访问数据库的类。我遇到的问题是我无法获取包含发送到数据库的 case 语句的变量值。
Here's the code, and possibly someone could help me resolve the problem:
这是代码,可能有人可以帮助我解决问题:
This is what i have in the form that accepts the user input(it also contains textboxes but i have them figured out):
这是我在接受用户输入的表单中所拥有的(它还包含文本框,但我已经弄清楚了):
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
Now the following code is in the class:
现在类中的代码如下:
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
The strTEST works properly and gets and sends listbox items to database(exampe "Shoot in-laws"), but dblTestScore doesnt. Instead values, for example 50,15, 25 etc it sends 0,1,2 which is the case number.
strTEST 正常工作并将列表框项获取并发送到数据库(例如“Shoot in-laws”),但 dblTestScore 没有。取而代之的是值,例如 50,15, 25 等,它发送 0,1,2,这是案例编号。
Could anyone help me figure it out? I search internet but couldnt find an answer. Thanks in advance.
有人可以帮我弄清楚吗?我在互联网上搜索,但找不到答案。提前致谢。
回答by ???ěxě?
This will work for you just great... Please see below... This is your class you want to call from your main form or anywhere else you need it...
这对你很有用......请看下面......这是你想从你的主窗体或任何你需要它的地方调用的类......
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
Then here is your main form or whatever else form you have...
那么这是您的主要形式或您拥有的任何其他形式......
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
Thanks, let me know how it works out for you...
谢谢,让我知道它对你有用...

