none
Adding two textboxes with old value in vb.net

    Question

  • Dear All

    I am facing one issue while adding two textboxes

    one textbox is my stock inHAND and other is new STOCK

    now my stock inhand is showing total available stock = 10 

    when i enter new stock like 10 it should show me 20 but instead of 20 it giving me 22 and when i remove new stock, stock inhand remain 20 and if i enter new stock again it make it 30 instead of 20....

    available stock =10

    new stock is = 10

    available stock should give 20 but it giving 22

    and if i remove new stock and make it 50 then it will make it 72 instead of 60

    please guide 

    Monday, February 4, 2019 8:03 AM

Answers

All replies

  • Hi,
    I'm afraid you need to your source code.
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Monday, February 4, 2019 8:54 AM
  • thank you for your reply

    actually i am using a simple logic in adding two textboxes

    dim a as integer

    dim b as integer

    dim c as integer

    dim sum as integer

    a=Val(textbox1.text)

    b=val(textbox2.text)

    sum=a+b

    textbox3.text=sum

    with this code i can able to add two value but it giving me wrong sum suppose i add 10+10 so instead of 20 this code is giving me 22 

    but i need something like a code that increment the available stock 

    Monday, February 4, 2019 9:15 AM
  • Hi,
    I guess it is a school assignment, so I can't provide code.
    Instead, I recommend to use CInt(textbox.text). 
    Or use Integer.Parse(textbox.text)
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html


    • Edited by Ashidacchi Monday, February 4, 2019 9:32 AM
    Monday, February 4, 2019 9:26 AM
  • no my friend this is not school assignment.

    i am working on a project inventory management system for my company and i am stuck at this point. i am network engineer. 

    please provide me codes that will be your great help

    Monday, February 4, 2019 10:13 AM
  • Hi

    As Ashidacchi mentioned, use a better way to deal with string to numeric conversion.

    I cannot reproduce the results you say you get, but I can get exceptions using your example.

    Here is a different way to get what you want. The only difference is to use a Function. The function is useful as you can use anywhere you need to convert a String to an Integer. (Types Double, Decimal etc are also available)

    ' Form1 with TextBox1, TextBox2,
    ' TextBox3 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As Integer
        Dim b As Integer
        Dim sum As Integer
        a = GetInteger(TextBox1.Text)
        b = GetInteger(TextBox2.Text)
        sum = a + b
        TextBox3.Text = sum.ToString
      End Sub
      Function GetInteger(s As String) As Integer
        Dim i As Integer = 0
        If Integer.TryParse(s, i) Then Return i
        Return 0
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Monday, February 4, 2019 10:28 AM
  • Hi Omi4u,

    I've made a sample.
       
    Code:
    	Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) _
    		Handles TextBox1.TextChanged, TextBox2.TextChanged
    		' ---
    		If (Me.TextBox1.Text <> "") AndAlso (Me.TextBox2.Text <> "") Then
    			Dim a As Integer
    			Dim b As Integer
    			a = CInt(TextBox1.Text)
    			b = CInt(TextBox2.Text)
    			TextBox3.Text = (a + b).ToString
    		End If
    	End Sub
    the simplest(?):
    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) _
    	Handles TextBox1.TextChanged, TextBox2.TextChanged
    	' ---
    	If (Me.TextBox1.Text <> "") AndAlso (Me.TextBox2.Text <> "") Then
    		TextBox3.Text = (CInt(TextBox1.Text) + CInt(TextBox2.Text)).ToString
    	End If
    End Sub
    Regars,


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html


    • Edited by Ashidacchi Monday, February 4, 2019 10:48 AM
    Monday, February 4, 2019 10:47 AM
  • Hello,

    If this is truly for a app than the basics would be to have a method for adding and subtracting where the basics are shown below. The top TextBox is read-only as adjustments should be made only with the two TextBox controls and their buttons for add and subtract.

    Public Class Form2
        Private StockQuantity As Integer
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            StockQuantity = 10
            currentStockTextBox.Text = StockQuantity.ToString()
        End Sub
        Private Sub subtractStockButton_Click(sender As Object, e As EventArgs) _
            Handles subtractStockButton.Click
    
            Dim value As Integer = 0
    
            If Integer.TryParse(subtractStockTextBox.Text, value) Then
                StockQuantity -= value
                currentStockTextBox.Text = StockQuantity.ToString()
            End If
        End Sub
        Private Sub addStockButton_Click(sender As Object, e As EventArgs) _
            Handles addStockButton.Click
    
            Dim value As Integer = 0
    
            If Integer.TryParse(addStockTextBox.Text, value) Then
                StockQuantity += value
                currentStockTextBox.Text = StockQuantity.ToString()
            End If
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 4, 2019 11:31 AM
    Moderator
  • hi Les, Livingston, Scotland

    thank you for your code . i tried it but it showing me +1 value 

    means

    right now my stock in hand is 100 and if i add new stock suppose 10 it giving me 111 value instead of 110

    Monday, February 4, 2019 12:54 PM
  • Thank you for your code karen

    but i want to use textbox change event and when i tried your code it adding new stock because i am using your code in new_stock_change event 

    but when i backspace it, it keep the added stock value in available stock instead of showing the old till i save it..

    Monday, February 4, 2019 1:02 PM
  • hi Les, Livingston, Scotland

    thank you for your code . i tried it but it showing me +1 value 

    means

    right now my stock in hand is 100 and if i add new stock suppose 10 it giving me 111 value instead of 110

    Hi

    The code I posted CAN NOT produce the result you say it does!

    You must have other code doing something. You say in a reply you want to use the TextBox TextChange event - did you have that event active when you said you tried my code - could it be adding something?

    Here is my code amended to use the TextBox TextChanged event(s) and I assure you it DEOSN'T add spurious values to the result.

    ' Form1 with TextBox1, TextBox2,
    ' and TextBox3
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged, TextBox1.TextChanged
        Dim a As Integer
        Dim b As Integer
        Dim sum As Integer
        a = GetInteger(TextBox1.Text)
        b = GetInteger(TextBox2.Text)
        sum = a + b
        TextBox3.Text = sum.ToString
      End Sub
      Function GetInteger(s As String) As Integer
        Dim i As Integer = 0
        If Integer.TryParse(s, i) Then Return i
        Return 0
      End Function
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Monday, February 4, 2019 1:16 PM
    Monday, February 4, 2019 1:14 PM
  • hi sir 

    your code is working fine but i want something like below code

     Dim a As Integer
            Dim b As Integer
            Dim sum As Integer
            a = GetInteger(TextBox1.Text)
            b = GetInteger(TextBox2.Text)
            sum = a + b
            TextBox1.Text = sum.ToString

    i want to add additional new stock to available stock instead of textbox3..

    i want to add additional stock in textbox 1 

    your code is working if i add new stock and output result in textbox 3

    Monday, February 4, 2019 1:40 PM
  • hi sir 

    your code is working fine but i want something like below code

     Dim a As Integer
            Dim b As Integer
            Dim sum As Integer
            a = GetInteger(TextBox1.Text)
            b = GetInteger(TextBox2.Text)
            sum = a + b
            TextBox1.Text = sum.ToString

    i want to add additional new stock to available stock instead of textbox3..

    i want to add additional stock in textbox 1 

    your code is working if i add new stock and output result in textbox 3

    Hi

    That makes no sense at all, as well as causing exceptions.

    For example, there is 10 in TextBox1. The code noriced the TextChanged in the TextBox and immediately jumps to the TextChanged event handler - where, the Text is again changed and so immediately jumps to the TextChanged event handler - where, the Text is again changed and so immediately jumps to the TextChanged event handler - where, the Text is again changed and so immediately jumps to theTextChanged event handler - where, the Text is again changed and so immediately jumps to theTextChanged event handler - where, the Text is again changed and so immediately jumps to theTextChanged event handler - where, the Text is again changed and so immediately jumps to the ............

    can you see where this is going?


    Regards Les, Livingston, Scotland

    Monday, February 4, 2019 1:50 PM
  • This will work.

    Note the subtraction part.

    Public Class Form1
        Private StockQuantity As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            StockQuantity = 10
            currentStockTextBox.Text = StockQuantity.ToString()
    
        End Sub
    
        Private Sub changeTextBox_TextChanged(sender As Object, e As EventArgs) _
            Handles changeTextBox.TextChanged
    
            Dim value As Integer = 0
    
            If Integer.TryParse(changeTextBox.Text, value) Then
                If value < 0 Then
                    StockQuantity -= (value * -1)
                Else
                    StockQuantity += value
                End If
    
                currentStockTextBox.Text = StockQuantity.ToString()
    
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 4, 2019 2:33 PM
    Moderator
  • Please check the attachment to know about my issue

    https://www.dropbox.com/s/ptzpiu287ivq65w/example%20form.rar?dl=0

    if you add new stock as 10 it will increment the available stock to 66 instead of 55 which is available stock. also while entering you ease 10 then available stock will show 67 instead of 55.


    • Edited by Omi4u Monday, February 4, 2019 4:39 PM
    Monday, February 4, 2019 4:37 PM
  • May this help you

    Try

    Public Class Form1
    
        Private StockQuantity As Integer
        Private TempNewStockQuantity As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            StockQuantity = Val(TextBox1.Text)
            TextBox1.Text = StockQuantity.ToString()
        End Sub
        Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
            TempNewStockQuantity = Val(TextBox2.Text)
        End Sub
    
        Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
            TextBox1.Text += (Val(TextBox2.Text) - TempNewStockQuantity)
            TempNewStockQuantity = Val(TextBox2.Text)
        End Sub
    End Class

    Monday, February 4, 2019 6:45 PM
  • This will work.
         
    Private StockQuantity As Integer
    ' ---
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    	Handles MyBase.Load
    	StockQuantity = TextBox1.Text
    End Sub
    ' --- key [Enter] > [Tab]
    Private Sub TextBox2_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) _
    	Handles TextBox2.PreviewKeyDown
    	If (e.KeyCode = Keys.Enter) Then
    		SendKeys.Send("{TAB}")
    	End If
    End Sub
    ' ---
    Private Sub TextBox2_Leave(sender As Object, e As EventArgs) _
    	Handles TextBox2.Leave
    	Dim value As Integer
    	' ---
    	If (Integer.TryParse(TextBox2.Text, value)) Then
    	    StockQuantity += value
    		TextBox1.Text = StockQuantity.ToString()
    		TextBox2.Select()
    	End If
    End Sub
    
    


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Monday, February 4, 2019 10:45 PM
  • Please check the attachment to know about my issue

    https://www.dropbox.com/s/ptzpiu287ivq65w/example%20form.rar?dl=0

    if you add new stock as 10 it will increment the available stock to 66 instead of 55 which is available stock. also while entering you ease 10 then available stock will show 67 instead of 55.


    DO NOT use DroptBox period, use Microsoft Onedrive which you sign into with the same login as this forum.

    In any event try the following and don't use TextChanged.

    Public Class Form1
        Private StockQuantity As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            StockQuantity = 10
            currentStockTextBox.Text = StockQuantity.ToString()
    
        End Sub
        Private Sub changeTextBox_LostFocus(sender As Object, e As EventArgs) _
            Handles changeTextBox.LostFocus
    
            Dim value As Integer = 0
    
            If Integer.TryParse(changeTextBox.Text, value) Then
                If value < 0 Then
                    StockQuantity -= (value * -1)
                Else
                    StockQuantity += value
                End If
    
                currentStockTextBox.Text = StockQuantity.ToString()
    
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 5, 2019 12:48 AM
    Moderator
  • hi ma'am thank you for your codes but i am getting error while running test application 

    Conversion from string "" to type 'Integer' is not valid.

    codes are that i tried 

    Public Class Form1
    
        Private StockQuantity As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            StockQuantity = TextBox1.Text
    
            TextBox2.Text = StockQuantity
    
        End Sub
    
        Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
            Dim value As Integer = 0
    
            If Integer.TryParse(changeTextBox.Text, value) Then
                If value < 0 Then
                    StockQuantity -= (value * -1)
                Else
                    StockQuantity += value
                End If
    
                TextBox2.Text = StockQuantity.ToString()
    
            End If
        End Sub
    
        Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    
        End Sub
    End Class
    


    Friday, February 8, 2019 6:02 AM
  • Hi,

    TextBox1.Text=""

    ""  convert to integer ,error

    try the code:

            StockQuantity = If(TextBox1.Text = "", 0, Convert.ToInt32(TextBox1.Text))
    

    Best Reagrds,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 8, 2019 7:19 AM
    Moderator
  • not working
    Monday, February 11, 2019 9:08 AM
  • Hi,

    Does this still return an error?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 11, 2019 9:12 AM
    Moderator
  • yes and it not adding the stock the way i want
    Monday, February 11, 2019 10:39 AM
  • hi ma'am thank you for your codes but i am getting error while running test application 

    Conversion from string "" to type 'Integer' is not valid.

    codes are that i tried 

    Public Class Form1
    
        Private StockQuantity As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            StockQuantity = TextBox1.Text
    
            TextBox2.Text = StockQuantity
    
        End Sub
    
        Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
            Dim value As Integer = 0
    
            If Integer.TryParse(changeTextBox.Text, value) Then
                If value < 0 Then
                    StockQuantity -= (value * -1)
                Else
                    StockQuantity += value
                End If
    
                TextBox2.Text = StockQuantity.ToString()
    
            End If
        End Sub
    
        Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    
        End Sub
    End Class


    That is very strange, what line is the error on?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 11, 2019 1:46 PM
    Moderator
  • i am using another method now 

    • Marked as answer by Omi4u Friday, February 15, 2019 5:10 AM
    Friday, February 15, 2019 5:10 AM