Click here to Skip to main content
15,072,391 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.
Richard MacCutchan 3-Aug-21 5:13am
   
There is a dropdown at the top right of the page where you can select VB as the language.
Beginner213456 3-Aug-21 21:08pm
   
These are the examples in the Double.TryParse
My problem is i dont know how to insert it in my code

like the "console.writeline" are these like 'msgbox.show'?
where can i save the result


Dim values() As String = {"1,643.57", "$1,643.57", "-1.643e6",
"-168934617882109132", "123AE6",
Nothing, String.Empty, "ABCDEF"}
Dim number As Double

For Each value In values
If Double.TryParse(value, number) Then
Console.WriteLine("'{0}' --> {1}", value, number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
Next

Richard MacCutchan 4-Aug-21 4:07am
   
You do not use Console methods in a WinForm application. You need something like:
Dim multiplier As Double
Dim multiplicand As Double
Dim result As Double
If Not Double.TryParse(TextBox1.Text, multiplier) Then
  MessageBox.Show("TextBox1 is invalid")
Else
  If Not Double.TryParse(TextBox2.Text, multiplicand) Then
    MessageBox.Show("TextBox2 is invalid")
  Else
    result = multiplier * multiplicand
    TextBox3.Text = result.ToString("N2")
  End If
End If
Beginner213456 4-Aug-21 21:02pm
   
Dim a as double
dim b as double
dim result1 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
<pre>



Question: It works to compute the sum of the textboxes but when i click on the save button, "INVALID" pops up.
KM,KF,KT in my database has a Data Type as Number
Richard MacCutchan 5-Aug-21 3:05am
   
I do not see any code connected to a SAVE button.

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!
   
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