如何将所有数字加在一起,只在显示文本框中显示一行?

时间:2017-12-09 01:38:29

标签: vb.net visual-studio-2015

Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1

    Private strDeskSold(3) As String

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim intQtySold As Integer

        If rbtnEast.Checked Then
            strDeskSold(0) = "East"
        Else
            strDeskSold(0) = "West"
        End If

        If rbtnLarge.Checked Then
            strDeskSold(1) = "Large L Shape"
        ElseIf rbtnSmall.Checked Then
            strDeskSold(1) = "Small L Shape"
        ElseIf rbtnExec.Checked Then
            strDeskSold(1) = "Executive"
        Else
            strDeskSold(1) = "Presidential"
        End If

        If rbtnEast.Checked Then
            If lstBoxManagers.Text = "Anna Banes" Then
                strDeskSold(2) = "Anna Banes"
            ElseIf lstBoxManagers.Text = "Charlie Deer" Then
                strDeskSold(2) = "Charlie Deer"
            End If
        End If

        If rbtnWest.Checked Then
            If lstBoxManagers.Text = "Dan Easom" Then
                strDeskSold(2) = "Dan Easom"
            ElseIf lstBoxManagers.Text = "Fran Granger" Then
                strDeskSold(2) = "Fran Granger"
            ElseIf lstBoxManagers.Text = "Hamp Ideal" Then
                strDeskSold(2) = "Hamp Ideal"
            End If
        End If

        Integer.TryParse(txtNumberSold.Text, intQtySold)

        strDeskSold(3) = intQtySold.ToString
        txtDisplay.Text = txtDisplay.Text & strDeskSold(0) & ControlChars.Tab & strDeskSold(1) & ControlChars.Tab & strDeskSold(2) & ControlChars.Tab & strDeskSold(3) & ControlChars.NewLine
    End Sub

该程序允许用户选择一个区域,然后选择该区域的经理,然后选择桌面类型,然后选择订购的办公桌数量。如果在仅显示文本框中信息不同,我想在一行中显示所有信息,然后再按另一行显示。我不知道哪里可以开始添加订购的不同数量,以显示为每种不同的桌面类型订购的总办公桌。

1 个答案:

答案 0 :(得分:1)

如果我是对的,您希望为每个销售添加单独的行,除非同一个经理出售相同的商品。在这种情况下,您希望将最后一次销售数量添加到列表中的现有销售额中吗?

在这种情况下,你需要采取稍微不同的方式..

创建一个像这样的销售类

Private Class Sale
    Public Property Item As String
    Public Property Quantity As Integer
    Public Property ManagerName As String
End Class

这样的经理人课程。每位经理都有自己的销售清单。

Private Class Manager
    Public Property Name As String
    Public Property Region As String
    Public Property Sales As New List(Of Sale)
End Class

并创建经理列表

Dim Managers As New List(Of Manager)

此表单加载事件会将所有管理器添加到列表中。根据您存储经理的方式,您可以包含您希望的内容或编写自己的代码。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim manager1 As New Manager With {.Name = "Anna Barnes", .Region = "West"}
    Dim manager2 As New Manager With {.Name = "Charlie Deer", .Region = "West"}
    Dim manager3 As New Manager With {.Name = "Dan Easom", .Region = "East"}
    Dim manager4 As New Manager With {.Name = "Fran Granger", .Region = "East"}
    Dim manager5 As New Manager With {.Name = "Hamp Ideal", .Region = "East"}
    Managers = New List(Of Manager) From {manager1, manager2, manager3, manager4, manager5}
End Sub

当您更改东/西复选框选择

时,这两个事件会重新填充ListBox
Private Sub RbtnEast_CheckedChanged(sender As Object, e As EventArgs) Handles RbtnEast.CheckedChanged
    If RbtnEast.Checked Then
        lstBoxManagers.DataSource = Managers.FindAll(Function(x) x.Region = "East").ToArray
        lstBoxManagers.DisplayMember = "Name"
        lstBoxManagers.Update()
    End If
End Sub

Private Sub RbtnWest_CheckedChanged(sender As Object, e As EventArgs) Handles RbtnWest.CheckedChanged
    If RbtnWest.Checked Then
        lstBoxManagers.DataSource = Managers.FindAll(Function(x) x.Region = "West").ToArray
        lstBoxManagers.DisplayMember = "Name"
        lstBoxManagers.Update()
    End If
End Sub

当您点击添加按钮时,代码会创建一个包含所有详细信息的临时销售项目,以及除销售数量之外的所有内容是否与经理的先前销售相匹配。如果是这样,它会将该销售的数量添加到前一个。如果之前没有销售,它将被添加到经理的销售清单中。最后,更新richTextBox以显示每个经理的每个项目的总销售额。

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
    Dim tempSale As New Sale
    If rbtnLarge.Checked Then
        tempSale.Item = "Large L Shape"
    ElseIf rbtnSmall.Checked Then
        tempSale.Item = "Small L Shape"
    ElseIf rbtnExec.Checked Then
        tempSale.Item = "Executive"
    Else
        tempSale.Item = "Presidential"
    End If
    Integer.TryParse(TxtNumberSold.Text, tempSale.Quantity)
    Dim mgr As Manager = Managers.Find(Function(x) x.Name = CType(lstBoxManagers.SelectedItem, Manager).Name)
    'If Not IsNothing(mgr.Sales) Then
    Dim matchingSale As Sale = mgr.Sales.Find(Function(x) x.Item = tempSale.Item)
    If Not IsNothing(matchingSale) Then
        matchingSale.Quantity += tempSale.Quantity
    Else
        mgr.Sales.Add(tempSale)
    End If
    UpdateSalesTextBox()
End Sub

这是更新代码。它会遍历所有管理人员以及他们销售的每个独特商品的所有销售总额。

Private Sub UpdateSalesTextBox()
    rtbDisplay.SuspendLayout()
    rtbDisplay.Clear()
    For Each mgr As Manager In Managers
        For Each [sale] As Sale In mgr.Sales
            rtbDisplay.AppendText(mgr.Region & vbTab & [sale].Item & vbTab & mgr.Name & vbTab & [sale].Quantity & vbCrLf)
        Next
    Next
    rtbDisplay.ResumeLayout()
End Sub

如果答案能够解决您的问题,请不要忘记点击答案旁边的勾号 - 确保用户获得声誉积分,并让其他遇到相同问题的人更容易看到您的问题有一个可接受的答案,也可能适用于他们。 (并且回答这个问题的人获得声望点btw)