Hello Everyone,

Can someone please offer me some help here as I have spent 3 days trying to resolve this problem
I am having trouble while selecting one item from multiple items in combo box and calculate data of that item from list box to show in text box in vb.net

Data.jpg

I have list box in which this whole data is displayed as above.
and below that i have combo box for BRAND and text boxes for (Units sell in UK), (Units sell in USA), and (Total).

I want to select any brand from combo box and set an event for calculate button which can calculate (Total Units sell in UK), (Total Units sell in USA) and (Grand Total) and show them in their respective separate text boxes.

I am trying hard on it from last 3 days but unable to get it solved

The major problem is that i want to do the coding with basics of array, loop, integer, variable and double etc.

I tried to solve it with string and cstr but i was not allowed to do that.

I am greatly thankful if someone can help me out.

As a start you may do something like the following:

Imports System.Text

Public Class multiColumnListbox
    Private Sub multiColumnListbox_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add(New car("X1", "BMW", "140", "276", 1))
        ListBox1.Items.Add(New car("XS", "BMW", "225", "375", 2))
        '...
        '...
        ListBox1.Items.Add(New car("718", "Porsche", "88", "112", 2))
    End Sub
End Class
Public Class car
    Dim ID As Int32
    Dim model, brand As String
    Dim unitsUK, unitsUSA As Int32
    Public Sub New(model As String, brand As String, unitsUK As Int32, unitsUSA As Int32, ID As Int32)
        Me.model = model
        Me.brand = brand
        Me.unitsUK = unitsUK
        Me.unitsUSA = unitsUSA
        Me.ID = ID
    End Sub
    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder(String.Format("{0,-15} {1,-15}", model, brand))
        sb.Append(String.Format("{0,5} {1,5} {2,10}", unitsUK, unitsUSA, unitsUK + unitsUSA))
        Return sb.ToString
    End Function
End Class

multiColumnListBox.PNG

Be sure to set ListBox1.MultiColumn property to 'True'.

Thank you so much for your kind help @xrj

i was not allowed to use string and cstr in coding
Can you suggest me coding with basics of array, loop, integer, variable and double etc.

Thanks

And also i have already done coding to show all data in list box.

I just only want to select brand from (BrandComboBox),
and then by (CalculateButton), i wanna calculate data for e.g (Total Units sell in UK), (Total Units sell in USA) and (Grand Total) and show them in separate text boxes namely (UKTextBox, USATextBox, TotalTextBox)

To examplify- When i select Audi from BrandComboBox and press CalculateButton, then it should calculate (Total Units sell in UK), (Total Units sell in USA) and (Grand Total) for all models of Audi as following

Total Units sell in UK = 234+197+236+119 = 786
Total Units sell in USA = 229+251+289+264 = 1033
Grand Total = 463+448+525+383 = 1819

and show this calculated data in separate text boxes namely (UKTextBox, USATextBox, TotalTextBox) as following

UKTextBox 786
USATextBox 1033
TotalTextBox 1819

Setting the form's font to 'Courier New' is best because the letter width is fixed, this is the same for all characters.

Imports System.Text

Public Class multiColumnListbox
    Dim vCars() As car = {New car("X1", "BMW", "140", "276", 1), _
                          New car("XS", "BMW", "225", "375", 2), _
                          New car("XSR", "BMW", "415", "189", 3), _
                          New car("X7", "BMW", "178", "119", 4), _
                          New car("718", "Porsche", "88", "112", 5), _
                          New car("911", "Porsche", "75", "137", 6), _
                          New car("Carrera", "Porsche", "127", "219", 7), _
                          New car("A4", "Audi", "234", "229", 8), _
                          New car("Q3", "Audi", "197", "251", 9), _
                          New car("Q5", "Audi", "236", "289", 10), _
                          New car("Q7", "Audi", "119", "264", 11) _
                         }
    Private Sub multiColumnListbox_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim lastBrand As New StringBuilder(20)
        For i = 0 To vCars.Length - 1
            ListBox1.Items.Add(vCars(i))
            If lastBrand.ToString <> vCars(i).getBrand.ToString Then
                lastBrand = vCars(i).getBrand
                ComboBox1.Items.Add(lastBrand.ToString)
            End If
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim unitsUK, unitsUSA, total As Int32
        For i As Int32 = 0 To vCars.Length - 1
            If ComboBox1.SelectedItem.ToString = vCars(i).getBrand.ToString Then
                unitsUK += vCars(i).getUnitsUK
                unitsUSA += vCars(i).getUnitsUSA
                total += vCars(i).getUnitsUK + vCars(i).getUnitsUSA
            End If
        Next
        tbUnitsUK.Text = unitsUK.ToString
        tbUnitsUSA.Text = unitsUSA.ToString
        tbTotal.Text = total.ToString
    End Sub

End Class
Public Class car
    Dim ID As Int32
    Dim model, brand As StringBuilder
    Dim unitsUK, unitsUSA As Int32
    Public Sub New(model As String, brand As String, unitsUK As Int32, unitsUSA As Int32, ID As Int32)
        Me.model = New StringBuilder(model)
        Me.brand = New StringBuilder(brand)
        Me.unitsUK = unitsUK
        Me.unitsUSA = unitsUSA
        Me.ID = ID
    End Sub
    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder(String.Format("{0,-15} {1,-15}", model.ToString, brand.ToString))
        sb.Append(String.Format("{0,5} {1,5} {2,10}", unitsUK, unitsUSA, unitsUK + unitsUSA))
        Return sb.ToString
    End Function
    Public ReadOnly Property getBrand As StringBuilder
        Get
            Return brand
        End Get
    End Property
    Public ReadOnly Property getUnitsUK As Int32
        Get
            Return unitsUK
        End Get
    End Property
    Public ReadOnly Property getUnitsUSA As Int32
        Get
            Return unitsUSA
        End Get
    End Property

