
¿Creen que sería mejor utilizar un list box en vez de un combo box? Y si uso un list box, ¿cómo le asigno el valor a cada artículo? La información del combo box está siendo extraída de un archivo de texto. Aquí está el código, espero que me puedan ayudar con este pequeño detalle:
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
End
End Sub
' Load the ListBox from a file.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Try
Dim file_name As String = DataFile()
Dim stream_reader As New IO.StreamReader(file_name)
Dim line As String
' Read the file one line at a time.
line = stream_reader.ReadLine()
Do While Not (line Is Nothing)
' Trim and make sure the line isn't blank.
line = line.Trim()
If line.Length > 0 Then _
cboChoice.Items.Add(line)
' Get the next line.
line = stream_reader.ReadLine()
Loop
cboChoice.SelectedIndex = 0
stream_reader.Close()
Catch exc As Exception
' Report all errors.
MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & _
"Error")
End Try
End Sub
' Return the data file name.
Private Function DataFile() As String
Dim file_name As String = Application.StartupPath
If file_name.EndsWith("\bin") Then file_name = _
file_name.Remove(file_name.Length - 4, 4)
file_name &= "\Office.txt"
Return file_name
End Function
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If cboChoice.SelectedIndex <> -1 And Val(txtQuantity.Text) > 0 Then
'Añadir la seleccion a la compra
lstProduct.Items.Add(cboChoice.SelectedItem)
lstPrice.Items.Add(FormatCurrency(99.99))
lstQty.Items.Add(txtQuantity.Text)
lstExtPrice.Items.Add(FormatCurrency(Val(txtQuanti ty.Text) * (99.99)))
lstTax.Items.Add(FormatCurrency(Val(txtQuantity.Te xt) * 99.99 * 0.04))
Call CalcTotal()
Else
MsgBox("Select Item from List!" & vbCrLf & "Quantity must be greater than 0.", MsgBoxStyle.Critical, "Error")
End If
End Sub
Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click
lstProduct.Items.Clear()
lstQty.Items.Clear()
lstPrice.Items.Clear()
lstExtPrice.Items.Clear()
lstTax.Items.Clear()
txtTotal.Text = ""
End Sub
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
Dim s As Integer = lstProduct.SelectedIndex
If lstProduct.SelectedIndex <> -1 Then
lstProduct.Items.RemoveAt(s)
lstQty.Items.RemoveAt(s)
lstPrice.Items.RemoveAt(s)
lstExtPrice.Items.RemoveAt(s)
lstTax.Items.RemoveAt(s)
Call CalcTotal()
Else
MsgBox("Select Item from Product List!", MsgBoxStyle.Critical, "Error")
End If
End Sub
Private Function CalcTotal() As Double
Dim i As Integer
Dim A As Double
Dim Total, Tax As Double
For i = 0 To lstExtPrice.Items.Count - 1
lstExtPrice.SetSelected(i, True)
'Elimina el caracter $ del String
A = lstExtPrice.SelectedItem.trim("$")
Total = Total + Val(A)
Next
Tax = Total * 0.04
txtTotal.Text = FormatCurrency(Total + Tax)
Return True
End Function
End Class