Ayuda para suma de valores de un listbox

Quisiera saber si alguien me puede ayudar, estoy haciendo un formulario para una factura en visual studio 2008, tengo un problema he buscado como sumar los valores que voy agregando a un listbox y que me los vaya mostrando en una etiqueta, pero no he podido encontrar solución, espero que alguien me pueda ayudar. Gracias. Javier rosas (xxxxxx)

1 Respuesta

Respuesta
1
Lo puede hacer de dos maneras :
1 - Cada vez que adiona un item al Listbox en ese momento lo suma a un acumulador de valor y muestra ese valor en la etiqueta.
2 - Una vez que inserta todos los valores en el Listbox entonces los suma :
Dim Total as Currency
Dim I as integer
Private Sub Sumar_Valores()
     Total=0
     For I = 0 to Textbox1.Listcount
                 Total = Total+Valor
     Next I
     Label1=Format(Total, "###,###,###,##0.00")
End Sub
Suerte !
Antes que nada muchas gracias por tomarte la molestia en contestarme, pero mira el código que tengo más o menos esta así: (¿los renglones que están entre asteriscos es el código que me pasaste pero me en lugar de ese textbox1 que pongo? Ya tengo el formato de mi factura en visual studio 2008 y casi todo me funciona bien, no se si te lo pueda mandar a algún correo para que lo pudieras checar y de todas maneras pues por esta página de todoexpertos te sigo preguntando, bueno espero me puedas ayudar. Muchas gracias.
Public Class frmfactura
Dim indexproducto As Integer
Dim a¤adir As Boolean = True
' Sub SumarTotales()
*******************************************************
Private Sub Sumartotales()
Dim Total As Double
Dim I As Integer
Dim valor As Double
Total = 0
For I = 0 To textbox1.Listcount
Total = Total + valor
Next I
Me.lblsubtotal.Text = Format(Total, "###,##0.00")
End Sub
****************************************************** 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.cboproducto.Items.Add("32012 Pemex Premium")
Me.cboproducto.Items.Add("32011 Pemex Magna")
Me.cboproducto.Items.Add("34006 Pemex Diesel")
'Call Limpiar()
Me.lblfecha.Text = Format(Now, "dd - MMMM - yyyy")
'Me.lblhora.Text = Format(Now, "hh:mm:ss tt")
Me.Text = Space(100) + "Facturaci¢n"
End Sub
Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
Dim Index As Integer
Index = Me.lstlitros.SelectedIndex
Me.lstlitros.Items.RemoveAt(Index)
Me.lstdescripcion.Items.RemoveAt(Index)
Me.lstpunitario.Items.RemoveAt(Index)
Me.lstimporte.Items.RemoveAt(Index)
Me.btneliminar.Enabled = True
'Call SumarTotales()
End Sub
Private Sub lstlitros_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstlitros.SelectedIndexChanged
Dim Index As Integer
Index = Me.lstlitros.SelectedIndex
Me.lstdescripcion.SelectedIndex = Index
Me.lstpunitario.SelectedIndex = Index
Me.lstimporte.SelectedIndex = Index
Me.btneliminar.Enabled = True
End Sub
Private Sub lstdescripcion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstdescripcion.SelectedIndexChanged
Dim Index As Integer
Index = Me.lstdescripcion.SelectedIndex
Me.lstlitros.SelectedIndex = Index
Me.lstpunitario.SelectedIndex = Index
Me.lstimporte.SelectedIndex = Index
Me.btneliminar.Enabled = True
End Sub
Private Sub lstpunitario_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstpunitario.SelectedIndexChanged
Dim Index As Integer
Index = Me.lstpunitario.SelectedIndex
Me.lstdescripcion.SelectedIndex = Index
Me.lstlitros.SelectedIndex = Index
Me.lstimporte.SelectedIndex = Index
Me.btneliminar.Enabled = True
End Sub
Private Sub lstimporte_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstimporte.SelectedIndexChanged
Dim Index As Integer
Index = Me.lstimporte.SelectedIndex
Me.lstdescripcion.SelectedIndex = Index
Me.lstpunitario.SelectedIndex = Index
Me.lstlitros.SelectedIndex = Index
Me.btneliminar.Enabled = True
End Sub
Private Sub cboproducto_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboproducto.SelectedIndexChanged
Dim preciosiniva, preciou As Double
Dim total, SubTotal, iva As Double
Dim litros, cantidad As Double
Dim importe As Double
indexproducto = cboproducto.SelectedIndex
Select Case indexproducto
Case 0
lblprecio.Text = "9.58"
preciosiniva = 8.38453913
litros = cantidad / preciou
litros = (Val(Me.txtcant.Text) / Val(Me.lblprecio.Text))
SubTotal = (Val(Me.lbllitro.Text) * Val(Me.lblpreciosiniva.Text))
SubTotal = litros * preciosiniva
'importe = litros * preciosiniva
'importe = (Val(Me.lbllitro.Text) * Val(Me.lblpreciosiniva.Text))
total = cantidad + 0
total = (Val(Me.txtcant.Text) + 0)
iva = (Val(Me.lbltotal.Text) - Val(Me.lblsubtotal.Text))
iva = total - SubTotal
Me.lblsubtotal.Text = Format(SubTotal, "###,##0.00")
Me.lbliva.Text = Format(iva, "###,##0.00")
Me.lbltotal.Text = Format(total, "###,##0.00")
Me.lbllitro.Text = Format(litros, "###,#####0.00000")
Me.lblpreciosiniva.Text = Format(preciosiniva, "###,########0.00000000")
Me.lblimporte.Text = Format(importe, "###,##0.00")
Case 1
lblprecio.Text = "7.72"
preciosiniva = 6.7573913
litros = cantidad / preciou
litros = (Val(Me.txtcant.Text) / Val(Me.lblprecio.Text))
SubTotal = (Val(Me.lbllitro.Text) * Val(Me.lblpreciosiniva.Text))
SubTotal = litros * preciosiniva
' importe = litros * preciosiniva
' importe = (Val(Me.lbllitro.Text) * Val(Me.lblpreciosiniva.Text))
total = cantidad + 0
total = (Val(Me.txtcant.Text) + 0)
iva = (Val(Me.lbltotal.Text) - Val(Me.lblsubtotal.Text))
iva = total - SubTotal
Me.lblsubtotal.Text = Format(SubTotal, "###,##0.00")
Me.lbliva.Text = Format(iva, "###,##0.00")
Me.lbltotal.Text = Format(total, "###,##0.00")
Me.lbllitro.Text = Format(litros, "###,#####0.00000")
Me.lblpreciosiniva.Text = Format(preciosiniva, "###,########0.00000000")
Me.lblimporte.Text = Format(importe, "###,##0.00")
Case 2
lblprecio.Text = "7.78"
preciosiniva = 6.8041913
litros = cantidad / preciou
litros = (Val(Me.txtcant.Text) / Val(Me.lblprecio.Text))
SubTotal = (Val(Me.lbllitro.Text) * Val(Me.lblpreciosiniva.Text))
SubTotal = litros * preciosiniva
'importe = litros * preciosiniva
'importe = (Val(Me.lbllitro.Text) * Val(Me.lblpreciosiniva.Text))
total = cantidad + 0
total = (Val(Me.txtcant.Text) + 0)
iva = (Val(Me.lbltotal.Text) - Val(Me.lblsubtotal.Text))
iva = total - SubTotal
Me.lblsubtotal.Text = Format(SubTotal, "###,##0.00")
Me.lbliva.Text = Format(iva, "###,##0.00")
Me.lbltotal.Text = Format(total, "###,##0.00")
Me.lbllitro.Text = Format(litros, "###,#####0.00000")
Me.lblpreciosiniva.Text = Format(preciosiniva, "###,########0.00000000")
Me.lblimporte.Text = Format(importe, "###,##0.00")
Call SumarTotales()
End Select
End Sub
Private Sub btnagregar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnagregar.Click
Dim Descripcion As String
If Me.cboproducto.Text <> "" AndAlso Me.cboproducto.Text <> "" Then
Descripcion = Me.cboproducto.Text + " \ " + Me.cboproducto.Text
If a¤adir = False Then
If Me.cboproducto.Text <> "" Then
Descripcion = Descripcion + " \ " + Me.cboproducto.Text
End If
End If
Me.lstlitros.Items.Add(CStr(Me.lbllitro.Text))
Me.lstdescripcion.Items.Add(Me.cboproducto.Text)
Me.lstpunitario.Items.Add(Me.lblpreciosiniva.Text)
Me.lstimporte.Items.Add(Me.lblsubtotal.Text)
'Call SumarTotales()
Else
MsgBox("Seleccione el producto para facturar", MsgBoxStyle.Information, "Error")
End If
End Sub
Private Sub btnsalir_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsalir.Click
Me.Close()
End Sub
Private Sub btnnuevo_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnuevo.Click
Me.txtcant.Text = ""
Me.lbllitro.Text = ""
Me.lblpreciosiniva.Text = ""
Me.lblprecio.Text = ""
Me.lbliva.Text = ""
Me.lblsubtotal.Text = ""
Me.lbltotal.Text = ""
Me.lblimporte.Text = ""
Me.cboproducto.Text = ""
Me.lstlitros.Items.Clear()
Me.lstdescripcion.Items.Clear()
Me.lstimporte.Items.Clear()
Me.lstpunitario.Items.Clear()
End Sub
End Class
En el ejemplo que envié debe reemplazar estos nombres :
Textbox1 por el nombre de su Listbox
Valor por el nombre de la columna valor en su Listbox
Total debe cambiarlo por otro nombre pues ya existe un campo con dicho nombre donde usted maneja la cantidad.
Suerte !
Hola otra vez, vas a decir que soy muy tonto, porque no entiendo muy bien, mi código quedaría así :
Private Sub Sumar_Valores()
Dim Total As Currency
Dim I As Integer
Total = 0
For I = 0 To lstimporte.Listcount
Total = Total + lstimporte
Next I
lblsubtotal = Format(Total, "###,###,###,##0.00")
End Sub
Pero me marca dos errores:
1.- Ya no se admite el tipo currency utilice el tipo decimal en su lugar
2.- listcount no es un miembro de system.windows.forms. Listbox
No se porque me manda esos errores. Ojala me puedas seguir ayudando
Gracias
Creo que tenemos un pequeño inconveniente pues yo trabajo con Visual Basic 6.0 y usted lo hace con Visual Studio 2008.
Efectivamente en mi rutina hace falta el índice así que debemos corregir esto :
Private Sub Sumar_Valores()
      Dim Total As Decimal
      Dim I As Integer
      Gtotal = 0
      For I = 0 To 2      'Si se facturan los tres productos 
      Gtotal = Gtotal+ lstimporte(i)
      Next I
      Lblgtotal = Format(Gtotal, "###,###,###,##0.00")
End Sub
En la línea que tiene el comentario he cambiado Lstimporte. Listcount (ya que lo marca como error) por 2 para el caso que se facturen los tres productos, 1 para dos productos y 0 para un solo producto, si funciona con alguno de estos tres valores entonces creo que es que el Listbox no se llama Lstimporte. Me parece que Lstimporte es el nombre del campo del Listbox.
Suerte !
Hola, voy a hacerlo como me dijo haber como me queda, le agradezco mucho su ayuda. Dejo mi correo para cualquier cosa, (xxxxxx) mil gracias.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas