[英]How to add the value of items (representing numbers) of two ListBoxes
I'm trying to add two integers ( X
and Y
) from different ListBoxes and show the total in a Label or TextBox.我正在尝试从不同的 ListBoxes 添加两个整数(
X
和Y
),并在 Label 或 TextBox 中显示总数。
I've tried copying the code from answered questions and emending to suit my code, but still struggling.我已经尝试从已回答的问题中复制代码并进行修改以适合我的代码,但仍在苦苦挣扎。
Dim lblTotal As Integer
Dim X, Y As Integer
Const WasGo = 35
Const Vacuum = 20
' The item description from the ListBox are supposed to be Constants
X = extBox.SelectItems(0)
Y = intBox.SelectItems(0)
lblTotal = X + Y
ListBox has a SelectedItems property. ListBox 有一个 SelectedItems 属性。 Assuming you have exactly one selected from each, this should work:
假设您从每个中选择了一个,这应该有效:
X = Convert.ToInt32(extBox.SelectedItems(0).ToString())
Y = Convert.ToInt32(intBox.SelectedItems(0).ToString())
lblTotal = X + Y
Few other tips/questions.很少有其他提示/问题。
You don't use those constants in this code.您不在此代码中使用这些常量。 Convert the items to Integers so you can perform addition.
将项目转换为整数,以便您可以执行加法。 To display in a label's text property call
.ToString
on the Integer要在标签的文本属性中显示,请在 Integer 上调用
.ToString
Private Sub OPCode()
Dim lblTotal As Integer
lblTotal = CInt(extBox.SelectItems(0)) + CInt(intBox.SelectItems(0))
Label1.Text = lblTotal.ToString
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.