How to Math Operation at one textbox in VB>Net

Frangky Bunga 41 Reputation points
2023-04-14T11:23:49.56+00:00

Dear All, if I input a number and plus sign in a textbox. and when I click Button1, the Result Textbox will display the sum = 25 User's image

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,743 questions
{count} votes

Accepted answer
  1. Dewayne Basnett 1,366 Reputation points
    2023-04-14T13:42:15.3166667+00:00

    This will get you started

        Dim dt As New DataTable
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim result As Double = CDbl(dt.Compute(textBoxFormula.Text, Nothing))
            textBoxResult.Text = result.ToString
            'Stop
        End Sub
    
    

    If you want to parse the string then some reading, a lot of reading... https://www.google.com/search?client=firefox-b-1-d&q=shunting+yard+algorithm

    2 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2023-04-14T14:00:55.81+00:00

    Hi An alternative. Not a trivial question despite what you may think - it gets very difficult very soon. Here is a stand alone example (with NO error checking), which tries to handle a simple + or - function with 2 numbers only. It is quite messy, but then that is part of the difficulty for this type of coding (can be tidied up though) If you want to try this, start a new test project, add TextBox1, TextBox2 and Button1 to Form1 then copy/replace all Form1 default code with this code.

    ' Form1 with TextBox1 and TextBox2
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		Dim s As String = TextBox1.Text
    
    		Dim p1 As Integer = part(Trim(s))
    
    		s = s.Remove(0, p1.ToString.Length)
    
    		Dim op As String = s(0)
    
    		s = s.Substring(1, s.Length - 1)
    
    		Dim p2 As Integer = part(Trim(s))
    
    		Select Case op
    			Case "+"
    				TextBox2.Text = (p1 + p2).ToString
    			Case "-"
    				TextBox2.Text = (p1 - p2).ToString
    		End Select
    	End Sub
    	Function part(s As String) As Integer
    		Dim v As Integer = 0
    		Dim x As Integer = 0
    		For i As Integer = 1 To s.Length
    			Dim s2 As String = s.Substring(0, i)
    			Integer.TryParse(s2, v)
    			If v = 0 Then Exit For
    			x = v
    		Next
    		Return x
    	End Function
    End Class
    
    0 comments No comments

  2. Castorix31 86,046 Reputation points
    2023-04-14T14:50:21.3233333+00:00

    A way is to use MS ScriptControl with VBScript. Test with TextBox1 & TextBox2 :

      Dim objScriptControl = Activator.CreateInstance(Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC")))
                objScriptControl.Language = "VBScript"
                Dim objResult As Object = objScriptControl.Eval(TextBox1.Text)
                TextBox2.Text = objResult.ToString()
    
    0 comments No comments

  3. Frangky Bunga 41 Reputation points
    2023-05-09T04:13:29.3933333+00:00

    Hi All,

    Thank you alot for your answer very helpfull..

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.