question

MatthewTurcotte-2973 avatar image
0 Votes"
MatthewTurcotte-2973 asked karenpayneoregon answered

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

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.

dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, only can assist by seeing your code, if you have a GitHub repository provide the link and if not post your code using the code block.
79853-codeblock.png


0 Votes 0 ·
codeblock.png (8.1 KiB)
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered XingyuZhao-MSFT edited

Hi @MatthewTurcotte-2973 ,
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.



gif.gif (122.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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



college.png (9.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.