简体   繁体   English

如何使用组合框中的价格执行总计计算?

[英]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.

相关问题 如何撤消对 Access 中组合框的更改? - How do I undo a change to a combo box in Access? 如何从 Excel 电子表格中的列填充组合框? - How do I populate a combo box from a column in my excel spread sheet? 如果没有为以下代码选择我的 me.combo 框,如何添加错误消息? - How do I add Error message if my me.combo box is not selected for the following code? 如何通过VBA编程在组合框中启用“自动完成”选项? - How do I Enable Auto complete option in combo box by programming in VBA? 如何允许通过 Microsoft Access 中的组合框向表中添加值? - How do I allow the addition of a value to a table through a combo box in Microsoft Access? Excel VBA用户窗体-如何从用户窗体中的列表向组合框添加值? - Excel VBA Userform - How do I add a value to a combo box from a list in the userform? 如何使用单个组合框元素引用来自不同工作表的单元格? - How do I reference cells from different sheets with single combo box element? 如何在“访问”表单的组合框中仅显示可用选项? - How do I show only the available options in a combo box on an Access form? 在 Access VBA 中,如何从表的多个列中获取 DISTINCT 值作为组合框的行源? - In Access VBA, how do I get DISTINCT values from multiple columns of a table to be the row source of a combo box? 如何使用组合框列表中的值过滤联合查询中的记录? - How do you filter records from a union query using values from a combo box list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM