I've got a simple form in my app used for adding orders.
It contains two textbox controls, 'neworder_costprice' and 'neworder_saleprice'.
I also have a slider control, markup_percent', which can be a value between 0 and 100 at increments of 10.
I'm trying to make it so if the user types in "1.20" for example in the costprice textbox, the saleprice textbox will automatically populate with the value of costprice + markup_percent.
I've tried a few different ways of getting this to work, but nothing seems to want to do it for me! Can anyone point out the error of my ways?
The following is my current code from a 'workoutsaleprice()' function which is called upon costprice.valuechanged....
tech_neworder_costprice.Text = String.Format("{0:n2}", neworder_costprice.Text)
Dim costprice As Double = neworder_costprice.Text
Dim markup As Integer = percent_slider.Value
Dim saleprice As Double = ((costprice / 100) * markup) + costprice
neworder_saleprice.Text = saleprice.ToString
Use NumericUpDown for numeric input instead of a TextBox. Validation is handled automatically so it is guaranteed to never have a non numeric value.
You would instead use the NumericUpDown.Value property of type Decimal to perform numeric operations.
Related
I have got a data column representing integer values for example
value 100000, i would like to format display into 100.000 or 100 000. To fix this i used code below but seems it doesn't work in my case.
dt.Rows(i).Item("QORDPO") = FormatNumber(ilosc, 0,,, TriState.True)
If you are using a DataGridView (as the tags indicate) have you tried the DefaultCell Property on the column? There are various option one of which is Custom, if the ones presented don't suit.
Examples of setting the column format properties in code:
datagridview.Columns("aColumn").DefaultCellStyle.Format() = "C" '--- currency
datagridview.Columns("aColumn").DefaultCellStyle.Format() = "N1" '--- number
datagridview.Columns("aColumn").DefaultCellStyle.Format() = "d" '--- date
Simple calculation TotalPrice = QTY * UnitPrice
Does not calculate when I get the Unit Price to display as currency.
The unit price is retrieved from a combo box in the AfterUpdate event as follows:
Private Sub cboItemRequested_AfterUpdate()
' 0 1 2 3 4 5 6
' ITEM Category SIZE UI PRICE NSN_ORDER UNIT_PACK
With Me
.txtDescrOfItemRequested = .cboItemRequested
.txtUI = .cboItemRequested.Column(3)
.txtQTY = 1
.txtUnitPrice = Format(.cboItemRequested.Column(4), "Currency")
.txtPartNumNSN = .cboItemRequested.Column(5)
End With
End Sub
Even though the combo box displays the currency correctly in the 4th column, it does not populate the Unit Price txtbox correctly unless I apply the Format(XXXXX, "Currency"). Incidentally, the textbox is also formatted as currency. However, when I do get the dollar sign to appear, the final calculation TotalPrice remains zero.
I even applied the following to the txtTotalPrice
=Val(Nz([txtQTY],0))*Val(Nz([txtUnitPrice],0))
I tried responding to #krish KM so I scrapped everything above and instead created this function:
Public Function cTotalPrice(vQTY As Variant, vUnitPrice As Variant) As Currency
'only return a value if both fields are numeric
If IsNumeric(vQTY) = True And IsNumeric(vUnitPrice) = True Then
cTotalPrice = vQTY * vUnitPrice
End If
End Function
which I only call like this the AfterUpdate event of the cboItemRequested box and any time txtQTY or txtUnitPrice are updated
.txtTotalPrice = cTotalPrice(.txtQTY, .txtUnitPrice)
But txtUnitPrice simply does not display as currency even with txtUnitPrice formatted as currency.
SOLUTION:
I needed to Format the txtUnitPrice with VBA
.txtUnitPrice = Format(.cboItemRequested.Column(4), "Currency")
Yes, the txtBoxPrice formatted as currency just ignores it for some reason.
But in order for the TotalPrice to come out right, it was necessary to recalulate it at the end. This was the first tip I was given. Thanks krish KM
I am fairly new to programming. I am making a program where you enter a number into a textbox and when you leave the textbox, it converts your number to currency. I have also added another textbox which does the same.
This is what I want to happen:
If TextBox1.Text + TextBox2.Text =< 10,000 Then
Sum = 10,000
Else
Sum = TextBox1.Text + TextBox2.Text
However, (i think) because I am converting the numbers to currency before I add them together, it always returns 10,000 no matter what.
I know there is probably something pretty obvious I am missing. If you need any clarification, just ask! I appreciate your help.
+ concatenates strings, so TextBox1.Text + TextBox2.Text is a string and comparing it to the integer 10000 doesn't do what you want.
I would look for a text box control that lets you display numbers with a given format, but also gives you access to the numerical value as a property. Then you could add the values together without having to do a type conversion that may not work on your formatted currency string.
MaskedTextBox might give you some of that, but it's been a long time since I've used it.
You need to convert String values to Double (or any other numeric type), before adding them up.
Dim tbSum as Double = CDbl(TextBox1.Text) + CDbl(Textbox2.Text)
If tbSum =< 10000 Then Sum = 10000 Else Sum = tbSum
I have two fields on a record ("Qty" and "Harga").
How do I multiply the two and save result into another field in a ListView?
The ListView control has no built-in ability to perform calculations for you, like a spreadsheet. It just displays whatever data you give it to display. If you want it to display the product of a multiplication equation, you will need to do that calculation yourself in the code and then add the result to the ListView column. For instance:
Public Sub AddItem(description As String, total As Integer, count As Integer)
Dim i As ListViewItem = ListView1.Items.Add(description)
i.SubItems.Add(total.ToString())
i.SubItems.Add(count.ToString())
Dim product As Integer = total * count
i.SubItems.Add(product.ToString())
End Sub
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)
???