Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi Im doing an invoice project and the problem i face here is - I got 4 text boxes in which i type in 2 values [like in a calculator ,where u add 3 numbers] and in the 3rd one the output is displayed. now when i type in 3 values [in the first 3 text boxes] the result displayed is the same as the previous value ie 4th box..Can someone help
Im using VB.NET 2008 ver..

If you didn't understand do inform .I'l try to make it more clear..

VB
Private Sub q1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles q1.TextChanged
Dim amt As Integer
amt = Val(d1.Text) * Val(q1.Text) * Val((un1.Text) / 30)
amt1.Text = Int(amt) Val(d1.Text)
Me.Refresh()
End Sub
Posted
Updated 28-Apr-12 4:55am
v4
Comments
OriginalGriff 28-Apr-12 9:52am    
We would need to see the relevant code fragments - it could be anything from that description!
Neo carter 28-Apr-12 10:31am    
Private Sub q1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles q1.TextChanged
Dim amt As Integer
amt = Val(d1.Text) * Val(q1.Text) * Val((un1.Text) / 30)
amt1.Text = Int(amt)
Val(d1.Text)
Me.Refresh()


End Sub
Maciej Los 28-Apr-12 10:38am    
Please, use "Improve question" widget, if you want to edit your qestion.
Neo carter 28-Apr-12 10:51am    
ok.do i have to improve my question ..is that what your trying to say..if so which part
Neo carter 28-Apr-12 10:58am    
ok thank you for that..but that's not the problem.the 3 input boxes get cleared.and i can input variables.but the problem is that in the output [ie 4th textbox] ie amt1 the value shows the same answer as the first one .No matter how many times i change the input

I "see" 4 textboxes:
d1 - input value,
q1 - input value,
un1 - input value,
amt1 - result.

To solve your problem, just add new button and move the body of q1_TextChanged() procedure into Button1_Click() procedure.
To clear textboxes, copy code below and paste it before Me.Refresh() command:
VB
d1.text = ""
q1.text = ""
un1.text = ""

Now, your textboxes are clear.


****************[EDIT]Second solution - step by step with custom Class and Interface[EDIT]****************

1) Create new (windows application) project
2) Add 4 lables and 4 texboxes and change them names as follows: TxtVal1, TxtVal2, TxtVal3, TxtVal4
3) Add 1 button (Name: BtnCalc)
4) Add new Interface[^] (menu Project->Add new item->Interface) and save it as "ITestCalc.vb". We need it to correspond with between class and form.
5) Copy and paste code below:
VB
Public Interface ITestCalc
    Function Calc() As Integer
    Function Calc(ByVal _d1 As Integer, ByVal _q1 As Integer, ByVal _un1 As Integer) As Integer
    Property D1() As Integer
    Property Q1() As Integer
    Property Un1() As Integer
End Interface

6) Add new Class[^] to the project (Menu Project->Add new item->Class) and save it as "TestCalc.vb"
7) Copy code below and paste it into your class:
VB
Public Class TestCalc
    Implements ITestCalc

    Private d1 As Integer = 0
    Private q1 As Integer = 0
    Private un1 As Integer = 0

    'default constructor
    Public Sub New()
        'do nothing
    End Sub

    'constructor with input values
    Public Sub New(ByVal _d1 As Integer, ByVal _q1 As Integer, ByVal _un1 As Integer)
        d1 = _d1
        q1 = _q1
        un1 = _un1
    End Sub


    Public Function Calc() As Integer Implements ITestCalc.Calc
        Dim retVal As Integer = 0

        retVal = d1 * q1 * (un1 / 30)

        Return retVal
    End Function

    Function Calc(ByVal _d1 As Integer, ByVal _q1 As Integer, ByVal _un1 As Integer) As Integer Implements ITestCalc.Calc
        d1 = _d1
        q1 = _q1
        un1 = _un1
        Return Calc()
    End Function

    Public Property D11() As Integer Implements ITestCalc.D1
        Get
            Return d1
        End Get
        Set(ByVal value As Integer)
            d1 = value
        End Set
    End Property

    Public Property Q11() As Integer Implements ITestCalc.Q1
        Get
            Return q1
        End Get
        Set(ByVal value As Integer)
            q1 = value
        End Set
    End Property

    Public Property Un11() As Integer Implements ITestCalc.Un1
        Get
            Return un1
        End Get
        Set(ByVal value As Integer)
            un1 = value
        End Set
    End Property

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class

8) In the form (class):
VB
Public Class Form1
    'declare variable
    Private oCalc As ITestCalc = New TestCalc()

    Private Sub BtnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalc.Click
        Try
            oCalc.D1 = Int(Me.TxtVal1.Text)
            oCalc.Q1 = Int(Me.TxtVal2.Text)
            oCalc.Un1 = Int(Me.TxtVal3.Text)
            Me.TxtResult.Text = oCalc.Calc().ToString
            'or
            'Me.TxtResult.Text = oCalc.Calc(Int(Me.TxtVal1.Text), Int(Me.TxtVal2.Text), Int(Me.TxtVal3.Text)).ToString

        Catch ex As InvalidCastException
            MsgBox(ex.Message & vbCr & vbCr & _
                "Enter correct values!", MsgBoxStyle.Exclamation, "Error!")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
        End Try
    End Sub

    Protected Overrides Sub Finalize()
        oCalc = Nothing
        MyBase.Finalize()
    End Sub
End Class


If my answer was helpful, please rate it.
 
Share this answer
 
v2
Comments
VJ Reddy 28-Apr-12 10:53am    
Good answer. 5!
Maciej Los 28-Apr-12 11:26am    
Thank you ;)
The first thing you need to do is check that you have a handler set for two textboxes, not just one - you need to handle it for d1 and d1 (and probably un1 as well).
Then change the multiplication to put the divide in the right place...dividing the Value rather than the next Text property would be a good idea!
You should also be able to remove the Me.Refresh - you shouldn't need it as the Text Property assignment should take take of it.

If that doesn't fix it, you need to use the debugger and single step. Start with a breakpoint on the "amt = ..." line, and follow though with single step to check what is happening. You should get a breakpoint each time you type into either of the text boxes.
 
Share this answer
 
Comments
Maciej Los 28-Apr-12 11:28am    
Good advise! My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900