VB text calculator

Problem with arithmetic

Page 1 of 1

3 Replies - 4348 Views - Last Post: 09 April 2008 - 05:45 PM Rate Topic: -----

#1 r10lover10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-November 07

VB text calculator

Posted 07 April 2008 - 03:06 PM

I am building a text calculator with radio buttons as + - * /.

Textboxes are used for user to input the 1st and 2nd number, the 3rd box is the answer textbox.

Input 2 numbers and check the arithmetic radio button. The answer will be shown in the 3rd textbox.

I built the calculator form but I am having trouble assigning values into the arithmetic function.

here's my code:
	#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 RadioButton1 As System.Windows.Forms.RadioButton
	Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
	Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
	Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
	Friend WithEvents Button1 As System.Windows.Forms.Button
	Friend WithEvents Button2 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.RadioButton1 = New System.Windows.Forms.RadioButton
		Me.RadioButton2 = New System.Windows.Forms.RadioButton
		Me.RadioButton3 = New System.Windows.Forms.RadioButton
		Me.RadioButton4 = New System.Windows.Forms.RadioButton
		Me.Button1 = New System.Windows.Forms.Button
		Me.Button2 = New System.Windows.Forms.Button
		Me.SuspendLayout()
		'
		'TextBox1
		'
		Me.TextBox1.Location = New System.Drawing.Point(152, 40)
		Me.TextBox1.Name = "TextBox1"
		Me.TextBox1.Size = New System.Drawing.Size(75, 20)
		Me.TextBox1.TabIndex = 0
		Me.TextBox1.Text = ""
		'
		'TextBox2
		'
		Me.TextBox2.Location = New System.Drawing.Point(152, 88)
		Me.TextBox2.Name = "TextBox2"
		Me.TextBox2.Size = New System.Drawing.Size(75, 20)
		Me.TextBox2.TabIndex = 1
		Me.TextBox2.Text = ""
		'
		'TextBox3
		'
		Me.TextBox3.Location = New System.Drawing.Point(152, 136)
		Me.TextBox3.Name = "TextBox3"
		Me.TextBox3.Size = New System.Drawing.Size(75, 20)
		Me.TextBox3.TabIndex = 2
		Me.TextBox3.Text = ""
		'
		'RadioButton1
		'
		Me.RadioButton1.Location = New System.Drawing.Point(24, 40)
		Me.RadioButton1.Name = "RadioButton1"
		Me.RadioButton1.Size = New System.Drawing.Size(75, 23)
		Me.RadioButton1.TabIndex = 3
		Me.RadioButton1.Text = "+"
		'
		'RadioButton2
		'
		Me.RadioButton2.Location = New System.Drawing.Point(24, 120)
		Me.RadioButton2.Name = "RadioButton2"
		Me.RadioButton2.Size = New System.Drawing.Size(75, 23)
		Me.RadioButton2.TabIndex = 4
		Me.RadioButton2.Text = "*"
		'
		'RadioButton3
		'
		Me.RadioButton3.Location = New System.Drawing.Point(24, 88)
		Me.RadioButton3.Name = "RadioButton3"
		Me.RadioButton3.Size = New System.Drawing.Size(75, 23)
		Me.RadioButton3.TabIndex = 5
		Me.RadioButton3.Text = "-"
		'
		'RadioButton4
		'
		Me.RadioButton4.Location = New System.Drawing.Point(24, 168)
		Me.RadioButton4.Name = "RadioButton4"
		Me.RadioButton4.Size = New System.Drawing.Size(75, 23)
		Me.RadioButton4.TabIndex = 6
		Me.RadioButton4.Text = "/"
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(120, 184)
		Me.Button1.Name = "Button1"
		Me.Button1.TabIndex = 7
		Me.Button1.Text = "Calculate"
		'
		'Button2
		'
		Me.Button2.Location = New System.Drawing.Point(208, 184)
		Me.Button2.Name = "Button2"
		Me.Button2.TabIndex = 8
		Me.Button2.Text = "Clear"
		'
		'Form1
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(292, 266)
		Me.Controls.Add(Me.Button2)
		Me.Controls.Add(Me.Button1)
		Me.Controls.Add(Me.RadioButton4)
		Me.Controls.Add(Me.RadioButton3)
		Me.Controls.Add(Me.RadioButton2)
		Me.Controls.Add(Me.RadioButton1)
		Me.Controls.Add(Me.TextBox3)
		Me.Controls.Add(Me.TextBox2)
		Me.Controls.Add(Me.TextBox1)
		Me.Name = "Form1"
		Me.Text = "Calculator"
		Me.ResumeLayout(False)

	End Sub

#End Region

	Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
	   [b] Return x + y[/b] 'error'
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
		Dim x As Double
	End Sub

	Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
		Dim y As Double
	End Sub

	Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

	End Sub

	Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click

	End Sub

	Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged

	End Sub
End Class


Also, how do I get the Calculate and clear button to work?

Help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: VB text calculator

#2 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: VB text calculator

Posted 07 April 2008 - 05:01 PM

well in the code you are only dimensioning x and y as doubles. I would say use decimal instead and add a button and double click it, the block of code you write next will be used when the button is clicked. use the following code

Dim x as decimal
dim y as decimal
If isnumeric(textbox1.text) and isnumeric(textbox2.text) then
x = textbox1.text
y= textbox2.text
If radAdd.checked then
textbox3.text = x + y
Elseif radsubtract.checked then
textbox3.text = x - y
Elseif radMult.checked then
textbox3.text = x * y
Elseif RadDiv.checked then
textbox3.text = x / y
end if
end if



Im pretty sure you did not write any code for this besides the dim statements, but i would still like to help. Please be aware as your projects get tougher and more challenging, if you do not show us you have done any of your own coding, we[The DIC Community] will not be doing it for you
Was This Post Helpful? 0
  • +
  • -

#3 r10lover10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-November 07

Re: VB text calculator

Posted 09 April 2008 - 10:21 AM

Thanks for the explanation.
I am fresh to VB.net and I don't understand how to link the event and the text box together. I google for it but I couldn't undertstand the guide nor the codes. The code I tried were all error highligted.

According to your advice,
Create a click event for the calculate button and add the calculation code in it. The radAdd.check, radSubtract.check etc are the rename of the check box.
Correct me if I am wrong. I will have time to work on it tonight.

appreciated for the help, ferrari.
Was This Post Helpful? 0
  • +
  • -

#4 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: VB text calculator

Posted 09 April 2008 - 05:45 PM

anytime, but im not sure i understand you

Quote

The radAdd.check, radSubtract.check etc are the rename of the check box.

The rad.add is the name of the radio buttons. I would suggest radio buttons for this as they will not allow someone to select add and subtract at the same time. the rad in radadd stands for radiobutton. the ".checked" function of it checks whether or not the radiobutton is checked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1