How do i put number buttons for calculator?
Here's what i have so far.
Code:
Private Sub Add_Click()
TextBox3.Text = Val(Value1Text.Text) + Val(TextBox2.Text)
MSD.Text = "+"
End Sub
Private Sub Clear_Click()
TextBox3.Text = 2
TextBox2.Text = 1
Value1Text.Text = 1
MSD.Text = ""
End Sub
Private Sub Divide_Click()
TextBox3.Text = Val(Value1Text.Text) / Val(TextBox2.Text)
MSD.Text = "/"
End Sub
Private Sub Exxit_Click()
Unload Me
MsgBox "You Have exited out of Calculator"
End Sub
Private Sub Multiply_Click()
TextBox3.Text = Val(Value1Text.Text) * Val(TextBox2.Text)
MSD.Text = "*"
End Sub
Private Sub Square_Click()
TextBox3.Text = Val(Value1Text.Text) ^ Val(TextBox2.Text)
MSD.Text = "^"
End Sub
Private Sub Subtract_Click()
TextBox3.Text = Val(Value1Text.Text) - Val(TextBox2.Text)
MSD.Text = "-"
End Sub
How would i go about making buttons like, 1, 2, 3, type buttons? It's kind of confusing for me.
And if it helps i'm trying to make a calculator and i started 2 days ago.
Re: How do i put number buttons for calculator?
Honestly, making a calculator in VB isn't a very good idea. If you have a "screen" textbox, all you need to do is to add your digit to the screen when the number button is pressed.
You can message me to find out better ways of making a calculator. But that won't be in VB, it would be in HTML-javascript.
Re: How do i put number buttons for calculator?
assuming this is homework, you have to do your own
if you get stuck post the code for the part that is giving problem
the best way is make all the number buttons in a control array, 0 to 9
that way you only need one lot of code for all number buttons, just use the index property to pass the pressed number
Re: How do i put number buttons for calculator?
Huh? Still kind of confused. Also need help on unloading the app, i know it's simple but i only knew how to do it in the older versions and now that i got the newest version it recognizes unload me as a variable. This is what i got so far. It's in the attachment. I'll post the code too, i made a completely new one, (My old one looked like a piece of crap.)
Damn it says wrong extension so i can't upload that...
This one's for the design.
Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Calculator
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
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.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Button5 = New System.Windows.Forms.Button
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Label1 = New System.Windows.Forms.Label
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 110)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(63, 43)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Multiply"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 61)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(63, 43)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Subtract"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(12, 12)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(63, 43)
Me.Button3.TabIndex = 2
Me.Button3.Text = "Add"
Me.Button3.UseVisualStyleBackColor = True
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(12, 159)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(63, 43)
Me.Button4.TabIndex = 3
Me.Button4.Text = "Divide"
Me.Button4.UseVisualStyleBackColor = True
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(34, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(139, 20)
Me.TextBox1.TabIndex = 4
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(34, 73)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(139, 20)
Me.TextBox2.TabIndex = 5
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(34, 133)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(139, 20)
Me.TextBox3.TabIndex = 6
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(12, 211)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(63, 43)
Me.Button5.TabIndex = 7
Me.Button5.Text = "Square"
Me.Button5.UseVisualStyleBackColor = True
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Location = New System.Drawing.Point(81, 63)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(210, 191)
Me.TabControl1.TabIndex = 10
'
'TabPage1
'
Me.TabPage1.Controls.Add(Me.TextBox3)
Me.TabPage1.Controls.Add(Me.TextBox2)
Me.TabPage1.Controls.Add(Me.TextBox1)
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Padding = New System.Windows.Forms.Padding(3)
Me.TabPage1.Size = New System.Drawing.Size(202, 165)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "TabPage1"
Me.TabPage1.UseVisualStyleBackColor = True
'
'TabPage2
'
Me.TabPage2.Controls.Add(Me.Label1)
Me.TabPage2.Controls.Add(Me.RichTextBox1)
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Padding = New System.Windows.Forms.Padding(3)
Me.TabPage2.Size = New System.Drawing.Size(202, 165)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "TabPage2"
Me.TabPage2.UseVisualStyleBackColor = True
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(0, 17)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(202, 152)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "Notepad"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(37, 3)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(117, 13)
Me.Label1.TabIndex = 11
Me.Label1.Text = "Keep Your Notes Here!"
'
'Calculator
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.TabControl1)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Calculator"
Me.Text = "Calculator"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.TabPage1.PerformLayout()
Me.TabPage2.ResumeLayout(False)
Me.TabPage2.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
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 Button5 As System.Windows.Forms.Button
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
End Class
And this one is the actual code.
Code:
Public Class Calculator
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox3.Text = Val(TextBox1.Text) - Val(TextBox2.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox3.Text = Val(TextBox1.Text) ^ Val(TextBox2.Text)
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Re: How do i put number buttons for calculator?
I'm not one to help people with their homework assignments, so I am not going to do so...but here's a few pointers to help you improve your code:
1) Using button0 to button9 is a simple way of doing things, but using a control array is much better. Rename "button0" to "button" then "right-click", "copy"...now click on the form and "right-click", "paste"...it'll ask if you want to make a control array, click "yes"...you'll notice "button" becomes "button(0)" and you now have "button(1)"...repeat the pasting to get 0-9 and edit each button's caption to match its index.
2) You're coding in .NET, this is NOT the .NET section forum, this is for VB6 and earlier...please refrain from asking questions here as there's people who know .NET better than us who could give better advice...ask in the .NET section of this forum
Re: How do i put number buttons for calculator?
Other than the fact that the TS is in the wrong forum, I would not have a problem with helping with home work if the TS has shown initiative by posting the code he's created. What's unacceptable is something like...
"Hi! My assignment is to build a calculator. How do I do that? Can someone post some code?" :rolleyes:
Re: How do i put number buttons for calculator?
Yeah, I agree, CD...as you noticed, I did start giving a few pointers...I had more to give but when I noticed it was a .NET program I stopped and pointed that out, and left the one pointer I made...I had a few more I was going to suggest :-)
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by smUX
Yeah, I agree, CD...as you noticed, I did start giving a few pointers...I had more to give but when I noticed it was a .NET program I stopped and pointed that out, and left the one pointer I made...I had a few more I was going to suggest :-)
No problem. I just thought it would be pertinent to clarify helping vs doing someones home work for them. :)
As a side note, Lone_Rebel stated that he didn't think developing a calculator in VB was a good idea. No explanation as to why though? :confused:
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by CDRIVE
As a side note, Lone_Rebel stated that he didn't think developing a calculator in VB was a good idea. No explanation as to why though? :confused:
No idea...I'm sure the Windows calc.exe is a VB executable, and it works well enough :-)
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by smUX
No idea...I'm sure the Windows calc.exe is a VB executable, and it works well enough :-)
My only issue with the windows calc is those ridiculously small button chars! My old eyes can barely see the difference between (-) & (*)!:mad:
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by smUX
2) You're coding in .NET, this is NOT the .NET section forum, this is for VB6 and earlier...please refrain from asking questions here as there's people who know .NET better than us who could give better advice...ask in the .NET section of this forum
Really? I swear to god i thought i was coding in VB6. :(
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by Deep2
Really? I swear to god i thought i was coding in VB6. :(
"Inherits System.Windows.Forms.Form"
Clearly not
Re: How do i put number buttons for calculator?
the code in the first post looked like vb6, but subsequent code definitely vb dot net
Re: How do i put number buttons for calculator?
Good morning all,
I swear I am not doing home work, and I am coding in VB6.
I am sure I could find a calculator online, but I really just want to build my own. Mostly because I want it to be customizable and because my other project has me stumped and I wanted to take a break from it. :bigyello:
now that I got that out of the way. I am trying to work out the logic of (key press to results window) so far I have done this:
Code:
Private Sub cmd_butt_num_Click(Index As Integer)
If cmd_butt_num(0) Then
tmp_num = 0
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(1) Then
tmp_num = 1
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(2) Then
tmp_num = 2
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(3) Then
tmp_num = 3
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(4) Then
tmp_num = 4
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(5) Then
tmp_num = 5
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(6) Then
tmp_num = 6
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(7) Then
tmp_num = 7
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(8) Then
tmp_num = 8
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(9) Then
tmp_num = 9
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
End If
End sub
now this does place the numbers in the result window, my question is this. would I concatenate a string and then convert it to a number (the real ?? how do I convert it). I did build the buttons in to an array (not sure how to use the index to get the value thus this cumbersome IF THEN ELSE block.
I am going to do some more searching but if any one can point me in the right direction I would be very grateful.
Re: How do i put number buttons for calculator?
Assuming you have a control array of buttons numbered 0 to 9,
1. Store the first clicked button index in a variable (say x)
2. Store the operator in another variable (say op)
3. Store the 2nd clicked button index in a variable (say y)
4. Then do a Select Case on your opeator, calculate the result accordingly and display the result in your textbox.
Pradeep
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by LA.Rhodes
Good morning all,
I swear I am not doing home work, and I am coding in VB6.
I am sure I could find a calculator online, but I really just want to build my own. Mostly because I want it to be customizable and because my other project has me stumped and I wanted to take a break from it. :bigyello:
now that I got that out of the way. I am trying to work out the logic of (key press to results window) so far I have done this:
Code:
Private Sub cmd_butt_num_Click(Index As Integer)
If cmd_butt_num(0) Then
tmp_num = 0
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(1) Then
tmp_num = 1
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(2) Then
tmp_num = 2
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(3) Then
tmp_num = 3
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(4) Then
tmp_num = 4
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(5) Then
tmp_num = 5
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(6) Then
tmp_num = 6
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(7) Then
tmp_num = 7
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(8) Then
tmp_num = 8
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
ElseIf cmd_butt_num(9) Then
tmp_num = 9
main_calc_mem = main_calc_mem & tmp_num
txt_bx_results.Text = main_calc_mem
End If
End sub
now this does place the numbers in the result window, my question is this. would I concatenate a string and then convert it to a number (the real ?? how do I convert it). I did build the buttons in to an array (not sure how to use the index to get the value thus this cumbersome IF THEN ELSE block.
I am going to do some more searching but if any one can point me in the right direction I would be very grateful.
Instead of multiple If...Else use Select Case.
Do like:
vb Code:
Select case Index
Case 0
'whatever
Case 1
'whatever
.
.
.
End Select
Re: How do i put number buttons for calculator?
or try
Code:
Private Sub cmd_butt_num_Click(Index As Integer)
main_calc_mem = main_calc_mem & index
txt_bx_results.Text = main_calc_mem
end sub
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by Pradeep1210
Assuming you have a control array of buttons numbered 0 to 9,
1. Store the first clicked button index in a variable (say x)
2. Store the operator in another variable (say op)
3. Store the 2nd clicked button index in a variable (say y)
4. Then do a Select Case on your opeator, calculate the result accordingly and display the result in your textbox.
Pradeep
Pradeep, thank you for your quick reply. I had in fact used the case select function. and cut it and saved it as a text file. (i had a small routine that was causing a problem) having removed the problem I will use the case select again. If I am reading your post correctly i would only be able to perform single number equations (ie) 1 + 1 up 9 * 9 . I think I sort of know what to do next.
again thank you.
Re: How do i put number buttons for calculator?
Quote:
Originally Posted by westconn1
or try
Code:
Private Sub cmd_butt_num_Click(Index As Integer)
main_calc_mem = main_calc_mem & index
txt_bx_results.Text = main_calc_mem
end sub
Westconn, thank you for your quick response.
in order to perform ops on large numbers, and i just woke up so I am still trying to get the fuzzy stuff off of my brain. i could use something like:
this is the revised version but I hit a snag
Code:
Private Sub cmd_butt_num_Click(Index As Integer)
If FNE = False Then
Select Case Index
Case 0
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 1
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 2
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 3
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 4
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 5
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 6
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 7
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 8
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
Case 9
main_calc_first = main_calc_first & Index
txt_bx_results.Text = main_calc_first
End Select
ElseIf FNE = True Then
Select Case Index
Case 0
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 1
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 2
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 3
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 4
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 5
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 6
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 7
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 8
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
Case 9
main_calc_second = main_calc_second & Index
txt_bx_results.Text = main_calc_second
End Select
End Sub
I think I am good up to this point but here comes the snag
Code:
Private Sub com_butt_enter_Click()
main_calc_final = main_calc_first Operator main_calc_second
txt_bx_results.Text = main_calc_final
End Sub
How do I tell VB that "Operator" is the actual operator (ie) + or - or * or /
I am getting this error "Expected end of statement" and "Operator" is highligthed" I am assuming it is because VB isnt reading the operator as an operator ???
Code:
Private Sub com_butt_oprs_Click(Index As Integer)
Operator = Index
FNE = True 'boolean flag to determine if we're working on the first or second number
End Sub
I will keep plugging away (coffee may help ) with the fuzzy stuff anyway.
Re: How do i put number buttons for calculator?
as the code in each of your cases is still the same, there is no need for the select case, the index variable will put the correct value
you would only need a case for anything that is different, then have everything else in case other
for your operator you need a select case
vb Code:
select case operator
case 1
a + b
case 2
a - b
case 3
a * b
'etc