another textbox question.

lOnEr

Member
Joined
Jun 18, 2004
Messages
14
Programming Experience
Beginner
how to add a value in the textbox..
for example: when the user input 6 + 6 and press enter
it gives a value of 12...


tanx in advance..
 
Adding values to TextBox

If the TextBox contains "6+6" and you want to see "12" then you should parse the Text property of the control. Try this way:

VB.NET:
  [size=2][color=#800080]
Private[/color][/size][size=2] [/size][size=2][color=#800080]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#800080]ByVal[/color][/size][size=2] sender [/size][size=2][color=#800080]As[/color][/size][size=2] System.Object, [/size][size=2][color=#800080]ByVal[/color][/size][size=2] e [/size][size=2][color=#800080]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#800080]Handles[/color][/size][size=2] Button1.Click

[/size][size=2][color=#800080]	 Dim[/color][/size][size=2] op1, op2 [/size][size=2][color=#800080]As[/color][/size][size=2] [/size][size=2][color=#800080]Integer[/color][/size]
[color=#800080]	 [size=2]Dim[/size][/color][size=2] position [/size][size=2][color=#800080]As[/color][/size][size=2] [/size][size=2][color=#800080]Integer[/color][/size]
[size=2][color=#800080] 
[/color][/size][size=2]	 position = TextBox1.Text.IndexOf([/size][size=2][color=#ff0000]"+"[/color][/size][size=2])[/size]
[size=2]	 op1 = [/size][size=2][color=#800080]CInt[/color][/size][size=2](TextBox1.Text.Substring([/size][size=2][color=#0000ff]0[/color][/size][size=2], position))[/size]
[size=2]	 op2 = [/size][size=2][color=#800080]CInt[/color][/size][size=2](TextBox1.Text.Substring(position + [/size][size=2][color=#0000ff]1[/color][/size][size=2]))
	 TextBox1.Text = op1 + op2
[/size][size=2][color=#800080]End[/color][/size][size=2] [/size][size=2][color=#800080]Sub
[/color][/size]
 
Jonatan said:
If the TextBox contains "6+6" and you want to see "12" then you should parse the Text property of the control. Try this way:

VB.NET:
   [size=2][color=#800080]
 Private[/color][/size][size=2][color=#800080]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#800080]ByVal[/color][/size][size=2] sender [/size][size=2][color=#800080]As[/color][/size][size=2] System.Object, [/size][size=2][color=#800080]ByVal[/color][/size][size=2] e [/size][size=2][color=#800080]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#800080]Handles[/color][/size][size=2] Button1.Click
 
 [/size][size=2][color=#800080]	 Dim[/color][/size][size=2] op1, op2 [/size][size=2][color=#800080]As[/color][/size][size=2][color=#800080]Integer[/color][/size]
 [color=#800080]	 [size=2]Dim[/size][/color][size=2] position [/size][size=2][color=#800080]As[/color][/size][size=2][color=#800080]Integer[/color][/size]
 [size=2][color=#800080] 
 [/color][/size][size=2]	 position = TextBox1.Text.IndexOf([/size][size=2][color=#ff0000]"+"[/color][/size][size=2])[/size]
 [size=2]	 op1 = [/size][size=2][color=#800080]CInt[/color][/size][size=2](TextBox1.Text.Substring([/size][size=2][color=#0000ff]0[/color][/size][size=2], position))[/size]
 [size=2]	 op2 = [/size][size=2][color=#800080]CInt[/color][/size][size=2](TextBox1.Text.Substring(position + [/size][size=2][color=#0000ff]1[/color][/size][size=2]))
 	 TextBox1.Text = op1 + op2
 [/size][size=2][color=#800080]End[/color][/size][size=2][color=#800080]Sub
 [/color][/size]
tanx jonatan for your effort..
it only validate an addition.
i use regex.
heres the code: courtesy of brown monkey from Vbforums
VB.NET:
 	Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
 		If e.KeyCode = Keys.Enter Then
 			Dim rg As New Regex("\d+\s*(\+|\/|\*|\-)\s*\d+")
 			If Not rg.Match(TextBox1.Text).Success Then
 				MsgBox("invalid")
 			Else
 			    Dim r() As String = Regex.Split(TextBox1.Text, "\s*[+,/,*,-]\s*")
 			    If (Regex.Match(TextBox1.Text, "\s*\+\s*")).Success Then
 				    TextBox1.Text = Integer.Parse(r(0)) + Integer.Parse(r(1))
 			    ElseIf (Regex.Match(TextBox1.Text, "\s*\*\s*")).Success Then
 				    TextBox1.Text = Integer.Parse(r(0)) + Integer.Parse(r(1))
 			    ElseIf (Regex.Match(TextBox1.Text, "\s\/\s*")).Success Then
 				    TextBox1.Text = Integer.Parse(r(0)) + Integer.Parse(r(1))
 			    ElseIf (Regex.Match(TextBox1.Text, "\s*\-\s*")).Success Then
 				    TextBox1.Text = Integer.Parse(r(0)) + Integer.Parse(r(1))
 				End If
 			End If
 		End If
 end sub
 
Back
Top