Friday 25 October 2013

More variables in VB.NET: short, integer, single, double, decimal, long etc...

More variables in VB.NET:

we have met 2 variables such as: integer and string till now.
Now we will get few more regarding the variables..

1.SHORT:

First place the TEXTBOX and BUTTON in the FORM.

change the properties of TEXTBOX as:
Name the TEXTBOX as txtNumbers by using name property.
And remove the text and empty the TEXTBOX by using text property in property window.

change the properties of BUTTON as:
Change the text of the BUTTON as result using text property in property window.

The form design is:


Then double click on the Result BUTTON.
Then type the code below.

     Dim testNumber As Short

        testNumber = Val(txtNumbers.Text)

      MsgBox(testNumber)





Then RUN the FORM.
Goto DEBUG ---->> START DEBUGGING.
Then type 1 in the TEXTBOX then click on Result.
It displays the result in the MESSAGE BOX.




In this manner type the numbers in TEXTBOX by placing a digit to next place of already existed digit in this manner.



In this manner we can print the numbers until 5 digits.ex: 12345
But we can not proceed to 6 digit numbers.
when we try to goto 6 digit numbers it will display an error message.




When you click the Break button, you are returned to the coding environment. You'll see the problem line highlighted in yellow:




Why the error raised ??

The reason we got an error message after just 6 numbers was because of the variable type. We had this:
Dim testNumber As Short

And it's As Short that is causing us the problems. If we use As Short we're only allowed numbers up to a certain value.
The range for a Short variable is -32 768 to 32 767.
 When we entered 6 numbers, Visual Basic decided it didn't want to know.

 If we run your program again, and then enter 32768, we'll get the same Overflow error message.


 If we change it once more to -32769, we'll get the error message as well. 
So it's not just 6 numbers.
A Short Type can't handle - it's 5 numbers range(-32 768 to 32 767 ) above or below the values specified.

The solution for this problem is::
change the Variable SHORT into INTEGER.

Integer:

Now start the program and try again with INTEGER variable, adding numbers one at a time to the TEXTBOX, and then clicking the Result button. 

How far did you get this time?
change the above code into below by introducing INTEGER instead of SHORT
Dim testNumber As Integer

        testNumber = Val(txtNumbers.Text)

        MsgBox(testNumber)

          
    If you started at 1 and added the numbers in sequence, we should have been allowed to enter 1234567890.



 One more number and Visual Basic gave us the Overflow error message.




 That's because variable types with As Integer also have a limitation. 
The range you can use with the As Integer variable type is -2,147,483,648 to 2,147,483,647.



 If we want a really, really big number we can use As Long.

Dim testNumber As Long

        testNumber = Val(txtNumbers.Text)

        MsgBox(testNumber)



The result is:


In this result the number after 16 digits it was rounded.



later sections we will get the details about the FLOATING numbers.


<---- Previous page                                            NextPage ---->


VB.NET HOME PAGE

Thursday 17 October 2013

Reading Data From TEXT BOXES and store them in another TEXT BOX instead of MESSAGE BOX.

Example program:

Reading Data From TEXT BOXES and store them in another TEXT BOX instead of MESSAGE BOX

  • Firstly add 3 LABELS to our FORM identifying 3 TEXTBOXES.
  • Then add a BUTTON and name it as APPEND NAMES by using text property.
  • And also name the LABELS as "FirstName" , "LastName" and "FullName" by using text property.
  • And naming the TEXTBOXES
  •  by using NAME property in the property window as "txtFirstName", "txtLastName" and "txtFullName".
The form design  is shown below:




This is the code for Button to take the input from 2 textboxes and print them in another textbox.
Double click on Button. It opens the code window..
the code is:
        Dim FirstName As String
        Dim LastName As String
        Dim FullName As String

        FirstName = txtFirstName.Text
        LastName = txtLastName.Text

        FullName = FirstName & " " & LastName

        txtFullName.Text = FullName



Instead of MsgBox(FullName)   we write txtFullName.Text = FullName. 
The code is:

Then RUN the form and enter the data into first 2 text boxes:

Then click on button the result will be displayed in 3rd TextBox.






<----Previous page                                                          Next page ---->



