What's wrong with this code

chewy

Member
Joined
Oct 1, 2003
Messages
7
Ok. I am just begining and I am having troubles with this little bit of code. I have three text boxes. textbox1,textbox2 textbox3. And I have one button. The object is when I click the button textbox3 = textbox1 + textbox2. Here is what I have for code and I tried it many different ways and none of them work.

Dim textbox1 as integer
Dim textbox2 as integer
Dim textbox3 as integer

textbox3 = textbox1 + textbox2

I have also tried
textbox3 = "textbox1" + "textbox2"

I know its a dumb simple question but it is how I learn. And I am just starting out. Thanks for the help.
Chewy
 
Here is the most complete code you can have...

Enjoy...


[VB]
Public Class Form7
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

This call is required by the Windows Form Designer.
InitializeComponent()

Add any initialization after the InitializeComponent() call

End Sub

Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()

TextBox1

Me.TextBox1.Location = New System.Drawing.Point(16, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes
Me.TextBox1.Size = New System.Drawing.Size(152, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""

TextBox2

Me.TextBox2.Location = New System.Drawing.Point(16, 48)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.RightToLeft = System.Windows.Forms.RightToLeft.Yes
Me.TextBox2.Size = New System.Drawing.Size(152, 20)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = ""

TextBox3

Me.TextBox3.Location = New System.Drawing.Point(16, 80)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.ReadOnly = True
Me.TextBox3.RightToLeft = System.Windows.Forms.RightToLeft.Yes
Me.TextBox3.Size = New System.Drawing.Size(152, 20)
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = ""

Button1

Me.Button1.Location = New System.Drawing.Point(184, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 88)
Me.Button1.TabIndex = 3
Me.Button1.Text = "Do it"

Form7

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(288, 118)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Name = "Form7"
Me.Text = "Form7"
Me.ResumeLayout(False)

End Sub

#End Region

First of all...
This is ment to be a number operation... so no letters or other chars
not number are alowed ... right?
This is donne this way...
I dont know if your regional settings will allow this right!
If your decimal separator is the . (dot) change the , (coma) to a dot
Private Sub TextBoxes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress, TextBox2.KeyPress

If (e.KeyChar < "0" Or e.KeyChar > "9") _
And (e.KeyChar <> ",") Then e.Handled = True

End Sub

Now its just a matter of mathematics...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TextBox3.Text = CType(Me.TextBox1.Text, Double) + CType(Me.TextBox2.Text, Double)
End Sub

End Class

[/VB]
 
I dont really understand what your trying to do. But if it is adding try this.
Add under button1_click
Dim sum As Double
sum = CDBL(txtFirstNum.Text) + CDbl(txtSecondNum.Text)
TxtSum.Text = CStr(sum)

I am a beginner too. Use must name the text boxs TxtSomething to make this code work but is does work for a simple addition program.
Hope that helps.
 
Back
Top