ERRO NO LISTBOX
to colocando pra adicionar com um botao no listbox com o seguinte código no form comanda
Imports MySql.Data.MySqlClient
Imports System.Text
Public Class frmComanda
Dim index_cardapio As Decimal
Dim index_remove, index_remove2 As Integer
Dim total, totalgeral, totalremove As Double
Private Sub frmComanda_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Selecionacardapio()
btnmais.Enabled = True
End Sub
[ô]seleciona cardapio[ô]
Sub Selecionacardapio()
Dim strCon As String
strCon = [Ô]server=localhost ; user=root ; password=123456789a ; database=bardotorresmo [Ô]
Dim conn As New MySqlConnection
Dim comando As New MySqlCommand
Try
conn.ConnectionString = strCon
conn.Open()
comando.Connection = conn
comando.CommandText = [Ô]SELECT nomecardapio as cardapios FROM cardapio[Ô]
Dim dr As MySqlDataReader = comando.ExecuteReader
Dim str As String = dr.ToString
While (dr.Read())
cbxcardapiocomanda.Items.Add(dr([Ô]cardapios[Ô]))
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show([Ô]Erro ao efetuar a conexão com a base de dados : [Ô] + ex.Message)
conn.Dispose()
End Try
End Sub
[ô]tacando o preço na label[ô]
Private Sub cbxcardapiocomanda_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxcardapiocomanda.SelectedIndexChanged
Dim strCon As String
strCon = [Ô]server=localhost ; user=root ; password=123456789a ; database=bardotorresmo [Ô]
Dim conn As New MySqlConnection
Dim comando, comando2 As New MySqlCommand
Try
conn.ConnectionString = strCon
conn.Open()
comando.Connection = conn
comando.CommandText = [Ô]select idcardapio, nomecardapio, preco from cardapio where nomecardapio like UPPER([ô][Ô] & cbxcardapiocomanda.SelectedItem & [Ô][ô])[Ô]
Dim dr As MySqlDataReader = comando.ExecuteReader
Dim str As String = dr.ToString
While (dr.Read())
lbl_preco_unit.Text = (dr([Ô]preco[Ô]))
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show([Ô]Erro ao efetuar a conexão com a base de dados : [Ô] + ex.Message)
conn.Dispose()
End Try
End Sub
Private Sub btnmais_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmais.Click
If cbxcardapiocomanda.SelectedItem = [Ô][Ô] Then
MsgBox([Ô]Selecione o cardapio para adicionar a comanda[Ô])
Else
[ô] BY GIOVANNI
index_cardapio = cbxcardapiocomanda.SelectedIndex + 1
If txtquantidade.Text <> Nothing Then
total = lbl_preco_unit.Text * Val(txtquantidade.Text)
Else
txtquantidade.Text = 1
total = lbl_preco_unit.Text * 1
End If
For i As Integer = 1 To Val(txtquantidade.Text)
lbxcardapios.Items.Add(cbxcardapiocomanda.SelectedItem & [Ô]-[Ô] & lbl_preco_unit.Text)
Next
totalgeral = totalgeral + total
TxtValor.Text = totalgeral.ToString([Ô]c[Ô])
txtquantidade.Text = Nothing
End If
End Sub
Private Sub btnretirar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnretirar.Click
If lbxcardapios.Text = [Ô][Ô] Then
MsgBox([Ô]não é possivel deletar quando não se tem produtos selecionados na comanda[Ô])
Else
lbxcardapios.Items.RemoveAt(Val(index_remove2))
Dim strCon As String
strCon = [Ô]server=localhost ; user=root ; password=123456789a ; database=bardotorresmo [Ô]
Dim conn As New MySqlConnection
Dim comando, comando2 As New MySqlCommand
Try
conn.ConnectionString = strCon
conn.Open()
comando.Connection = conn
comando.CommandText = [Ô]select idcardapio, nomecardapio, preco from cardapio where idcardapio like UPPER([ô][Ô] & index_cardapio & [Ô][ô])[Ô]
Dim dr As MySqlDataReader = comando.ExecuteReader
Dim str As String = dr.ToString
While (dr.Read())
totalremove = (dr([Ô]preco[Ô]))
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show([Ô]Erro ao efetuar a conexão com a base de dados : [Ô] + ex.Message)
conn.Dispose()
End Try
TxtValor.Text = Val(TxtValor.Text) - totalremove
totalgeral = Val(TxtValor.Text)
TxtValor.Text = totalgeral
End If
End Sub
Private Sub lbxcardapios_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxcardapios.SelectedIndexChanged
index_remove = lbxcardapios.SelectedIndex + 1
End Sub
Private Sub lbxcardapios_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbxcardapios.MouseClick
index_remove = lbxcardapios.SelectedIndex + 1
End Sub
[txt-size=1]porém quando eu clico no botão de excluir ele exclui da listbox e zera o valor mas se tiver mais de 2 itens na listbox ele exclui e mesmo assim zera o valor da texttotal[/txt-size]
Imports MySql.Data.MySqlClient
Imports System.Text
Public Class frmComanda
Dim index_cardapio As Decimal
Dim index_remove, index_remove2 As Integer
Dim total, totalgeral, totalremove As Double
Private Sub frmComanda_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Selecionacardapio()
btnmais.Enabled = True
End Sub
[ô]seleciona cardapio[ô]
Sub Selecionacardapio()
Dim strCon As String
strCon = [Ô]server=localhost ; user=root ; password=123456789a ; database=bardotorresmo [Ô]
Dim conn As New MySqlConnection
Dim comando As New MySqlCommand
Try
conn.ConnectionString = strCon
conn.Open()
comando.Connection = conn
comando.CommandText = [Ô]SELECT nomecardapio as cardapios FROM cardapio[Ô]
Dim dr As MySqlDataReader = comando.ExecuteReader
Dim str As String = dr.ToString
While (dr.Read())
cbxcardapiocomanda.Items.Add(dr([Ô]cardapios[Ô]))
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show([Ô]Erro ao efetuar a conexão com a base de dados : [Ô] + ex.Message)
conn.Dispose()
End Try
End Sub
[ô]tacando o preço na label[ô]
Private Sub cbxcardapiocomanda_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxcardapiocomanda.SelectedIndexChanged
Dim strCon As String
strCon = [Ô]server=localhost ; user=root ; password=123456789a ; database=bardotorresmo [Ô]
Dim conn As New MySqlConnection
Dim comando, comando2 As New MySqlCommand
Try
conn.ConnectionString = strCon
conn.Open()
comando.Connection = conn
comando.CommandText = [Ô]select idcardapio, nomecardapio, preco from cardapio where nomecardapio like UPPER([ô][Ô] & cbxcardapiocomanda.SelectedItem & [Ô][ô])[Ô]
Dim dr As MySqlDataReader = comando.ExecuteReader
Dim str As String = dr.ToString
While (dr.Read())
lbl_preco_unit.Text = (dr([Ô]preco[Ô]))
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show([Ô]Erro ao efetuar a conexão com a base de dados : [Ô] + ex.Message)
conn.Dispose()
End Try
End Sub
Private Sub btnmais_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmais.Click
If cbxcardapiocomanda.SelectedItem = [Ô][Ô] Then
MsgBox([Ô]Selecione o cardapio para adicionar a comanda[Ô])
Else
[ô] BY GIOVANNI
index_cardapio = cbxcardapiocomanda.SelectedIndex + 1
If txtquantidade.Text <> Nothing Then
total = lbl_preco_unit.Text * Val(txtquantidade.Text)
Else
txtquantidade.Text = 1
total = lbl_preco_unit.Text * 1
End If
For i As Integer = 1 To Val(txtquantidade.Text)
lbxcardapios.Items.Add(cbxcardapiocomanda.SelectedItem & [Ô]-[Ô] & lbl_preco_unit.Text)
Next
totalgeral = totalgeral + total
TxtValor.Text = totalgeral.ToString([Ô]c[Ô])
txtquantidade.Text = Nothing
End If
End Sub
Private Sub btnretirar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnretirar.Click
If lbxcardapios.Text = [Ô][Ô] Then
MsgBox([Ô]não é possivel deletar quando não se tem produtos selecionados na comanda[Ô])
Else
lbxcardapios.Items.RemoveAt(Val(index_remove2))
Dim strCon As String
strCon = [Ô]server=localhost ; user=root ; password=123456789a ; database=bardotorresmo [Ô]
Dim conn As New MySqlConnection
Dim comando, comando2 As New MySqlCommand
Try
conn.ConnectionString = strCon
conn.Open()
comando.Connection = conn
comando.CommandText = [Ô]select idcardapio, nomecardapio, preco from cardapio where idcardapio like UPPER([ô][Ô] & index_cardapio & [Ô][ô])[Ô]
Dim dr As MySqlDataReader = comando.ExecuteReader
Dim str As String = dr.ToString
While (dr.Read())
totalremove = (dr([Ô]preco[Ô]))
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show([Ô]Erro ao efetuar a conexão com a base de dados : [Ô] + ex.Message)
conn.Dispose()
End Try
TxtValor.Text = Val(TxtValor.Text) - totalremove
totalgeral = Val(TxtValor.Text)
TxtValor.Text = totalgeral
End If
End Sub
Private Sub lbxcardapios_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxcardapios.SelectedIndexChanged
index_remove = lbxcardapios.SelectedIndex + 1
End Sub
Private Sub lbxcardapios_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbxcardapios.MouseClick
index_remove = lbxcardapios.SelectedIndex + 1
End Sub
[txt-size=1]porém quando eu clico no botão de excluir ele exclui da listbox e zera o valor mas se tiver mais de 2 itens na listbox ele exclui e mesmo assim zera o valor da texttotal[/txt-size]
aqui como fica quando eu deleto um item se tem 2 é pra tirar o seu valor e continuar o valor do outro produto no total;s
nesse codigo exclui linha selecionada no listbox, vejá se dá certo
Dim nomesel As ListBox.SelectedObjectCollection
Dim intcont As Integer
nomesel = listbox.SelectedItems
[ô]percorre coleçao de items selecionado
[ô]exclui cada item do list de nomes
For intcont = (nomesel.Count - 1) To 0 Step -1
listbox.Items.Remove(nomesel.Item(intcont))
Next
Dim nomesel As ListBox.SelectedObjectCollection
Dim intcont As Integer
nomesel = listbox.SelectedItems
[ô]percorre coleçao de items selecionado
[ô]exclui cada item do list de nomes
For intcont = (nomesel.Count - 1) To 0 Step -1
listbox.Items.Remove(nomesel.Item(intcont))
Next
Citação::
nesse codigo exclui linha selecionada no listbox, vejá se dá certo
Dim nomesel As ListBox.SelectedObjectCollection
Dim intcont As Integer
nomesel = listbox.SelectedItems
[ô]percorre coleçao de items selecionado
[ô]exclui cada item do list de nomes
For intcont = (nomesel.Count - 1) To 0 Step -1
listbox.Items.Remove(nomesel.Item(intcont))
Next
não deu certo não :(
mas vlw pela ajuda
Tópico encerrado , respostas não são mais permitidas