Hello All,
I'm an e-student at a local college just learning the art of programming for the first time. For the most I’ve been able to muddle through the course using the books and without asking any questions. When I finally posed one, the instructors answer was, to say the lest, mystifying.
With the hopes of getting a real answer I’ve decided to post my question here.
I am writing a relatively simple Sales Receipt program in VB.net with one “add to the list” button, a textbox to type in the “price”, a listbox to keep track of the numbers I’ve entered, and three more textboxes to tally the sub, tax, and total.
I’ve managed to accomplish the basic task of getting the textbox “price” into the listbox and maintaining the cursor focus on the textbox. I know how to calculate the sub, tax and total. But I can’t seem to wrap my head around the “How do I get the listbox accumulated into the subtotal textbox?”
A code that will add up the items in the list as they accumulate is not feasible. What I need I something a beginner can accomplish like taking the textbox “price” and add it, not only to the listbox as ive done, but also place it into the Subtotal textbox. Then with each additional "price", keep up with the addition.
Can it be done?
Here is my code so far
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim TextBox1 As Double
Dim ListBox1 As Double
Dim txtSubtotal As Double
Dim txtTax As Double
Dim txtTotal As Double
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
'Clear and Retain TextBox1 Focus
Dim Source As String
Source = TextBox1.Text
TextBox1.Text = ListBox1.Items.Add(Source)
If TextBox1.TextLength = 0 Then
TextBox1.Focus()
End If
If ListBox1.Items.Contains(Source) = True Then
TextBox1.Clear()
TextBox1.Focus()
End If
'Calculate amounts
Dim SubTotal As Decimal
Dim CurrentTaxRate As Decimal
Dim Tax As Decimal
Dim TotalDue As Decimal
txtSubTotal.Text = SubTotal
CurrentTaxRate = 0.065
Tax = CDec(SubTotal * (CurrentTaxRate))
txtTax.Text = (Tax)
TotalDue = (SubTotal + (Tax))
txtTotal.Text = (TotalDue)
End Sub
'Format Sales Receipt as currency
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
TextBox1.Text = FormatCurrency(TextBox1.Text)
End Sub
End Class
Someplace in there I need a running Subtotal for this to work. I hate to ask questions but I'm overlooking somthing and I need help.
Thanks for your time
H~
HowardPosted Apr 16, 2006, 9:43 AM
Brilliant!
There I was thinking I needed that IF statement, and as a student how smart I was for using it. It was the First time I’d ever written an If Statement so I guess I stubbornly overlooked that obvious flaw in my program.
Lesson Learned.
Thank You Ezdehar, you have been extremely helpful.
PS. I do have another If statement in the program that works just fine. It insures that only numbers are placed in the TextBox.
It works. :)
Ezdehar NoorPosted Apr 16, 2006, 3:38 AM
i can not understand the purpose of this if ,why u write it
If ListBox1.Items.Contains(Source) = True Then
i tried the following code in a test project and it achieve the things u need (if i understood u correctly)
without exceptions Private Sub addbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click Dim Source As String
Source = TextBox1.Text
TextBox1.Text = ListBox1.Items.Add(Source)
TextBox1.Clear()
TextBox1.Focus()
End Sub Private Sub ext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ext.Click End End Subif there is misunderstaning tell me plsHowardPosted Apr 15, 2006, 10:30 PM
OK it me again,
It's hot, the work is pitilessly agonizing, and now I’m all tangled up. Yet I’m |this| close to completion. Can I ask just one more question?
I know my problem lies in the Focus that I’ve placed on the textbox, but it sort of needs it to be like this.
Privet Sub BtnAdd_Click ( bla bla bla)
Dim Source As String
Source = TextBox1.Text
TextBox1.Text = ListBox1.Items.Add(Source)
If ListBox1.Items.Contains(Source) = True Then
TextBox1.Clear()
TextBox1.Focus()
End If
Otherwise I’d have to mouse click back to the textbox and backspace, as it counts up form zero, without the clear and focus commands in place.
However, as written, with the focus concentrated on the textbox, I’m unable to exit using the BtnExit_click command.
Is there some way I can If and Else my way out of this predicament. Or place some override or redirect focus on the Exit button.
What I real don’t understand is…Why doesn’t my exit button work but my add button does? A button click by definition should place the focus on the click.
HowardPosted Apr 15, 2006, 3:53 PM
In the immortal words of Elvis “Thank You, thank you very much”. I had to tweak my code just a little to make it work for me but, as a work in progress, I have some extra code in places which I do not need and have to remove it anyway - so tweaking is all part of the clean up process.
But yes your solution worked great. I wrote it down in my note book under “Ezdehar’s solution” in case I come across another problem like it.
Thanks again.
I’m now just trying to figure out where I went wrong with retaining focus on textbox1. I believe now when I click exit (BtnExit_Click) I get an unhandled exception error because of it.
I Think maybe I need another If condition statment in there.
Ezdehar NoorPosted Apr 15, 2006, 4:59 AM
Hello
I think that your problem is simple (if i understood it correctly), to accumulate the subtotal textbox with the total prices you can write
Dim i As Integer
Dim tot As Decimal = 0
For i = 0 To Me.ListBox1.Items.Count - 1
tot += Me.ListBox1.Items(i)
Next
SubTotal.Text = tot
if there is misunderstanding pls tell me (usually humans aske when they dont know some thing , question is a good method to benifits from other experiences)