Click here to Skip to main content
15,887,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Still Learning in Vb.net and MS access

How to compute 2 or more textboxes and save it in 1 textbox
and Show the result without button click

I tried to insert the code in the timer

Question: is my code fine or are there more ways to compute textbox values

What I have tried:

Dim A As Double
Dim B As Double
 If Not Double.TryParse(KMTextBox.Text, A) Then
            MessageBox.Show("INVALID")
        Else
            If Not Double.TryParse(KFTextBox.Text, B) Then
                MessageBox.Show("INVALID")
            Else
                result1 = A + B
                KTTextBox.Text = result1.ToString("")
            End If
        End If
Posted
Updated 4-Aug-21 14:53pm
v2
Comments
Richard MacCutchan 3-Aug-21 3:17am    
You need some way to check that all the TextBoxes have been filled in completely. Alternatively, set up an event handler that gets fired every time a TextBox gets updated. You can then do the calculation, even if the user has not finished adding text.
Beginner213456 3-Aug-21 4:31am    
can you please drop how some examples on how to do it.
I have read about textbox.change but i cant figure out how to code it in vb10
Beginner213456 3-Aug-21 4:34am    
I Have also Tried This, but its only for 2 textboxes

\\If IsNumeric(KMTextBox.Text) And IsNumeric(KFTextBox.Text) Then
' Dim num1 As Double
' Dim num2 As Double
' Dim num3 As Double
' num1 = Convert.ToDouble(KMTextBox.Text)
' num2 = Convert.ToDouble(KFTextBox.Text)
' num3 = num1 + num2
' KTTextBox.Text = CStr(num3)
'End If\\
Richard MacCutchan 3-Aug-21 4:40am    
See TextBox Class (System.Windows.Controls) | Microsoft Docs[^]. Also do not use Convert.ToDouble as it will crash your application if you have garbage in the textbox. Use Double.TryParse Method (System) | Microsoft Docs[^].
Beginner213456 3-Aug-21 5:00am    
I have read about the double.tryparse but its in C#, i dont know to apply it in my code.

1 solution

Why use a timer?
Instead, consider using the TextBox.TextChanged event[^] and update your result only when there is something to do?

And do yourself a favour: stop using outdated VB constructs and start using .NET properly - instead of CDbl use double.TryParse[^] and suchlike instead - then you can let the user know when he has mistyped ... You can replace most of that code with a single call!
 
Share this answer
 
Comments
CPallini 3-Aug-21 3:23am    
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