Trying to format textbox value in a something like money - vb.net

I'm trying to show on a textbox a value like money exemple: 10.50. But everytime i put 10.50 its show 10.5
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button1.Click
textbox3.text = val(textbox1.text) + val(textbox2.text)

Use format string.
textbox3.text = (val(textbox1.text) + val(textbox2.text)).ToString("N")
I'd like to suggest Decimal.TryParse method instead of val() function.
Dim no1,no2,no3 as Decimal
Decimal.TryParse(TextBox1.Text,no1)
Decimal.TryParse(TextBox2.Text,no2)
no3= no1 + no2
TextBox3.Text= no3.ToString("N")

So why don't you just use an NumericUpDown instead?
You can set it to have two decimal places using
NumericUpDownCtrlName.DecimalPlaces = 2
To get or set the value which is a decimal simply use:
NumericUpDownCtrlName.Value

Related

dividing number without rounding it up and without decimal

I'm trying to divide input number and show the result without decimals and without rounding it up, so if I divide 80/50 I want to get 1, not 2 (1.6).
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim box1 As Decimal = TextBox1.Text()
TextBox6.Text = box1 / 50
End Sub
End Class
You can use Truncate Method of Decimal:
Dim result As Decimal = Decimal.Truncate(CDec(80 / 50))
Console.WriteLine("result : " & result.ToString)
You can use the \ operator to do integer division.
Dim result = 80 \ 50 'result is Integer with value 1

