Multiplying multiple texboxes - vb.net

I am creating a menu application, I have at the moment 3 textboxes, textbox1 is price, textbox2 is quantity and textbox3 is total. I have successfully written the code to calculate the price of an item depending on the quantity they need. The code i have right now:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox8.Text = CStr(Val(TextBox6.Text) * Val(TextBox7.Text))
End Sub
Now what I need is that I will have more items say 10 along with textboxes beside as quantity which makes 20 textboxes and one total textbox. How can I write the code so that it calculates every textbox as well as null values say if out of the 10 items i want only 2 items 1 per quantity into the total value.
Thanks

First, i strongly suggest to use .NET methods instead of old VB methods. I also would set OPTION STRICT to On in general which avoids "magical" conversions done for you by the runtime. Instead you have to specify the correct types which is a good thing since it can prevent errors on runtime and it also helps to learn the .NET types and methods.
I would add those TextBoxes all to the same container-control (like a Panel or something similar). I also suggest to use more meaningful names for your controls(f.e. TxtTotal for the total-textbox).
So if all price-textboxes' names start with TxtPrice (f.e. TxtPrice1 etc) and all quantity-TextBoxes start with TxtQuantity (f.e. TxtQuantity1 etc), this LINQ query approach will work:
Dim allTextBoxes = PriceQuantityPanel.Controls.OfType(Of TextBox)()
Dim allPrices = From txt In allTextBoxes
Where txt.Name.StartsWith("TxtPrice")
Dim allQuantities = From txt In allTextBoxes
Where txt.Name.StartsWith("TxtQuantity")
Dim price As Decimal
Dim invalidPrices = From txt In allPrices
Where Not Decimal.TryParse(txt.Text, price)
If invalidPrices.Any() Then
MessageBox.Show("Please enter valid prices(Decimal)!")
Return
End If
Dim quantity As Int32
Dim invalidQuantities = From txt In allQuantities
Where Not Int32.TryParse(txt.Text, quantity)
If invalidQuantities.Any() Then
MessageBox.Show("Please enter valid quantities(Integer)!")
Return
End If
Now you can "join" the pairs of price and quantity textboxes by the number-suffix:
Dim query = From txtP In allPrices
Join txtQ In allQuantities
On txtP.Name.Substring("TxtPrice".Length) Equals txtQ.Name.Substring("TxtQuantity".Length)
Select New With {.Price = Decimal.Parse(txtP.Text), .Quantity = Int32.Parse(txtQ.Text)}
Dim totalSum As Decimal = query.Sum(Function(x) x.Price * x.Quantity)
TxtTotal.Text = totalSum.ToString()

Related

Visual Basic For Loop how to compare listbox to another listbox?

So im suppose to make a program on VB that records numbers into a list box and find the average, and then i am suppose to compare the previous list box numbers and transfer any number that are above average into the other list-box.
Here is my code so far. I am stuck on the part where I have to transfer the numbers that are above average to another list box.
My logic is [show the count of the numbers, then compare the count of numbers to the average, and any numbers that are greater than the average, ill added onto the new list box] but i dont know how to write the syntax.
Option Strict On
Public Class frmAverageOfScore
Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
Dim lblscore As Double
lblscore = CDbl(txtScore.Text)
lstListofScores.Items.Add(lblscore)
End Sub
Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
Dim listScores As Integer = lstListofScores.Items.Count
Dim sum As Double = 0
For average As Integer = 0 To (listScores - 1)
sum += CDbl(lstListofScores.Items(average))
Next
txtAverage.Text = (sum / listScores).ToString("N")
End Sub
End Class
Your existing two routines already provide the foundation for what you need to do. For example, you're already looping through the items in the first list in your averaging routine. Now, just compare each to your average, and populate the second list if they are greater, similar to how you populate the first list.
For i As Integer = 0 To (listScores - 1)
If (CDbl(lstListofScores.Items(i)) > CDbl(txtAverage.Text)) Then
lstListofScores2.Items.Add(lstListofScores.Items(i))
End If
Next
Something to note: There are far more efficient, and readable ways to do this, but I wanted to use code similar to what you've already written. You already know the functionality, you just have to apply it in a slightly different way. Some simple suggestions: store a variable for the average (probably as a Double) so you aren't recalculating it every iteration; use a For Each loop to iterate the Items in the ListBox instead of a For..Next loop, etcetera.

How to generate a random selection from SQL DB to a listbox using vb.net