VB.NET HOME PAGE

Wednesday 16 October 2013

Assigning TEXTBOX text to variables

Assigning TEXTBOX text to variables



            Instead of putting direct text into  variables, such as "Geetha" or "Rani",
we can get  text from a textbox and put that straight into your variables. We'll see
 how that's done now. 
  • Add a new textbox to our form.
  • With the TEXTBOX selected, locate the Name property in the                                               Properties area:

Change the NAME PROPERTY of TEXTBOX2 into 'txtLastName".



In this manner change the TEXTBOX1 NAME PROPERTY too.

Then add a BUTTON to the FORM.
Rename the button as shown in above.
Double click on button to write the below code.

        Dim FirstName As String
        Dim LastName As String
        Dim WholeName As String

        FirstName = txtFirstName.Text
        LastName = txtLastName.Text

        WholeName = FirstName & " " & LastName

        MsgBox(WholeName)


Then RUN the FORM.
Type any text in 2 TEXTBOXES.
Then click on Button..
It gives the RESULT in MESSAGE BOX.




<---- Previous page                                    Next Page ---->

VB.NET HOME PAGE

Tuesday 15 October 2013

Example program to store the string in REVERSE ORDER...

Example program for storing the strings in reverse order.


We saw in previous post about string variables..
We know how to design the form and code for the button to append the 2 strings.
This is the code for the Storing and appending the strings in TEXTBOX in REVERSE ORDER..

code:
        Dim FirstName As String
        Dim LastName As String
        Dim FullName As String

        FirstName = "Geetha"
        LastName = "Rani"

        FullName = LastName & " " & FirstName

        TextBox1.Text = FullName


This is the form design:



This is the code:




This is the output when you run the FORM:






<---- Previous page                                    Next page ---->


VB.NET HOME PAGE




Introduction to String variables..

String variables:

In previous section we have learnt about INTEGER variables.
In this section we will learn about the STRING variables.

             And if we want Visual Basic to store text we need to use the word "String". To set up a variable to hold text we need to use As String and not As Integer. If the information we want to store in our variables is a First Name and a Last Name, we can set up two variables like this.

Dim FirstName As String
Dim LastName As String

Here also we can assign the values to the variables like Integer, but the difference is we can write them in between DOUBLE QUOTES.

ex: FirstName = "Geetha"
      LastName = "Rani" .

This is form design for the String variable :

First place one text box and one button on the form.
Then change the button name as "appending 2 strings" in button properties.




This is the code for the Button:

        Dim FirstName As String
        Dim LastName As String
        Dim FullName As String

        FirstName = "Geetha"
        LastName = "Rani"

         FullName = FirstName & LastName

        TextBox1.Text = FullName


Place this in between Private sub and End sub.


Then run the form.
click on button.
then the result will be Printed in TESTBOX .

this is the output:

In the above output the 2 strings are appended with no space between them.

If we want to give space in between them just insert the DOUBLE QUOTES with no date in between those strings:

Ex:
 Dim FirstName As String
        Dim LastName As String
        Dim FullName As String

        FirstName = "Geetha"
        LastName = "Rani"

         FullName = FirstName & " " & LastName

        TextBox1.Text = FullName


This is the output :






<---- Previous page                                    Next page ---->


VB.NET HOME PAGE













Sunday 13 October 2013

Example Program 1: add, subtract, multiply, division of 2 numbers and print them in TEXT BOXES using BUTTONS..

Example 1:


First insert 4 LABELS, 4 TEXT BOXES, 4 BUTTONS.
This is the FORM design.







Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Integer = 100
        Dim number2 As Integer = 50

        Dim answer As Integer

        answer = number1 + number2
        TextBox1.Text = answer


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim number1 As Integer = 100
        Dim number2 As Integer = 50

        Dim answer As Integer

        answer = number1 - number2
        TextBox2.Text = answer

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim number1 As Integer = 100
        Dim number2 As Integer = 50

        Dim answer As Integer

        answer = number1 * number2
        TextBox3.Text = answer

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim number1 As Integer = 100
        Dim number2 As Integer = 50

        Dim answer As Integer

        answer = number1 / number2
        TextBox4.Text = answer

    End Sub
