Visual Basic > Visual Basic Forums > Visual Basic General > depositing numbers into a textbox and subtracting
Ask a questionAsk a question
 

Answerdepositing numbers into a textbox and subtracting

  • Wednesday, January 09, 2008 7:10 PMJJordan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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

  • Thursday, January 10, 2008 10:02 AMEarl Tut Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Block

    Public 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 Integer

    balance = "100"

    'TextBox1 to reflect your balance on the form

    TextBox1.Text = balance

    'TextBox2 to add / substract

    TextBox2.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 List

    ListBox1.Items.Add(TextBox2.Text)

    'Calculate your balance based on curront balance in TextBox1 + value in TextBox2

    Dim result As Integer

    Dim x As Double

    x = TextBox1.Text

    Dim y As Double

    y = TextBox2.Text

    result = (x + y)

    'Update the balance in your TextBox1

    TextBox1.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 Double

    x = TextBox1.Text

    Dim y As Double

    y = ListBox1.SelectedItem

    result = (x - y)

    'Removing the selected ListBox item:

    Dim remove As String

    remove = ListBox1.SelectedItem

    ListBox1.Items.Remove(remove)

    'Updating our total

    TextBox1.Text = result

    End Sub

    End Class

     

     

    You can substract from your balance by typing a - in front of the number in TextBox2!

  • Thursday, January 10, 2008 2:51 PMThE_lOtUs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Thursday, January 10, 2008 10:02 AMEarl Tut Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Block

    Public 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 Integer

    balance = "100"

    'TextBox1 to reflect your balance on the form

    TextBox1.Text = balance

    'TextBox2 to add / substract

    TextBox2.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 List

    ListBox1.Items.Add(TextBox2.Text)

    'Calculate your balance based on curront balance in TextBox1 + value in TextBox2

    Dim result As Integer

    Dim x As Double

    x = TextBox1.Text

    Dim y As Double

    y = TextBox2.Text

    result = (x + y)

    'Update the balance in your TextBox1

    TextBox1.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 Double

    x = TextBox1.Text

    Dim y As Double

    y = ListBox1.SelectedItem

    result = (x - y)

    'Removing the selected ListBox item:

    Dim remove As String

    remove = ListBox1.SelectedItem

    ListBox1.Items.Remove(remove)

    'Updating our total

    TextBox1.Text = result

    End Sub

    End Class

     

     

    You can substract from your balance by typing a - in front of the number in TextBox2!

  • Thursday, January 10, 2008 2:51 PMThE_lOtUs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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()