Results 1 to 8 of 8

Thread: [RESOLVED] How to add a value in same text box ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Resolved [RESOLVED] How to add a value in same text box ?

    i made two text boxes and a button in windows form application VS2010

    in that i enter a value ex: "24" in first text box

    when i press the button the ouput should come as 2+4 ="6" i.e 6 should come as out put in second text box how can i do that .please tell me the logic in code format . i tried this but stuck in implementing logic
    Code:
    Public Class Form1
    
    
        Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click
    
    Dim ftn as integer
    dim stn as integer
    ftn = TextBox1.Text
    stn= val(ftn)
    stn = TextBox2.Text
      End Sub
    End Class
    please tell me the code !!!!
    Last edited by charuwaka; Dec 27th, 2012 at 09:15 AM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to add a value in same text box ?

    Is the intention to add all the digits of the number whatever its size? So 23456 would be 16, for example? In that case you need to split the text into single digits.

    vb.net Code:
    1. Dim i As Integer = 0
    2.         For Each digit In TextBox1.Text 'takes advantage of nature of string as char array
    3.             i += Val(digit)
    4.         Next
    5.         TextBox2.Text = i.ToString
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: How to add a value in same text box ?

    Quote Originally Posted by charuwaka View Post
    i made two text boxes and a button in windows form application VS2010

    in that i enter a value ex: "24" in first text box

    when i press the button the ouput should come as 2+4 ="6" i.e 6 should come as out put in second text box how can i do that .please tell me the logic in code format . i tried this but stuck in implementing logic
    Code:
    Public Class Form1
    
    
        Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click
    
    Dim ftn as integer
    dim stn as integer
    ftn = TextBox1.Text
    stn= val(ftn)
    stn = TextBox2.Text
      End Sub
    End Class
    please tell me the code !!!!
    And what would you do if the user entered 124, do you splut to 12+4 or 1+24. Think past smaller size values.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: How to add a value in same text box ?

    try this:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim value As Integer
            If Integer.TryParse(TextBox1.Text, value) Then
                Dim numbers() As Integer = Array.ConvertAll(TextBox1.Text.ToArray, Function(c) CInt(c.ToString))
                Label1.Text = String.Join("+", Array.ConvertAll(numbers, Function(x) x.ToString)) & "=" & numbers.Sum.ToString
            Else
                Label1.Text = ""
            End If
        End Sub
    
    End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to add a value in same text box ?

    see if user inputs values like 99 then 9+9=18 and 123 then 1+2+3=6 like this anything enters it is converted pls tel the code now

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to add a value in same text box ?

    Not like that when i enter 16 then it should come as 1+6=7 and 256 then 2+5+6=13 like that out put should come

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Re: How to add a value in same text box ?

    Thank You Very much my problem solved

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How to add a value in same text box ?

    Quote Originally Posted by charuwaka View Post
    Thank You Very much my problem solved
    Was it solved using the code paul provided? Do you understand what HIS code does?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width