End Class


Then RUN the FORM.

we get the result as:



Then click on the buttons the result is:





  <---- Previous page                                     Next page ---->


VB.NET HOME PAGE  
    



Saturday 12 October 2013

Print the result value in the TEXT BOX instead of MESSAGE BOX

Sample program:

1. Place the TEXT BOX and a BUTTON on FORM.
2. Change the button name into ADD 2 NUMBERS.

3.Then double click on button is show the code of the form.
 then type the below code.
        Dim var1 As Integer
        Dim var2 As Integer
        Dim result As Integer

        var1 = 10
        var2 = 15

        result = var1 + var2
        TextBox1.Text = result         
Type the yellow marked code instead of  MsgBox(result)






  <---- Previous page                                Next page ---->

VB.NET HOME PAGE

Sample Program

Example program:

     First add a BUTTON to the FORM As Shown above.
     Select the button and then go to Property window and change the button name as ADD 2 NUMBERS.

     how to change the name of the button?
     refer this link.
        
            To get our first look at the code window, double click your Button control. The code 
      window will appear.




As shown in the above figure we need to write our code where the cursor blinks.. here we need to concentrate on our code for our program. The cursor will be blinks in between Private sub and End sub.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles Button1.Click                                                                                                                             
                                                                                                                                                                  
End Sub                                                                                                                                                

                     
Private
Private means that no other part of the program can see this code except for our button.
Sub
Short for Subroutine. The "Sub" word tells VB that some code follows, and that it needs to be executed.
Button1
This is the name of our button. You might think that we've just erased the word "Button1" when we changed the Text property, so why does VB insist that it's still called Button1? We'll, the Name property of the control is the important one. If you change the Name property, VB will change this button name for you.
_Click ( )
This is something called an Event. In other words, when the button is clicked, the Click Event will fire, and the code we're going to write will be executed.
End Sub
The subroutine ends right here. This signifies the end of our code.

Now we are going to write our code in between Private sub and end sub.


        Dim var1 As Integer
        Dim var2 As Integer
        Dim result As Integer
      
        var1 = 13
        var2 = 15
       
        result = var1 + var2
        MsgBox(result)



Then save our project and then run it.
Goto debug menu ----> start Debugging.
Then click on button.
Then the result will be displayed in MESSAGE BOX.



                                             

<---- Previous page                                                 Next Page ---->

VB.NET HOME PAGE

Friday 11 October 2013

Dealing with variables in VB.NET.

How to create variables in VB.NET?              

                With Visual Basic, and most programming languages, what you are doing is storing things in the computer's memory, and manipulating this store. If you want to add two numbers together, you put the numbers into storage areas and "tell" Visual Basic to add them up. But we can't do this without variables.

For ex:


Dim var1 As Integer
Dim var2 As Integer

var1= 10
var2= 15

This code is used to setting up the variables in the VB.NET.
Dim
Short for Dimension. It's a type of variable. we declared  that we  are setting up a variable with this word. We'll meet other types of variables later.
var1
This is a variable. In other words, our storage area. After the Dim word, Visual Basic is looking for the name of our variable. we can call our variable almost anything you like such num1, x, y and so on. But there are a few reserved words that VB won't allow.
As Integer
We're telling Visual Basic that the variable is going to be a number (integer). Well meet alternatives to Integer later.
var1 = 10
The equals sign is not actually an equals sign. The = sign means assign a value of. In other words, here is where you put something in your variable. We're telling Visual Basic to assign a value of 10 to the variable called varr1. 


<---- Previous page                        Next page ---->

VB.NET HOME PAGE


Change the Background color of the Form

How to change the background color of  the Form?

  •  First select the Form by mouse left click on it.
  •  Then move to Property window.
  •  Select Back color. By default it has system colors nothing but "control".
  •  From there we can select the other colors as we like.

Now we will have a look that shows the form background color:


Before changing (Default):


After changing:



For FORM Back Color we have 3 different types of colors:
 They are:   
  •  CUSTOM
  •  WEB
  •  SYSTEM
 Custom colors:


Web colors:


System colors:






<----Previous page                                          Next page ---->


VB.NET HOME PAGE