[英]How do I perform a total calculation using price in a combo box?
I am attempting to make a project on visual studio.我正在尝试在视觉工作室上制作一个项目。
I have the following data in a combo box and I was wondering how I would be able to multiply the price (the CStr
value) by the number of days the user selects, showing the total in another text box using a calculate button我在组合框中有以下数据,我想知道如何将价格(
CStr
值)乘以用户选择的天数,使用计算按钮在另一个文本框中显示总数
{cmbPedigreeDog.Items.Add("African Hairless" & CStr(1.14))
cmbPedigreeDog.Items.Add("Boxer" & CStr(0.86))
cmbPedigreeDog.Items.Add("Chihuahua" & CStr(1.83))
cmbPedigreeDog.Items.Add("Dalmation" & CStr(0.65))
cmbPedigreeDog.Items.Add("Eskimo Dog" & CStr(1.14))
cmbPedigreeDog.Items.Add("Farm Collie" & CStr(0.95))
cmbPedigreeDog.Items.Add("GreyHound" & CStr(1.99))
cmbPedigreeDog.Items.Add("Husky" & CStr(1.85))
cmbPedigreeDog.Items.Add("Irish Setter" & CStr(0.65))
cmbPedigreeDog.Items.Add("Jack Russell Terrier" & CStr(1.77))
cmbPedigreeDog.Items.Add("King Charles Spaniel" & CStr(1.02))
cmbPedigreeDog.Items.Add("Labrador Retreiver" & CStr(1.74))
cmbPedigreeDog.Items.Add("Maltese" & CStr(1.47))
cmbPedigreeDog.Items.Add("Pug" & CStr(1.31))
cmbPedigreeDog.Items.Add("Rottweiler" & CStr(2.17))
cmbPedigreeDog.Items.Add("St Bernard" & CStr(1.63))
cmbPedigreeDog.Items.Add("Tibetan Mastiff" & CStr(1.15))
cmbPedigreeDog.Items.Add("Working Sheep Dog" & CStr(0.75))
cmbPedigreeDog.Items.Add("Yorkshire Terrier" & CStr(0.88))
cmbPedigreeDog.Items.Add("Other" & CStr(1.22))}
Not the best way, it requires that all your strings in combobox are of the same pattern with same quantity of delimiters.不是最好的方法,它要求 combobox 中的所有字符串都具有相同的模式和相同数量的分隔符。
Example: If you use instead of "Boxer" & CStr(0.86)
something like "Boxer|" & CStr(0.86)
示例:如果您使用
"Boxer" & CStr(0.86)
代替"Boxer|" & CStr(0.86)
"Boxer|" & CStr(0.86)
(added just a | symbol), then you can split this string back like this. "Boxer|" & CStr(0.86)
(只添加了一个 | 符号),然后您可以像这样将该字符串拆分回来。
line = cmbPedigreeDog.Value
price = CDbl(Split(line, "|")(1))
And some explanations:以及一些解释:
Each line of text is a container of symbols.每一行文本都是一个符号容器。 Each can be splitted to array with some delimiter.
每个都可以用一些分隔符拆分为数组。 So the line
price = CDbl(Split(line, "|")(1))
can be also written like this:所以行
price = CDbl(Split(line, "|")(1))
也可以这样写:
Dim someArray()
line = "Boxer|" & CStr(0.86)
someArray = Split(line, "|")
' after splitting you have two items in array one is "Boxer" and another is "0.86" (which is text)
' if you follow the pattern in each of your combobox options the price always will be the second one
' so you may refer to second item in array, which is number 1
Price = CDbl(someArray(1))
UPDATE更新
Another way, as per request in comment.另一种方式,根据评论中的要求。 Use two columns combobox.
使用两列 combobox。 To fill your combobox you have to do following
要填写您的 combobox 您必须执行以下操作
1 - Go to you ComboBox's properties and set the "ColumnCount" values to 2 (see here how to do it). 1 - Go 到您组合框的属性并将“ColumnCount”值设置为 2(请参阅此处如何操作)。
2 - This is up to you - you may use the binding with prices on a worksheet as like in topic I gave a link in p.1 or change each line on your code to this pattern: 2 - 这取决于您 - 您可以在工作表上使用价格绑定,就像我在 p.1 中提供的主题中一样,或者将代码中的每一行更改为这种模式:
With cmbPedigreeDog
.AddItem ("Boxer")
.column(1, cbx.ListCount - 1) = 0.86
.AddItem ("Chihuahua")
.column(1, cbx.ListCount - 1) = 1.83
' and so on
End With
3 - To retrieve the price of selected item use such code 3 - 要检索所选商品的价格,请使用此类代码
If cmbPedigreeDog.ListIndex >= 0 Then
price = cmbPedigreeDog.List(cbx.ListIndex, 1)
Else
MsgBox "item is not chosen"
End If
UPDATE 1更新 1
This answer is for VBA, not for Visual Basic WinForm application.此答案适用于 VBA,不适用于 Visual Basic WinForm 应用程序。 For this try to look for a solution like "Multi Column ComboBox"
为此,请尝试寻找“多列组合框”之类的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.