:( hi people i nid help in calculating an item from a texfile that exists from menu_cmbselectedIndex,i want to display total number of items when i click a combobox and should also display total amount for those items,pls respond A.S.A.P

Dani AI

Generated

: the problem can be solved reliably, but the thread is missing the file format and whether "total amount" means (a) unit price * chosen quantity or (b) total value of all stock. asked for those details and showed a quick line-by-index split — that works for tiny, stable files but breaks if lines move or items are renamed.

A safer pattern:

  • Store one record per line with a stable key (ID or name) and explicit fields (CSV or simple tab-delimited).
  • Load the file once into a lookup (Dictionary) at startup.
  • Populate the ComboBox with the human name/ID.
  • On selection, look up the product, show its stock, and compute totals from Price*Quantity (either stock or a user-entered quantity).
  • Use Decimal for money and Decimal.TryParse with NumberStyles.Currency to accept optional currency symbols.

Example VB.NET loader + lookup (expects CSV: id,name,price,stock):

Imports System.Globalization

Public Class Product
    Public Property Id As String
    Public Property Name As String
    Public Property Price As Decimal
    Public Property Stock As Integer
End Class

Private products As New Dictionary(Of String, Product)()

Private Sub LoadProducts(path As String)
    For Each line In IO.File.ReadLines(path)
        If String.IsNullOrWhiteSpace(line) Then Continue For
        Dim p = line.Split(","c)
        If p.Length < 4 Then Continue For
        Dim id = p(0).Trim(), name = p(1).Trim()
        Dim price As Decimal, stock As Integer
        If Decimal.TryParse(p(2).Trim(), NumberStyles.Currency, CultureInfo.CurrentCulture, price) _
           AndAlso Integer.TryParse(p(3).Trim(), stock) Then
            products(name) = New Product With {.Id = id, .Name = name, .Price = price, .Stock = stock}
            ComboBox1.Items.Add(name)
        End If
    Next
End Sub

Private Sub ComboBox1_SelectedIndexChanged(...) Handles ComboBox1.SelectedIndexChanged
    Dim key = CStr(ComboBox1.SelectedItem)
    If key Is Nothing OrElse Not products.ContainsKey(key) Then Return
    Dim prod = products(key)
    Dim totalValue = prod.Price * prod.Stock   ' or * userQuantity
    LabelStock.Text = prod.Stock.ToString()
    LabelTotalValue.Text = totalValue.ToString("C")
End Sub

Troubleshooting notes: check for SelectedIndex = -1, handle missing/malformed file with Try/Catch, trim fields and tolerate currency symbols, and prefer Decimal over Double for money. If items must stay in original order, store an explicit index field instead of relying on ComboBox.SelectedIndex.

Recommended Answers

All 2 Replies

More detail please.

What is menu_cmbselectedIndex? A menu, a combobox, or a combobox.selectedIndex property? What does it hold and where is this textfile?

Give us some code that you've already attempted to put together.

Giving it a shot.

If your text file displays as the following:

price: $10/total items in stock: 100
price: $20/total items in stock: 200
price: $30/total items in stock: 300
price: $40/total items in stock: 400
price: $50/total items in stock: 500

The following code will read a line from the file depending on which index is selected in the "Combobox".

Public Class Form1

    Private myFile() As String = IO.File.ReadAllLines("C:\test.txt") '// load file into an array.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ComboBox1.Items
            .Add("Item 1") : .Add("Item 2") : .Add("Item 3") : .Add("Item 4") : .Add("Item 5")
        End With
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim txtLine As Integer = ComboBox1.SelectedIndex '// get index for which line to read.
        Dim myArray() As String = myFile(txtLine).Split("/") '// split line into strings.
        MsgBox(myArray(0), MsgBoxStyle.Information) '// display first string in myArray.
        MsgBox(myArray(1), MsgBoxStyle.Information) '// display second string in myArray.
    End Sub
End Class

As the_carpenter stated, more details will be beneficial for those replying and for you, the original poster.

Otherwise, I hope the above project sample helps.

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.