I am trying to write a program that will allow a user to generate a random list of names. I have a gridview of names from a SQL Db when the form launches. Is it possible to generate a random list from the names in the gridview or does that have to come from another Sql Connection string and reference different parameters? I was trying to display random names from the gridview to a listbox. Thank you.
Here is the code that I have been trying to experiment with:
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
Dim listCount As Integer
Dim i As Integer = 0
Dim rnd As New Random
Dim listselection As Integer
listCount = grdEmployees.
Do While i < CInt(grdEmployees.Text)
'randomize selection
listselection = rnd.Next(0, grdEmployees.Items.Count)
lstSelected.Items.Add(grdEmployees.Items(listselection))
grdEmployees.Items.RemoveAt(listselection)
'increment i
i += 1
Loop
txtQuantity.Text = String.Empty 'Clears box after entry
End Sub
You could do it through your SQL query:
SELECT TOP 25 SomeField FROM SomeTable ORDER BY RAND()
Or through your managed code. Which is best depends on the size of the table and where you want the sorting to be done. If you prefer to sort on the server, or locally.

Use an InputBox to enter prices of 4 items

I'm new to VB. Today I'm working on entering 4 prices for items to purchase using an input box. I need to create a counter in a loop. The only 2 buttons on the form "Enter Prices" and "Exit". So far this is the code I have (see below). I know something is off. When I run it, I'm allowed to enter 4 numbers. But at the end, when the message box comes up to show my total, it just gives me my last number I entered. I know I've got to change a few things, as I need my numbers to be in currency. Any suggestions as to where I need to go from here to get this up and running?
Private Sub btnPrices_Click(sender As Object, e As EventArgs) Handles btnPrices.Click
'Declare a variable as counter and accumulator
Dim intcount As Integer = 1I
Dim intAccumulator As Integer = 0I
'Declare and intialize variable
Dim strInput As String = ""
'Number of Items
Const intNUM_PRICES As Integer = 4
'Pre-test loop will keep iterating as long as the expression is ture.
Do While intcount <= intNUM_PRICES
'Get price of each item purchased
strInput = InputBox("Enter Price " & intcount, "Price Needed")
'Add 1 to the counter
intcount += 1
Loop
'Look at the value placed in the
MessageBox.Show("Your combined Price for all 4 items is: " & strInput)
End Sub
You've only got one variable for the input and each time you call InputBox you replace the previous value each time. If you want a total then you have to add the values, so you need to add the current input to the previous total each time, not replace it. Make sure that you convert the input to a number and use a numeric variable, because adding strings will actually join them, not add them mathematically.

Visual Basic: Making multiple forms for an order

