Easy: Windows Form- Add two or more fileds

Posted on 2004-10-22
Last Modified: 2010-04-23

This is a very easy one, I have done it on MS Access but can't do it in VB.NET.... (it has no database, only windows form)

On a windows form I have two or more text fields (txt1, txt2, txt3 ....etc) and I have another text field call (txtAnswer).

I need to add the values entered on the text fields to the "txtAnswer" field, but I want the sum as I keep on entering value to each field.

 I also need to be able to only enter numbers on the text field.

Any help?
Question by:nibirkhan
    LVL 2

    Accepted Solution

    Hai nibirkhan,

    Here is the code.

    'declare total to sum up. I am assuming total as double

    Private total As Double

    'there are three text boxes named TextBox1, TextBox2 and TextBox3.
    'this function is invoked whenever text user changes a value in TextBox 1 and 2. TextBox3 finds the
    'total and display it

        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
            Dim a, b As Double
            If TextBox2.Text = "" Then
                a = 0
                a = Convert.ToDouble(TextBox2.Text)
            End If
            If TextBox1.Text = "" Then
                b = 0
                b = Convert.ToDouble(TextBox1.Text)
            End If
            TextBox3.Text = Convert.ToDouble(a + b)
        End Sub

        'This function restricts user to type digits and decimal point.

        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress
            'Restrict user to key numeric values, decimal point and control keys
            If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) _
                    And Not e.KeyChar = "." Then
                e.Handled = True
                ' allow only one decimal point in textbox
                If e.KeyChar = "." And CType(sender, Control).Text.IndexOf(".") <> -1 Then
                    e.Handled = True
                End If
            End If
        End Sub

    LVL 2

    Author Comment

    hi Kar,

    thanks for the code.....

    Enjoy the points...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    860 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now