depositing numbers into a textbox and subtracting
hi i would like to know how you add and subtract numbers from a text box, i currently have 3 text boxes one which is the balance textbox, and i have a textbox to deposit and withdraw, i want to add numbers to the balance box then be able to take them out again but i cant find the right code to do it. any ideas?
Answers
Not sure if I got it right but I'll try!
Create a new project.
Put on the left side of the form a ListBox1 and underneath a Textbox1
On the right side of the form add a TextBox2 and a Button1
Now switch to CODE VIEW and replace everything in there with the code below!
The code does the following:
-
Calculates your current balance based on its actual value and what you type in to the TextBox2 when clicked on Button1
-
Substracts the double-clicked value in the ListBox1 from your balance and removes the ListBox item afterwards
Warning:
I am beginner, so, this is a very unprofessional code (but functional!)
Code BlockPublic
Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set a default balance on program start Dim balance As Integerbalance =
"100" 'TextBox1 to reflect your balance on the formTextBox1.Text = balance
'TextBox2 to add / substractTextBox2.Text =
"0" End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'When button is clicked add the text of TextBox2 to the item ListListBox1.Items.Add(TextBox2.Text)
'Calculate your balance based on curront balance in TextBox1 + value in TextBox2 Dim result As Integer Dim x As Doublex = TextBox1.Text
Dim y As Doubley = TextBox2.Text
result = (x + y)
'Update the balance in your TextBox1TextBox1.Text = result
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim result As Integer Dim x As Doublex = TextBox1.Text
Dim y As Doubley = ListBox1.SelectedItem
result = (x - y)
'Removing the selected ListBox item: Dim remove As Stringremove = ListBox1.SelectedItem
ListBox1.Items.Remove(remove)
'Updating our totalTextBox1.Text = result
End SubEnd
ClassYou can substract from your balance by typing a - in front of the number in TextBox2!
-
You'll have to convert the value in your textbox to a double
Dim val As Double
val = Double.Parse(_textbox.Text)
After that you can do what ever you want with this value
val += 1000
And set it back to the text box by converting it to a string again
_textbox.Text = val.ToString()
All Replies
Not sure if I got it right but I'll try!
Create a new project.
Put on the left side of the form a ListBox1 and underneath a Textbox1
On the right side of the form add a TextBox2 and a Button1
Now switch to CODE VIEW and replace everything in there with the code below!
The code does the following:
-
Calculates your current balance based on its actual value and what you type in to the TextBox2 when clicked on Button1
-
Substracts the double-clicked value in the ListBox1 from your balance and removes the ListBox item afterwards
Warning:
I am beginner, so, this is a very unprofessional code (but functional!)
Code BlockPublic
Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set a default balance on program start Dim balance As Integerbalance =
"100" 'TextBox1 to reflect your balance on the formTextBox1.Text = balance
'TextBox2 to add / substractTextBox2.Text =
"0" End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'When button is clicked add the text of TextBox2 to the item ListListBox1.Items.Add(TextBox2.Text)
'Calculate your balance based on curront balance in TextBox1 + value in TextBox2 Dim result As Integer Dim x As Doublex = TextBox1.Text
Dim y As Doubley = TextBox2.Text
result = (x + y)
'Update the balance in your TextBox1TextBox1.Text = result
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim result As Integer Dim x As Doublex = TextBox1.Text
Dim y As Doubley = ListBox1.SelectedItem
result = (x - y)
'Removing the selected ListBox item: Dim remove As Stringremove = ListBox1.SelectedItem
ListBox1.Items.Remove(remove)
'Updating our totalTextBox1.Text = result
End SubEnd
ClassYou can substract from your balance by typing a - in front of the number in TextBox2!
-
You'll have to convert the value in your textbox to a double
Dim val As Double
val = Double.Parse(_textbox.Text)
After that you can do what ever you want with this value
val += 1000
And set it back to the text box by converting it to a string again
_textbox.Text = val.ToString()