I've been working on this project for two weeks now and I'm drained from all the hiccups I've come across. Now I've hit a brick wall.
I'm making a "simple" order form in Visual Basic 2010 that calculates the amount due for an order and then from the total determines what the shipping will be. I've been able to accomplish the following tasks:
accept customer data (name, address, city, state abbreviation, zipcode)
calculate total with shipping costs (accurately, for the most part)
clear the order form and exit
But the way I have it right now, a customer can only put in 1 order form.
Problem: I have to figure out a way to allow a customer to place an order form for multiple items. (I'll assume that a customer will not place an order for more than 20 items). So what would be the best way to go about saving the information for multiple items then calculating the cost at the end?
If it's multiple form creation, how is that done? Is the keyword "Container" the way to go, and how? At the end of the day, I just don't know how to create this multiple item form (it's like a database in Visual Basic), so any method, at least pointing me in the right direction, would be helpful.
(Tried to post an image of my work so far but I'm new here - can't post images yet. I've posted a bit of my code below - code works for 1 order)
Snippet Code:
Private Sub total_mouseleave(ByVal sender As Object, ByVal e As EventArgs) Handles total.MouseLeave
Dim gs_1 As Decimal = 4.95
Dim gs_2 As Decimal = 6.95
Dim gs_3 As Decimal = 8.95
Dim gs_4 As Decimal = 10.95
Dim gs_5 As Decimal = 12.95
Dim ex_1 As Decimal = 14.95
Dim ex_2 As Decimal = 16.95
Dim ex_3 As Decimal = 18.95
Dim ex_4 As Decimal = 20.95
Dim ex_5 As Decimal = 22.95
Dim totalcost As Decimal
totalcost = Convert.ToDecimal(total.Text)
If gs_radiobutton.Checked Then
If (totalcost < 1) Then
total.Text = Val(0)
shipping.Text = Val(0)
ElseIf (1 < totalcost AndAlso totalcost < 15) Then
total.Text = Val(totalcost + gs_1)
shipping.Text = ground_1
ElseIf (15 < totalcost AndAlso totalcost <= 49.99) Then
total.Text = totalcost + gs_2
shipping.Text = gs_2
ElseIf (50 < totalcost AndAlso totalcost <= 99.99) Then
total.Text = totalcost + g_3
shipping.Text = gs_3
ElseIf (100 < totalcost AndAlso totalcost <= 199.99) Then
total.Text = totalcost + gs_4
shipping.Text = gs_4
Else
total.Text = totalcost + gs_5
shipping.Text = gs_5
End If
End If
'// . . . .
End Sub
It's not clear from your post if you mean order forms as in a Form object within VB or an order placed by the user.
If you meant orders placed by the user then it would be simpler to use a single form but create a few custom classes to hold all the order line details:
' Class used for each order line
Public Class orderline
Public Value As Decimal
Public StockID As Integer
Public Qty As Integer
End Class
' Class used to store the entire order
Public Class Order
' List of orderlines (similar to an array)
Public OrderLines As List(Of orderline)
Private _totalcost As Decimal
Public ReadOnly Property TotalCost As Decimal
Get
Dim t As Decimal
For Each l In OrderLines
t = t + (l.Value * l.Qty)
Next
Return t
End Get
End Property
End Class
Then in the form you can use the classes as below:
Public Class Form1
Dim ord1 As New Order
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ord1.TotalCost
End Sub
End Class
You can then create more than one order stored on the form using a list:
Dim CustomerOrders as List<Of Order>
You should be able to create multiple instances of a form and show them non-morally. Whether this is the best design is another discussion.
The non-modal forms would all be visible and can be 'owned' by a main form.
Try something like this:
Dim firstInstance As New YourForm()
Dim secondInstance As New YourForm()
firstInstance.Show() 'Not ShowDialog()
secondInstance.Show(myParent) 'Optional parent form parameter
You'll need to watch the scope of your form instances as the forms will be closed if the variables go out of scope.
This may not be the ultimate solution, but it's something you can try on the way to finding the optimal approach.
Considering the latest comment:
The problem is not clear. Do you want to create multiple forms, one form per order? Or do you want one form that can show multiple orders?
My solution will allow you to show multiple forms while the other answer posted suggests a neat solution to creating one form with multiple orders.
Personally, I would create a single form that shows multiple orders, ListView maybe, that provides some way of viewing/editing each order's details and then allowing the orders to be submitted in a batch. It's more complex but would be more usable, IMHO.

VB.net Can't get output to appear in my listbox. Beginners Question

Trying to get the user to put 3 numbers in 3 text boxes and get the average.
Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
Dim a As Integer = CInt(txtone.Text)
Dim b As Integer = CInt(txtTwo.Text)
Dim c As Integer = CInt(txtThree.Text)
Dim average As Integer
average = (a + b + c) / 3
lstOutput.Text = average
End Sub
Try changing the type of average from Integer to Double
Dim average as Double
Right now you're trying to store the Average in an Integer which can only hold a whole number. Averages tend to be non-whole numbers and need a data type that can represent that. Double is good for most situations. That should fix your problem.
EDIT OP mentioned that lstOutput is a ListBox
This is one of the confusing things with WinForms. Even though every single control has a Text property, not all of them actually do anything. They only apply to elements that directly display a single text block or value. Ex Button, Label, etc ...
A ListBox on the other hand displays a group of items. You want to add a new item to the list.
lstOutput.Items.Add(average.ToString())
The Text property of a list box will get or set the selected item. You haven't added your average to the listbox yet.
Try:
lstOutput.Items.Add(average)
Are you sure that txtOne.text txtTwo.text and txtThree.txt will always be an integer value?
You might need to also change the a,b,c vars to Doubles and check that the user didn't supply non-numeric values.
If the user puts "one" in the txtOne textbox, you'll get an exception kablowee.
(air coding here)
dim a as new double
try
if isnumeric(txtOne.text.tostring.trim) then
a = cdbl(txtOne.text.tostring.trim)
end if
'repeat for b and c ...
catch ex as exception
messagebox.show(ex.message.tostring)
end try
And, I'm not sure if I'm right about this, (maybe someone will enlighten me) but does .NET consider type conversion from string to int differently in these two cases
a = cint(txtOne.text)
and
a = cint(txtOne.text.tostring)
???