VB.net - Introduction to the Language [Making a Calculator]

Discussion in 'Archives' started by SLiZZaRD, Feb 6, 2011.

VB.net - Introduction to the Language [Making a Calculator]
  1. Unread #1 - Feb 6, 2011 at 8:16 AM
  2. SLiZZaRD
    Joined:
    Feb 4, 2011
    Posts:
    230
    Referrals:
    1
    Sythe Gold:
    0

    SLiZZaRD Active Member
    Banned

    VB.net - Introduction to the Language [Making a Calculator]

    VB.net or VisualBasic.net is a common programming language that can be applied in a various amount of ways. It is frequently chosen as the first programming language to learn by many new programmers because of its ease of use and handy features.

    What I'll be using in the following tutorial is Microsoft Visual Studio Express, its free ware and is available from the MS website here.

    The first thing we're going to do is download and open Visual Studio and create a new form. To do this; Ctrl+N>Windows Form Application

    A blank box will now appear, if you are using 2008, go ahead and pin the toolbox to the screen, if you are using 2010 it will be set there by default (I believe). If the toolbox isn't on screen then Ctrl+Alt+X to make it visible.

    You'll notice the Properties box in the bottom right hand corner, this is useful for customising your form for now and you can do so if you please then do so but I wont explain how to do it in this guide.

    The first thing we will be doing is creating a basic calculator that adds two numbers and displays the answer in a textbox. The important thing to know with this is that a dot (.) basically means >. So for example "Textbox1.text" will grab the text from Textbox1. So knowing this we could say

    Textbox1.Text + Textbox2.Text = Answer, right? Well this is basically what we will be doing but a bit more complex. It is important to plan out all your programs before hand but this guide will be more of an explanation and what to do, not much thinking power required.

    The first thing we will do is make three text box's. To do this, click on TextBox on the left side of the client and click them onto the form. Aligning is easy with Visual Basic so just play around with the drag points.

    [​IMG]

    Textbox* is the default name for a textbox in VB. With * being the amount of textbox's with the same name + one. To see this, go to properties and scroll up to the top to see the "(Name)", not the text, the text provides no importance to us for now.

    Now, we need to label the textbox's so people know what they're looking at, to do this go onto the toolbox again and drag three labels to the form.

    [​IMG]

    These labels hold no attachments to anything to the form or textbox's. They're redundant in some form but provide visual ease. To edit the text of the labels go to properties and change the text from the default Label*.

    Leave the (name) of the labels the same as we will not be coding with them.

    Now we need to make a button. The button will provide an answer in the textbox next to the label "Answer" when someone inserts there two numbers in the top two textboxs. The answer will equal Textbox1.Text + Textbox2.text = Textbox3.text (or answer).

    Make a new button at the bottom of the form by dragging it from the toolbox and change the text to Answer, again, this is only for display, you do not need this but it looks better. Now we get to the coding, to do this double click on the button.

    We want to first declare that Textbox1 and textbox2 are numbers and not text. To do this we basically want to "Declare textbox1.text, textbox2.text as numbers" but we can't do that. Want you want to know is that "Dim" = declare and that a commonly used data type is double. Double holds decimals compared to if you were to declare as "integer". So what do you think we need to do? "Dim textbox1.text, texbox2.text as double"? Well, we cant do that so we are going to have to say

    This code declares that textbox1.text and textbox2.text are integers and that they will hold decimals. So enter this code in!

    So now for the answer we just add on to the code;
    Now hit F5 and your form will pop up out of the client, enter any numbers in the first to textbox's and click the button "Answer". The answer will be displayed in the answer textbox.

    Now we're about to make this a bit more complex, I'm going to skip basic steps such as how to make buttons and stuff because I believe you all ready know how to do so. To make this more complex, we will be adding subtraction, division and multiplication to the calculator.

    Leave the answer button you have now and change the text to "Answer (+)". Make three more buttons; Answer (-); Answer (*) and Answer (/). Its also important to know if you don't all ready that an asterisk means multiply in Visual Basic, and that using "x" wont work. Aswell as "/" for division.

    Arrange your buttons so there all around the same area, we'll now do basically the same coding as what we did before. However, we still need to declare the top two textbox's as doubles for every button, so copy and paste that coding.

    Do the following on your own if you think you can do so, trial and error is a better way to learn IMO.

    Click the Answer (*) button and enter this code:
    Click the Answer (-) button and enter this code:
    Click the Answer (/) button and enter this code:
    Your calculator is now done, but it doesn't end there. You can add other things and buttons such as Clear Everything or CE.

    Make a CE button and enter this:
    Make a close button or end button by simply doing this:
    Experiment with this, change around things and play around with it.

    The next part of the tutorial is a bit more complex than the previous part but is very cool and it feels good when you pull it off.

    To start this off make a new form. Add a long textbox at the top and a 3x3 grid of buttons below the textbox. So in total we will have nine buttons and one textbox. Change the (name)'s of the buttons to cmd1,cmd2,cmd3 etc, and change the text to the corresponding number, cmd1 = 1. Give the textbox the name of txtinput.

    Click on the button "1". Before we do any coding on the button we are going to have to add a global variable. Go up to the top just under "Public Class Form1" and add the following

    Boolean is a data type that holds two-state values such as true/false and yes/no. The default for boolean is false but we don't need to worry about that for now.

    Now go down to just under the Private sub etc... (button1/cmd1) part and insert this code.

    What this basically says is that,

    Now, the following buttons I will not show you how to do as it is not difficult. All you have to do is change cmd1 to cmd(the number you have chosen). Remember that, the button is not sending a number, it is sending the text of the button, it will send whatever you see as the text of the button.

    The number 0 is a different matter as we don't want people to use it at the start. So go ahead and add a "0" button. The code we will be using is:

    What this says is, if the textbox text length is greater than 1 we add the 0 onto it. Because we haven't disclosed anything about what to do if it isn't it won't do anything.

    -Will finish off tomorrow, sorry for any inconvenience-
     
  3. Unread #2 - May 7, 2011 at 8:51 PM
  4. onno
    Joined:
    Apr 22, 2011
    Posts:
    58
    Referrals:
    0
    Sythe Gold:
    0

    onno Member
    Banned

    VB.net - Introduction to the Language [Making a Calculator]

    lollll
     
< Fuck my life | Should i post a guide on how to make an Android/Iphone app? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site