First of all, yes this is for an assignment. I need to know HOW to do this so that I can perform this operation in the future. I have two list boxes that I am sending the data selections to ONE text box. My problem comes along when I am ready to perform the calculation of randomly chosen (by random user) parts. There are 6 choices in one box and 5 in another.

I know there has to be a simple calculation to tell my program how to "grab" the information from the text box, but I don't know how to assign a VALUE to the listboxItem() selection within the text box.

If I could only assign the value somehow, I think I could discover a way to get the calculation. Should I setup an Array (though we weren't taught arrays in this particular class) or should I set the value and 'DIM' the variables as Integer instead of string when passing them to the text box?

My question may not be clear enough for anyone to answer because of my own frustration and confusion at this point but I will attempt to clarify if need be.

Thanks in advanced for anyone's attempt at answering my dilemma. :)

Dani AI

Generated

For : a reliable pattern is to keep the numeric data attached to the list items instead of trying to parse values out of a display string in the TextBox. That makes calculations deterministic and keeps the UI as a presentation layer only. was right to point you toward handling the transfer on a button click — run the calculation from that handler after you move or confirm the selections.

A simple, maintainable WinForms approach is to store small objects in the ListBox and override their display. Example class:

Public Class Part
    Public Property Name As String
    Public Property Price As Decimal

    Public Sub New(name As String, price As Decimal)
        Me.Name = name
        Me.Price = price
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class

Add parts to each list and then read the numeric Price from the selected items:

' populate
ListBox1.Items.Add(New Part("Filter", 8.5D))
ListBox2.Items.Add(New Part("Gasket", 3.25D))

' in the button click
Dim a As Part = TryCast(ListBox1.SelectedItem, Part)
Dim b As Part = TryCast(ListBox2.SelectedItem, Part)
Dim total As Decimal = 0D
If a IsNot Nothing Then total += a.Price
If b IsNot Nothing Then total += b.Price
TextBoxTotal.Text = total.ToString("F2")

If you prefer data binding (handy for larger lists), bind a List(Of Part) and set DisplayMember/ValueMember, then use SelectedValue for arithmetic. Quick troubleshooting: guard against no selection (SelectedItem may be Nothing), keep values as numeric types to avoid repeated parsing, and use TryCast/Convert.ToDecimal when reading SelectedValue. This keeps the code readable and makes future changes (adding more parts or prices) simple.

Recommended Answers

All 3 Replies

I don't have a compiler atm,

But it would look something like this.

textbox1.Text = ListBox1.SelectedItem

As for how you initiate the transfer from the listbox to the text box.

You have to ways, code it to a button or use the SelectedIndexChanged Event

That actually really helps. I am not quite "there" on my coding, but it gets me closer to the "starting" line. We are to use a button to activate the event. I just couldn't get a hint of where to get a general idea of calculating without writing lines and lines of hard coding for calculating these choices.

Thanks so very much for the nudge!!!

if you show me your work I would help you with the calculate

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.