I have Textbox1.Lines
25.0
25.0
25.0
25.0
25.0
25.0
25.0
25.0
25.0
25.0
Dim value2 as double = 0.4
Dim lines As String() = Textbox1.Lines
lines = Val(lines(here is a line code loop)) + val(value2)
Textbox1.Lines = lines
how do I make this code work? it only calculates my integer, for example if it were 255.5, it only displays 255.
how can i transform this textbox, to calculate doubles, for example 25.4 + 25.7 = 30.1
I have to calculate the value of the line it has (ie 25.0) + the new value, if it is 0.4 then it will display 25.4 , Unfortunately, my Textbox doesn't know how to do this, and I don't know how to transform it so that I can read the values correctly.
Instead of the VisualBasic methods i suggest VB.NET. Use Double.Parse to parse the strings of each line to a Double, then add the fix value value2and use ToString to convert them back to string:
Dim invalidLines = From line in Textbox1.Lines
Where Not Double.TryParse(line.Trim(), Nothing)
If invalidLines.Any() Then
' Inform user that he should enter numeric values only '
Return
End If
Dim lineValues = From line in Textbox1.Lines
Select (Double.Parse(line.Trim()) + value2).ToString()
Textbox1.Lines = lineValues.ToArray()
You need to add Imports System.Linq.
Related
The problem now is how would I be able to format the values being displayed in datagridview from textfiles.
I have retrieved values from looping through textfiles removed the first two strings. Now I want to add separators or change the format of the displayed value like, for example:
textfile lines: result:
01Sample - line1
022 - line2
0306212019 - line3 06/21/2019
041234567890 - line4 12,345,678.90
I have already tried this one changing the defaultcellstyle but since the values are from textfiles in a directory its not affecting the output
DataGridView1.Columns("Gross Sales").DefaultCellStyle.Format = "##,0"
Private Sub ReadTextFiles()
Dim dt As New DataTable
dt.Columns.Add("Date")
dt.Columns.Add("Gross Sales")
Dim Folder As New IO.DirectoryInfo("c:\test\")
Dim lstLines As New List(Of String)
For Each fileentries As String In Folder.GetFiles("s*", IO.SearchOption.AllDirectories).OrderByDescending(Function(x) x.Name).Select(Function(x) x.FullName)
lstLines.AddRange(File.ReadAllLines(fileentries))
Next
Dim i As Integer
Dim OuterLoopIterations As Integer = CInt(lstLines.Count / 22)
For iterations = 0 To OuterLoopIterations - 1
Dim row = dt.NewRow
For col = 0 To 21
row(col) = lstLines(i).Remove(0, 2) 'i have removed the first 2 characters of each string
i += 1
Next
dt.Rows.Add(row(2), row(5), row(12), row(13), row(14), row(15), row(7), row(8), row(11))
Next
DataGridView1.DataSource = dt
'the code i tried applying
DataGridView1.Columns("Gross Sales").DefaultCellStyle.Format = "##,0"
this is my expected result
Current datagrid view:
the result should be for date column: 06/07/2019
for the gross : 48,990.14
Edit:
I tried this one
Dim B As Double
Dim Folder As New IO.DirectoryInfo("c:\test\")
Dim lstLines As New List(Of String)
For Each fileentries As String In Folder.GetFiles("s*", IO.SearchOption.AllDirectories).OrderByDescending(Function(x) x.Name).Select(Function(x) x.FullName)
B = CDbl(Val(fileentries))
lstLines.AddRange(File.ReadAllLines(B))
Next
If you want to format something as a number then it has to be a number. That means that, for example, if you read the text "1234.5" from the file and you want to display it as 1,234.50 in your grid then you have to convert the String you read to a Double or Decimal. If you do that then the numeric format specifier you're using in the grid column will work.
I'm making a counter that updates when the calculate button is pressed, but when I press it it add it like:
11111
where the answer should be 5.
Const tax = 1.05
Dim capPrice = 4.0
Dim espPrice = 2.5
Dim latPrice = 3.5
Dim icedPrice = 3.0
'keeping here teporarily
Dim takeAway = True
If (takeAway = True) Then
capCounter.text = capCounter.text + capAmount.text
espCounter.Text = espCounter.Text + espAmount.Text
latCounter.Text = latCounter.Text + latAmount.Text
icedCounter.Text = icedCounter.Text + icedAmount.Text
What you observe is string concatenation because a TextBox stores strings. You have to convert it to a number, for example with Int32.Parse, then you can sum the values:
Dim capCount As Int32
Dim capAmount as Int32
Dim validCapCount = Int32.TryParse(capCounter.Text, capCount)
Dim validCapAmount = Int32.TryParse(capAmount.Text, capAmount)
If validCapCount AndAlso validCapAmount Then
capCounter.Text = (capCount + capAmount).ToString()
End If
' do the same with the other values ...
This is because the Property Text of a control is a String.
If you want to perform your addition, you must convert those texts to a Integer first :
capCounter.text = (CType(capCounter.text, Integer) + CType(capAmount.text, Integer)).ToString()
(This if you are sure it will only be integers contained in the texts)
If you will use it for numbers only and allow user imput I will recommend you to use a numericUpDown and not a textboxt.
Then you can use the Value property of the numericupdown and it will return a numeric value, not a String.
However, if you need it to be a textbox, use CType as SuperPeanut sugested or TryParse as Tim sugested
Most of the other comments explain most of it, but have you tried using a different type of control to display the information, perhaps a NumericUpDownControl?
I am comparing a value that the user entered into a DataGridView cell - which, coming from an Editing Control will be a string to start with - with a decimal value from the data source (defined as decimal(3,2)).
How can I evaluate a user-entered value of ".7", for example, to be equal to the database value of 0.70?
You can use the CDec function to convert a string value to a decimal. e.g.
If CDec(".7") = 0.7 Then
' This will be true
End If
If you aren't sure that the value entered by the user will be a valid decimal, then you should use Decimal.TryParse:
Dim value As Decimal = 0
If Decimal.TryParse(".7", value) Then
If value = 0.7 Then
' This will be true
End If
End If
Dim str As String = ".7"
Dim test As Double = Double.Parse(str)
MessageBox.Show(test)
You can use Decimal.Parse as indicated by Plutonix as well.
I have got a text box that displays the result of two others multiplied together, before anything is in-putted the box displays NaN, is there a way to have it display "0" or even remain empty before anything multiplied.
Dim thick1 As Double
Dim tb8 As Double
Dim result As Double
thick1 = Val(thickness1.Text)
tb8 = Val(TextBox8.Text)
result = thick1 / tb8
TextBox30.Text = FormatNumber(result, 3)
^ the above code is what I am using for the text box.
Try something like this:
Dim result As Double
Dim thick1 As Double = CDbl(thickness1.Text)
Dim tb8 As Double = CDbl(TextBox8.Text)
If IsNumeric(thick1) AndAlso IsNumeric(tb8) AndAlso tb8 <> 0 Then
result = thick1 / tb8
TextBox30.Text = result.ToString("G3")
End If
Another way, try this:
TextBox30.Text = FormatNumber(result, 3).Tostring("n0")
I think it should displays a 0 even if number is not present (like a NaN string)
MSDN: The Numeric ("N") Format Specifier
If not, then try this else:
TextBox30.Text = FormatNumber(result, 3).ToString("0")
"0" Zero placeholder
Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
MSDN: Custom Numeric Format Strings
This would appear to be code in an event or form load that it is automatically running. If the textboxes do not yet have values, then result = thick1 / tb8 will result in Nan (not a Number) because you cannot divide by zero.
Again, assuming an event or something:
If tb8 = 0 then
Exit Sub
else
result = thick1 / tb8
TextBox30.Text = result.ToString("G3")
End if
In addition to dumping VAL for TryParse, consider turning Option Explicit on
My form contains several NumericUpDown controls. These controls show different amount of decimal places.
Later in my code I put the different NumericUpDown.Value's in a string array arrStr() like so:
arrStr(1) = NumericUpDown1.Value
arrStr(2) = NumericUpDown2.Value
arrStr(3) = NumericUpDown3.Value
Then I print the array with the File.WriteAllLines function to a text file.
If for example NumericUpDown1.Value = 1.00, NumericUpDown2.Value = 2.30 and NumericUpDown3.Value = 2.124 the file has the following values in it:
1
2.3
2.124
I would like to see:
1.00
2.30
2.124
I have tried Format which works, but that formatting method is not convenient as the amount of decimal places is already set for each NumericUpDown. It would be annoying to do the work again, but now with Format.
You could use String.Format to force two decimal places:
Dim value As Double = 2.3
Dim formatted = String.Format("{0:f2}", value) ' 2.30 '
Standard Numeric Format Strings
Edit: If you're array is large and you want to avoid:
arrStr(1) = String.Format("{0:f2}", NumericUpDown1.Value) up to arrStr(86) = String.Format("{0:f2}", NumericUpDown86.Value)
You can use LINQ to create the array. Assuming that your NumericUpDown controls are all in a GroupBox called NumericGroupBox. You can "inject" the correct number of decimal places:
Dim arrStr() As String =
(From n In NumericGroupBox.Controls.OfType(Of NumericUpDown)()
Select String.Format("{0:f" & n.DecimalPlaces & "}", n.Value)).ToArray()
and here's a version that searches all TabPages of your TabControl(as commented):
Dim allNumerics = From tp In Me.TabControl1.TabPages.Cast(Of TabPage)()
From n In tp.Controls.OfType(Of NumericUpDown)()
Select String.Format("{0:f" & n.DecimalPlaces & "}", n.Value)
Dim arrStr As String() = allNumerics.ToArray()