How to compare textbox value to txt file

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim linenumber0 As Integer
linenumber0 = 0
Dim mass As Double
mass = (File.ReadAllLines("225.txt").ElementAt(linenumber0).ToString)
If (Math.Abs((cDbl(TextBox1.Text) - mass < 0.5) Then
TextBox1.BackColor = Color.Green
End If
Im getting an error conversing from string to double is not valid. It is probably a simple solution but i cant see it right now
Your error occurs because the data read from the file is a String, however you are attempting to assign it to a variable declared as Double.
You can use TryParse to convert the String to Double, avoid errors and provide appropriate feedback.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim lineNumber0 As Integer
Dim mass As Double
Dim input As Double
If Double.TryParse(File.ReadAllLines("225.txt").ElementAt(linenumber0), mass) Then
If Double.TryParse(TextBox1.Text, input) AndAlso Math.Abs(input - mass) < 0.5 Then
TextBox1.BackColor = Color.Green
End If
Else
'Bad file input
End If
'...
End Sub
I think that when you set the value to mass is catching a String, so parse it with a simple CDbl, like this
mass = cDbl(File.ReadAllLines("225.txt").ElementAt(linenumber0).ToString)
I supose that with this fix it will works.
Just in case, surround it with a TRY CATCH in case what it reads is not valid

Random Band Name Generator

I'm trying to make a random band name generator in vb.net but whenever i randomly generate it shows a number rather than a word from my combo boxes
Public Class Form1
Private Sub btnMake_Click(sender As Object, e As EventArgs) Handles btnMake.Click
Randomize()
ComboBox1.Text = Int(Rnd() * ComboBox1.Items.Count)
ComboBox2.Text = Int(Rnd() * ComboBox2.Items.Count)
txtResult.Text = ComboBox1.Text + " " + ComboBox2.Text
End Sub
Private Sub btnFavourite_Click(sender As Object, e As EventArgs) Handles btnFavourite.Click
ListBox1.Items.Add(txtResult.Text)
End Sub
End Class
it should print one of the names that I've put into the combo box at random but it gives me random numbers instead
Set the SelectedIndex property instead of Text
ComboBox1.SelectedIndex = Int(Rnd() * ComboBox1.Items.Count)
Put Option Strict On at the top of your code file. You will see the compile error
Option Strict On disallows implicit conversions from 'Single' to 'String'.
on this line
ComboBox1.Text = Int(Rnd() * ComboBox1.Items.Count)
because Int() returns the integer portion of a number in the same type it was passed. So pass it a single, and it returns a single.
From Microsoft.VisualBasic metadata:
' Summary:
' Return the integer portion of a number.
' ...
Public Function Int(Number As Single) As Single
But you don't want to set the ComboBox.Text equal to a number anyway. You may want to set the ComboBox.SelectedIndex. You can try
ComboBox1.SelectedIndex = Int(Rnd() * ComboBox1.Items.Count)
but with Option Strict On, you still have type mismatch because you're trying to set an integer to a single. You could wrap Int in a CInt conversion
ComboBox1.SelectedIndex = CInt(Int(Rnd() * ComboBox1.Items.Count))
but now it's getting out of hand...
Here's the issue: you're using outdated functions, Int and Rnd. These were brought over from VB6 days and if you use VB.NET functions, it's a whole lot easier. See the Random class. Using Random, you have access to Random.Next, which actually returns an integer. Great!
Dim r As New Random()
ComboBox1.SelectedIndex = r.Next(ComboBox1.Items.Count)
ComboBox2.SelectedIndex = r.Next(ComboBox2.Items.Count)
Here is the complete code (with an added event handler to update txtResult whenever the indices are changed i.e. when the user manually changes one)
Private Sub btnMake_Click(sender As Object, e As EventArgs) Handles btnMake.Click
Dim r As New Random()
ComboBox1.SelectedIndex = r.Next(ComboBox1.Items.Count)
ComboBox2.SelectedIndex = r.Next(ComboBox2.Items.Count)
End Sub
Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
txtResult.Text = ComboBox1.Text & " " & ComboBox2.Text
End Sub
Private Sub btnFavourite_Click(sender As Object, e As EventArgs) Handles btnFavourite.Click
ListBox1.Items.Add(txtResult.Text)
End Sub

calculating program in visual basic

I trying to make program in visual basic in Text Changed event that when enter value the text box the program will store that value and when delete that value and enter new value it will use the first value and Compares with the new value if the new value > from the first value Decreases the new value from the first value (new value-first value) and if the new value < from the first value it will Decreases the first value from new value (first value - new value) and the The result another text box
Dim f As String
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text > f Then
TextBox18.Text = TextBox2.Text - f
f = (TextBox2.Text)
End If
I make this code it work but when the new value is (10) or Larger its not working
Try the following:
Dim f As String
Private Sub TextBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Leave
If not string.isnullOrEmpty(TextBox2.Text) Then
If CDbl(TextBox2.Text) > CDbl(f) Then
TextBox18.Text = CStr(CDbl(TextBox2.Text) - CDbl(f))
f = TextBox2.Text
End If
End If
End Sub

How to get Value in dynamic generated Textbox

I'm new to Visual Basic.
I have a little program look like this.
https://www.dropbox.com/s/xr44pxp3n79atkk/wall.png
It will calculate the total area by adding up all wall area.
Public Sub btnWallAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWallAdd.Click
FlowLayoutPanel1.Controls.Clear()
FlowLayoutPanel1.AutoScroll = True
For i As Integer = 1 To Val(txtWallNo.Text)
Dim Width As New TextBox()
Dim Height As New TextBox()
Width.Name = "Width" & i
Width.Text = Width.Name
Height.Name = "Height" & i
Height.Text = Height.Name
FlowLayoutPanel1.Controls.Add(Width)
FlowLayoutPanel1.Controls.Add(Height)
Next
End Sub
I have successfully create dynamic textbox base on the number entered by user but I don't know how to get values from those textbox an add them up. Please teach me how do it. Thank you very much!
Sorry for my English!
You can access the controls by name, like this:
Dim txtWidth As TextBox = FlowLayoutPanel1.Controls.Item("Width" & i)
Or, if you have option strict turned on, you need to be explicit about the type conversions:
Dim txtWidth As TextBox = CType(FlowLayoutPanel1.Controls.Item("Width" & i.ToString()), TextBox)
For instance, to add up the area on all the walls, you could do something like this:
Public Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim totalArea As Decimal = 0
For i As Integer = 1 To Val(txtWallNo.Text)
Dim txtWidth As TextBox = CType(FlowLayoutPanel1.Controls.Item("Width" & i.ToString()), TextBox)
Dim txtHeight As TextBox = CType(FlowLayoutPanel1.Controls.Item("Height" & i.ToString()), TextBox)
totalArea = totalArea + (Decimal.Parse(txtHeight.Text) * Decimal.Parse(txtWidth.Text))
Next
lblResult.Text = totalArea.ToString()
End Sub