Add items to listview after cleared - vb.net

I am adding all the textboxes and labels to display in the listview. When I click the clear button everything on my form clears as it should, but when I then want to add more items to the listview, nothing displays in the listview, and my header information is also cleared. Can someone please assist?
Public Class Form2
Dim decTotalDue As Decimal
Dim intTotalItems As Integer
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged
End Sub
Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
Dim decUnitPrice As Decimal
Dim intQuantity As Integer
Dim decTotal As Decimal
Dim decTotalPayable As Decimal
Dim item As New ListViewItem
Decimal.TryParse(txtUnitPrice.Text, decUnitPrice)
Integer.TryParse(txtQuantity.Text, intQuantity)
decTotal = decUnitPrice * intQuantity
lblTotal.Text = decTotal.ToString("C2")
decTotalDue = decTotal + decTotalDue
lblTotalDue.Text = decTotalDue.ToString("C2")
intTotalItems = intQuantity + intTotalItems
lblTotalItems.Text = intTotalItems.ToString
decTotalPayable = decTotalDue
lblTotalPayable.Text = decTotalPayable.ToString("C2")
lblTotalPayable.Hide()
lblTotalItems.Hide()
item = ListView1.Items.Add(cboItemName.Text)
item.SubItems.Add(txtUnitPrice.Text)
item.SubItems.Add(txtQuantity.Text)
item.SubItems.Add(lblTotal.Text)
ListView1.ForeColor = Color.White
txtUnitPrice.Text = decUnitPrice.ToString("C2")
End Sub
Private Sub btnPurchase_Click(sender As Object, e As EventArgs) Handles btnPurchase.Click
lblTotalItems.Show()
lblTotalPayable.Show()
cboItemName.Text = String.Empty
txtUnitPrice.Clear()
txtQuantity.Clear()
lblTotal.Text = ""
lblTotalDue.Text = ""
ListView1.Clear()
End Sub
Private Sub btnCalculateChange_Click(sender As Object, e As EventArgs) Handles btnCalculateChange.Click
Dim decCashTenderted As Decimal
Dim decChange As Decimal
Decimal.TryParse(txtCashTendered.Text, decCashTenderted)
txtCashTendered.Text = decCashTenderted.ToString("C2")
decChange = decCashTenderted - decTotalDue
lblChange.Text = decChange.ToString("C2")
If decCashTenderted < decTotalDue Then
MessageBox.Show("Cash Tendered is less than Total Due", "Invalid", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
cboItemName.Text = String.Empty
txtCashTendered.Clear()
txtUnitPrice.Clear()
txtQuantity.Clear()
lblTotalDue.Text = ""
lblTotalItems.Text = ""
lblTotalPayable.Text = ""
lblChange.Text = ""
ListView1.Clear()
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
End Class

MSDN on the behaviour of the Clear function is:
You can use this method to remove all items and columns from the ListView control without having to call the individual Clear methods from the ListView.ColumnHeaderCollection and ListView.ListViewItemCollection classes.
From what you describe you want, you should be doing is calling:
ListView1.Items.Clear()
This will remove just the items that are displayed and not remove the column definitions.

Related

How I can change the text of a single label from a string array vb.net?

so I want to display a string array in a single label that change the contents with a period of time ,
I ve tried every thing the timer, the background worker every thing , the problem when I use a loop inset a timer the interval in the start should be so long if the array items was so many so I tried the background worker but it not works
this is the code :
Dim array() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim delay As Integer = 2000
Dim interval As Integer = 100
Dim elapsed As Integer = 0
Dim pos As Integer = array.Length
While Not worker.CancellationPending
If (elapsed >= delay) Then
worker.ReportProgress(pos)
' change label text in the Progress event handler
pos = (pos + 1)
elapsed = 0
If (pos = array.Length) Then
Exit While
End If
End If
Thread.Sleep(interval)
End While
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim j As Integer
For j = 0 To array.Length
Label1.Text = array(j)
Next
End Sub
Your for loop is overwriting the .Text property of the lable on each iteration. You will also get an Index Out of Range exception because the indexes of an array are zero based. The highest index will be 1 less than the .Length.
Private arStr() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim LabelText = String.Join(Environment.NewLine, arStr)
Label1.Text = LabelText
End Sub
EDIT
Private arStr() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static index As Integer
If index < arStr.Length Then
Label1.Text &= arStr(index) & Environment.NewLine
index += 1
End If
End Sub
With asynchronous approach you can display array values one after another without BackgroundWorker and explicitly created Timer.
Private _values As New String() From {"so", "nb", "de", "rty", "dcds"}
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each value In _values
await Task.Delay(2000)
Next
End Sub
With asynchronous approach you can prevent button click before all values are displayed in the simple way as you would do in synchronous code.
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim button = DirectCast(sender, Button);
button.Enabled = False
For Each value In _values
await Task.Delay(2000)
Next
button.Enabled = True
End Sub

calculation is only performed once on food ordering form (VB)

I am making a food ordering form, and I am having trouble with the calculations. Every time a hotdog is ordered the form is supposed to add the cost to the total amount, so one hotdog is 2 dollars, add another and it says 4 dollars, etc. But it only performs the calculation once, so no matter how many items I add to the order the price stays the same, as if I had only ordered one.
I don't get any errors at runtime so it must be a logical error. Here is the full code. I don't know what other information I should give at this moment. All help is appreciated.
Public Class DroneDogs
Dim DogChoice As String
Dim condiment1 As String
Dim condiment2 As String
Dim condiment3 As String
Const DBL_TAX_RATE As Double = 0.07
Const DBL_DRONE_DOG As Double = 1.99
Dim intNumDog As Integer
Dim dblSubTotal As Double
Dim dblSalesTax As Double
Dim dblTotalCost As Double
Private Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
cboCurrentCustomers.Items.Add(txtAddCustomer.Text)
End Sub
Private Sub optBeef_CheckedChanged(sender As Object, e As EventArgs) Handles optBeef.CheckedChanged
If (optBeef.Checked) Then
DogChoice = "Beef Dog"
End If
End Sub
Private Sub optPork_CheckedChanged(sender As Object, e As EventArgs) Handles optPork.CheckedChanged
If (optPork.Checked) Then
DogChoice = "Pork Dog"
End If
End Sub
Private Sub optTurkey_CheckedChanged(sender As Object, e As EventArgs) Handles optTurkey.CheckedChanged
If (optTurkey.Checked) Then
DogChoice = "Turkey Dog"
End If
End Sub
Private Sub chkKetchup_CheckedChanged(sender As Object, e As EventArgs) Handles chkKetchup.CheckedChanged
If (chkKetchup.Checked) Then
condiment1 = " + Ketchup"
Else
condiment1 = Nothing
End If
End Sub
Private Sub chkMustard_CheckedChanged(sender As Object, e As EventArgs) Handles chkMustard.CheckedChanged
If (chkMustard.Checked) Then
condiment2 = " + Mustard"
Else
condiment2 = Nothing
End If
End Sub
Private Sub chkRelish_CheckedChanged(sender As Object, e As EventArgs) Handles chkRelish.CheckedChanged
If (chkRelish.Checked) Then
condiment3 = " + Relish"
Else
condiment3 = Nothing
End If
End Sub
Private Sub btnAddDog_Click(sender As Object, e As EventArgs) Handles btnAddDog.Click
intNumDog = +1
dblSubTotal = intNumDog * DBL_DRONE_DOG
dblSalesTax = dblSubTotal * DBL_TAX_RATE
dblTotalCost = dblSubTotal + dblSalesTax
txtSub.Text = dblSubTotal.ToString("c2")
txtTax.Text = dblSalesTax.ToString("c2")
txtTotal.Text = dblTotalCost.ToString("c2")
Dim addCondiment As String = condiment1 + condiment2 + condiment3
If (DogChoice = "Beef Dog") Or
(DogChoice = "Pork Dog") Or
(DogChoice = "Turkey Dog") Then
lstOrder.Items.Add(DogChoice + addCondiment + ": " + txtTotal.Text)
Else
MsgBox("Please select a Hot Dog type.")
End If
End Sub
Private Sub btnClearOrder_Click(sender As Object, e As EventArgs) Handles btnClearOrder.Click
lstOrder.Items.Clear()
txtSub.Clear()
txtTax.Clear()
txtTotal.Clear()
chkKetchup.Checked = 0
chkMustard.Checked = 0
chkRelish.Checked = 0
optBeef.Checked = 0
optPork.Checked = 0
optTurkey.Checked = 0
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Exits the program
If MessageBox.Show("Do you want to exit the DroneDogs application?", "DroneDogs",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
= DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub btnOrderComplete_Click(sender As Object, e As EventArgs) Handles btnOrderComplete.Click
Receipt.Show()
End Sub
Private Sub btnRemoveDog_Click(sender As Object, e As EventArgs) Handles btnRemoveDog.Click
lstOrder.Items.Remove(lstOrder.SelectedItem)
End Sub
End Class
I think the problem is with the line
intNumDog = +1
In this line, you always set intNumDog to the value of +1. This is the same as:
intNumDog = 1
You want to add one to the value of intNumDog:
intNumDog += 1

lblBox Returns zero after assigning lstBox selected item using .ToString("P0") and .TrimEnd

I am writing a simple program, the textbook want me to use the string.TrimStart() and string.TrimEnd() methods within the program. You input your amount into txtSales, then select the appropriate sales tax within lstRates, press the calculate button and it calculates how much tax you are charged and displays it in lblTax. Now, seems no matter what method i use do do this, lstRates always returns 0.. Any suggestions?
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtSales_Enter(sender As Object, e As EventArgs) Handles txtSales.Enter
txtSales.SelectAll()
End Sub
Private Sub lstRates_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstRates.SelectedValueChanged
lblTax.Text = String.Empty
End Sub
Private Sub txtSales_TextChanged(sender As Object, e As EventArgs) Handles txtSales.TextChanged
lblTax.Text = String.Empty
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
' fills the list box with tax rates
For dblRate As Double = 0.02 To 0.1 Step 0.01
lstRates.Items.Add(dblRate.ToString("P0"))
Next dblRate
lstRates.SelectedIndex = 0
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim dblSales As Double
Dim dblRates As Double
Dim strRates As String
Double.TryParse(txtSales.Text, dblSales)
strRates = lstRates.Text
strRates = strRates.TrimEnd("%"c)
Double.TryParse(strRates, dblRates)
Double.TryParse(lstRates.Text, dblRates)
lblTax.Text = (dblSales * dblRates).ToString
End Sub
End Class
Deleted Double.TryParse(lstRates.Text, dblRates) and added dblRates *= 0.01
Works like a charm!
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim dblSales As Double
Dim dblRates As Double
Dim strRates As String
Double.TryParse(txtSales.Text, dblSales)
strRates = lstRates.Text
strRates = strRates.TrimEnd("%"c)
Double.TryParse(strRates, dblRates)
dblRates *= 0.01
lblTax.Text = (dblSales * dblRates).ToString("C2")
End Sub

How to store last button clicked and add to listbox

So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub

add subitems to a listview

I'm using a backgroundworker to populate a listview, but i want to add subitems also. Can anyone help me out?
Public Class Form1
Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Dim li As New List(Of ListViewItem)
For Each fn As String In My.Computer.FileSystem.GetFiles("s:\Videos", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
li.Add(New ListViewItem(My.Computer.FileSystem.GetName(fn)))
'here i want to add a subitem containing the filesize
'My.Computer.FileSystem.GetFileInfo(fn).Length
Next
e.Result = li.ToArray
End Sub
Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
lv.Items.AddRange(DirectCast(e.Result, ListViewItem()))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
bgw.RunWorkerAsync()
End Sub
End Class
Try this in your For Each loop:
Dim NewItem as New ListViewItem(My.Computer.FileSystem.GetName(fn))
NewItem.SubItems.Add(My.Computer.FileSystem.GetFileInfo(fn).Length)
li.Add(NewItem)
Hopefully that should do the trick
this is working too, but is it correct?
Public Class Form1
Dim item1 As String = ""
Dim item2 As String = ""
Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Dim progress As Integer = 0
'calculate progress later
progress = 1
For Each fn As String In My.Computer.FileSystem.GetFiles("s:\Videos", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
item1 = My.Computer.FileSystem.GetName(fn)
item2 = My.Computer.FileSystem.GetFileInfo(fn).Length
bgw.ReportProgress(progress)
Next
End Sub
Private Sub bgw_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
Dim li As New ListViewItem
li = lv.Items.Add(item1, 0)
li.SubItems.Add(item2)
End Sub
Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lv.Items.Clear()
bgw.RunWorkerAsync()
End Sub
End Class