How to assign a price to a string in a list box

Matthew Turcotte 1 Reputation point
2021-03-19T16:35:40.797+00:00

Doing a college tuition calculator and have a list box with 3 colleges already loaded I have a combo box with the credit hours loaded I have a button to calculate and a label to display the results. I am stuck on how to assign the 3 colleges in the list box a price per credit hour so that I can calculate after choosing a college and how many credit hours I want to get a price of the tuition. I am guessing I need to assign a price to each index in the list box but do not know how to set that up. Any advice would be appreciated.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-03-22T02:23:14.42+00:00

    Hi @Matthew Turcotte ,
    You can store Key-Value pair in listbox.
    Check the following example.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            ComboBox1.Items.Add(100)  
            ComboBox1.Items.Add(150)  
            ComboBox1.Items.Add(200)  
      
            Dim dic As Dictionary(Of String, Double) = New Dictionary(Of String, Double)()  
            dic.Add("College A", 4) '4 per credit hour'  
            dic.Add("College B", 4.5)  
            dic.Add("College C", 5)  
            ListBox1.DataSource = New BindingSource(dic, Nothing)  
            ListBox1.DisplayMember = "Key"  
            ListBox1.ValueMember = "Value"  
        End Sub  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            If ComboBox1.SelectedItem IsNot Nothing Then  
                Label1.Text = CDbl(ListBox1.SelectedValue) * CDbl(ComboBox1.SelectedItem)  
            End If  
        End Sub  
    

    Result of my test.
    79981-gif.gif
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Karen Payne MVP 35,191 Reputation points
    2021-03-22T14:11:02.717+00:00

    Here is an example that calculates without a button.
    80189-college.png

    Full source

    College class

    Public Class College  
        Public Property Id() As Integer  
        Public Property Name() As String  
        Public Property Credit() As Double  
      
        Public Overrides Function ToString() As String  
            Return Name  
        End Function  
    End Class  
    

    Class for mocking up schools

    Public Class Mocked  
        ''' <summary>  
        ''' Should come from a database or file  
        ''' </summary>  
        ''' <returns></returns>  
        Public Shared Function Colleges() As List(Of College)  
            Return New List(Of College) From {  
                    New College() With {.Id = 1, .Name = "University Of Phoenix", .Credit = 10.5},  
                    New College() With {.Id = 2, .Name = "Bryn Mawr College", .Credit = 20.5},  
                    New College() With {.Id = 3, .Name = "Grinnell College", .Credit = 30.5},  
                    New College() With {.Id = 4, .Name = "Capella University", .Credit = 40.5},  
                    New College() With {.Id = 5, .Name = "Strayer University", .Credit = 60.5}  
                }  
        End Function  
    End Class  
    

    Form code

    Public Class Form1  
        Private ReadOnly _collegeBindingSource As New BindingSource  
        Private ReadOnly _hoursBindingSource As New BindingSource  
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            _collegeBindingSource.DataSource = Mocked.Colleges  
            SchoolListBox.DataSource = _collegeBindingSource  
      
            AddHandler _collegeBindingSource.PositionChanged, AddressOf _  
                BindingSources_PositionChanged  
      
            AddHandler _hoursBindingSource.PositionChanged, AddressOf _  
                BindingSources_PositionChanged  
      
            _hoursBindingSource.DataSource = Enumerable.  
                Range(100, 10).  
                Select(Function(item) (item * 2).ToString()).  
                ToList()  
      
            HoursComboBox.DataSource = _hoursBindingSource  
      
        End Sub  
      
        Private Sub BindingSources_PositionChanged(sender As Object, e As EventArgs)  
            Dim currentCollege = CType(_collegeBindingSource.Current, College)  
            CostLabel.Text =  
                $"Cost: {(currentCollege.Credit * CDbl(_hoursBindingSource.Current)).ToString("C")}"  
        End Sub  
    End Class  
    
    0 comments No comments