Skip to content

make calculator for noobs

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #190274
    pharaon
    Member

    ok i’m not so good in english but i’ll try to explain as much as i can
    1- open vb standard exe
    2- creat on the form command botton, 3 textbox, 3 labels, 4 option bottons
    make it look like this

    2- choose lable1 from the right panal (properties) change the Caption from lable1 to First number…do the same witht he second lable and make it Second number..and do it witht he 3rd lable make it the result
    3- now the text1 choose it from the right panal (properties) look for text and delete the word text1 from it..do it with the other 2 textbox…but in the 3rd one look for the Locked and make it True..because the 3rd textbox is the result and we wont people to edit it..it will be just to show the result not to writ any thing in it
    4- now click on Command 1 botton and go to properties panlal change the Caption to The Result
    5- select the Form it self and from the properties panal change the caption from Fourm1 to Calculator, also look for Border Style and make it fixed single so no one can change the calculator size.
    5- now choose option1 from the prperties panal change the caption to +,the second option botton make it -, 3rd make it x, 4rs make it ÷,
    so it should be like that

    6- now double click on the result botton and type this code which i’ll explain

    Private Sub Command1_Click()
    Dim A, B
    A = Val(Text1.Text)
    B = Val(Text2.Text)
    If Option1.Value = True Then
    Text3.Text = A + B
    End If
    If Option2.Value = True Then
    Text3.Text = A - B
    End If
    If Option3.Value = True Then
    Text3.Text = A * B
    End If
    If Option4.Value = True Then
    Text3.Text = A / B
    End If
    End Sub

    ok now to explain what the code mean

    Dim A,B mean define variable A and variable B

    A=Val(Text1.text) variable A is a numerical value and loctae in text1

    B=Val(Text2.text) variable B is a numerical value and loctae in text2

    If Option1.Value=True Then
    Text3.Text=A + B
    End if

    its clear to us …if we select the option 1 wich is to plus in text3 place show the result of A + B

    and so on for the rest of the

    so now what i need of people who read and learn and understand to be created…so i want them to make the same progam but change the option with comman botton when we click on it .it will do the calculate that we want..also make the 3 textbox to be one…i’ll tell you a clue on who to do that …make the 3 text box over each other so when we type the first number in the text1 and press on the botton for what we want to do like + or – or × or ÷ it hide the textbox1 and show the textbox2 in the same place then when we click the result botton it hide them both and show the textbox3…hope you get the idea and want to see you doing it right.

    and when i understand more command about VB i’mm make tutorial for it and explain it to you

    #190286
    AhFox
    Member

    Very good … one thing I would like to suggest

    If then elseif

    instead of (WASTE RESOURCE)
    If ‘evaluate first time
    If ‘ evaluate second time
    If ‘evaluate third time

    do this instead (Better method)

    if ‘if true ‘then exit the if statement
    elseif ‘if not true ‘then evaluate
    elseif ‘if not true then evaluate

    #190285
    pharaon
    Member

    nice man ok then writ down the code and pose it..we all learn of each other and new ideas make us evolve our programing skills..so i’ll wait for the code of you

    #190284
    String
    Member

    What NVYE means, and correctly so, is that it is more efficient in this instance to use Elseif.

    If Option1.Value = True Then
    Text3.Text = A + B
    ElseIf Option2.Value = True Then
    Text3.Text = A - B
    ElseIf Option3.Value = True Then
    Text3.Text = A * B
    ElseIf Option4.Value = True Then
    Text3.Text = A / B
    End If

    Since you are making a tutorial for others, you should make sure you show proper coding techniques. You should learn how to properly define your variables. Dim A, B could cause you problems down the road.

    Overall, nice submission. TY

    #190283
    pharaon
    Member

    nice string so now we want to remove the optionbox and replace it with bottons and make 1 textbox so let us see how to do that

    #190282
    Departure
    Member

    very good job, this is what people should be doing that are new to programming, write tutorials so you fully understand what is happening in the code just as pharaon has done… Good job and im looking to forward to seeing your solution with replace the option box with buttons and only one text box

    #190281
    pharaon
    Member

    ok guys new i have change the option box to keys and remove the 3rd textbox and put labelbox inested..so now how the result will appear in labelbox ?..i tell you how i’mm make the label Caption is the result of numbers calculating..let us see how to do that…
    1- the first textbox for the first number
    2- the second textbox
    3- the 4 botton for the + – × ÷
    it should look like this
    so now let us explain the first code for the botton +

    Private Sub Command1_Click()
    Label1.Caption = Val(Text1.Text) + Val(Text2.Text)
    End Sub

    that mean when we click on that botton (+) which it name is command one do the following
    make the labelbox caption which its name is label1 to change to the sum of the value of the number in the textbox1 and the value of the number in the text box2

    and so on for the rest of the bottons
    so the full code looks like this

     

    Private Sub Command1_Click()
    Label1.Caption = Val(Text1.Text) + Val(Text2.Text)
    End Sub
    
    Private Sub Command2_Click()
    Label1.Caption = Val(Text1.Text) - Val(Text2.Text)
    End Sub
    
    Private Sub Command3_Click()
    Label1.Caption = Val(Text1.Text) * Val(Text2.Text)
    End Sub
    
    Private Sub Command4_Click()
    Label1.Caption = Val(Text1.Text) / Val(Text2.Text)
    End Sub

     

    now we still want to replce it to 1 textbox only

    #190280
    String
    Member

    Look into placing your 4 cmdbuttons into an array.

    #190279
    Chike
    Member

    Names like Command1_Click will get you lost quickly when you have a larger projec.

    Give fields/variables meaningful names at the time you create/declare them. e.g. input1/2, instead of Text1/2, cmdPlus cmdMinus etc for the buttons etc.

    As for the ElseIf, more important then being efficient it makes the code more readable and understand (won’t lbe onng before you forget what you intended to do.) Also add comments. It does not have to be every line, but at least for routines and logical code blocks.

    #190278
    AhFox
    Member

    Excellent !!! I personally would not suggest to have any code inside Commnad1_Click instead write a new fucntion.

    Function getCalculation(Byval action as String, Byval number1 as Float, Byval number2 as Float) As Float
    Select Case action
    Case "+"
    Return number1 + number2
    Case "-"
    Return number1 - number2
    ....[then so on]
    End Select
    End Function

    Then

    Private Sub Command1_Click()
    Label1.Caption = getCalculation("+", Text1.text, Text2.text)
    End Sub

    Why do you need to do this? Efficiency. I found that 9/10 programmers would write code inside the Command Click handler, which is really bad because at some point may delete that button, in a large project you might leave orphan code behind.

    Hope this helps

    #190277
    Departure
    Member

    I agree naming your controls is the big one!! even if you don’t make Command1 into cmd… at least make it something understandable to you and who ever you show your code to, my personal favorite for “Command1” is Btn which means button to me, or Lbl which means label or Ed meaning edit anyway you get the idea while there is some sort of standard you don’t need to always stick with it, but at least make it something you will understand and people reading your code will get what it means. as for what NVYE says this is also useful because you can actually reuse that function in other projects if needed without having link to any of your controls, any code which will be used more than once should be turned into function also.

    #190276
    pharaon
    Member

    well guys i’m still learning…the point of this tutorial now to show how to do..but to show what it do…i writ the code and explain what each part of it do and mean so me and other understand the programing language…

    good work NVYE..nice good….but i would ask you to explain what that code mean what each part of it do and where to type it..

    i need you guys to explain codes so i can understand this programing language

    #190275
    autopilot
    Member

    pharaon i see from your screenshots that you are using vb6

    most of the people on this site that understand programming are using vb 2005 or vb 2008. i would recommend that you download and use VB 2008 express from .

    there is not much point in learning vb6 now as it is rather old and and not as flexible as using a .net language (imho).

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.