End Class

multiColumnListBox2.PNG

Note almost all the strings have been replaced by StringBuilder.

Another good election for Font Type is 'Lucida Sans Typewriter', also monospace/fixed witdth. Like here:

multiColumnListBox3.PNG

Thanks for the help @xrj

Private Sub LoadButton_Click(sender As Object, e As EventArgs) Handles LoadButton.Click

    If RawDataListBox.Items.Count = 0 Then
        MessageBox.Show("Please import data first!")
        Exit Sub
    End If
    'split the data and store in a structure
    Try
        Dim counterInteger As Integer = 11
        Dim ModelString As String
        Dim BrandString As String
        Dim UKCountInteger As Integer
        Dim USACountInteger As Integer
        Dim TotalCountInteger As Integer

        FinalDataListBox.Items.Add("Model" & vbTab & "Brand" & vbTab & "UK" & vbTab & "USA" & vbTab &
                                   "Total")
        While counterInteger < RawDataListBox.Items.Count
            'read one line/row into a string
            Dim DataSingleLineString As String = RawDataListBox.Items(counterInteger).ToString
            'Split string by comma
            Dim DataSplitArrayString As String() = DataSingleLineString.Split(",")
            'assign a value to each of the structure data elements from the array
            ModelString = DataSplitArrayString(0).Replace("""", "")
            BrandString = DataSplitArrayString(1).Replace("""", "")
            UKCountInteger = DataSplitArrayString(2).Replace("""", "")
            USACountInteger = DataSplitArrayString(5).Replace("""", "")
            TotalCountInteger = DataSplitArrayString(6).Replace("""", "")

            FinalDataListBox.Items.Add(ModelString & vbTab & BrandString & vbTab & UKCountInteger & vbTab & USACountInteger & vbTab & TotalCountInteger)

            counterInteger += 1
        End While
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

@xrj, i have done the above coding to load data in list box.

Imports System.Text
Public Class multiColumnListbox
    Dim vCars() As car = {New car("X1", "BMW", "140", "276", 1), _
                          New car("XS", "BMW", "225", "375", 2), _
                          New car("XSR", "BMW", "415", "189", 3), _
                          New car("X7", "BMW", "178", "119", 4), _
                          New car("718", "Porsche", "88", "112", 5), _
                          New car("911", "Porsche", "75", "137", 6), _
                          New car("Carrera", "Porsche", "127", "219", 7), _
                          New car("A4", "Audi", "234", "229", 8), _
                          New car("Q3", "Audi", "197", "251", 9), _
                          New car("Q5", "Audi", "236", "289", 10), _
                          New car("Q7", "Audi", "119", "264", 11) _
                         }
    Private Sub multiColumnListbox_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim lastBrand As New StringBuilder(20)
        For i = 0 To vCars.Length - 1
            ListBox1.Items.Add(vCars(i))
            If lastBrand.ToString <> vCars(i).getBrand.ToString Then
                lastBrand = vCars(i).getBrand
                ComboBox1.Items.Add(lastBrand.ToString)
            End If
        Next
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim unitsUK, unitsUSA, total As Int32
        For i As Int32 = 0 To vCars.Length - 1
            If ComboBox1.SelectedItem.ToString = vCars(i).getBrand.ToString Then
                unitsUK += vCars(i).getUnitsUK
                unitsUSA += vCars(i).getUnitsUSA
                total += vCars(i).getUnitsUK + vCars(i).getUnitsUSA
            End If
        Next
        tbUnitsUK.Text = unitsUK.ToString
        tbUnitsUSA.Text = unitsUSA.ToString
        tbTotal.Text = total.ToString
    End Sub
End Class
Public Class car
    Dim ID As Int32
    Dim model, brand As StringBuilder
    Dim unitsUK, unitsUSA As Int32
    Public Sub New(model As String, brand As String, unitsUK As Int32, unitsUSA As Int32, ID As Int32)
        Me.model = New StringBuilder(model)
        Me.brand = New StringBuilder(brand)
        Me.unitsUK = unitsUK
        Me.unitsUSA = unitsUSA
        Me.ID = ID
    End Sub
    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder(String.Format("{0,-15} {1,-15}", model.ToString, brand.ToString))
        sb.Append(String.Format("{0,5} {1,5} {2,10}", unitsUK, unitsUSA, unitsUK + unitsUSA))
        Return sb.ToString
    End Function
    Public ReadOnly Property getBrand As StringBuilder
        Get
            Return brand
        End Get
    End Property
    Public ReadOnly Property getUnitsUK As Int32
        Get
            Return unitsUK
        End Get
    End Property
    Public ReadOnly Property getUnitsUSA As Int32
        Get
            Return unitsUSA
        End Get
    End Property
End Class

Can u please convert this coding in the above coding style,

I mean without int32 and if possible using while loop

I am sorry, I can do the work in my own style but you may adopt what you please of the former, of course.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.