Go Premium for a chance to win a PS4. Enter to Win


Easy: Windows Form- Add two or more fileds

Posted on 2004-10-22
Medium Priority
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

Accepted Solution

karthikeyanTP earned 200 total points
ID: 12387211
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


Author Comment

ID: 12448544
hi Kar,

thanks for the code.....

Enjoy the points...


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2

916 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