Home

Introduction to Microsoft Visual Basic

 

The Code Editor

 

Introduction

There are two main ways you will manipulate an object of your application, visually or using code. In future sections, we will explore details of visually designing a control. Code of an application is ASCII text-based, written in plain English and readable to human eyes. For an application, you can use any text editor to write your code but one of Visual Studio's main strengths is the code editor. It is very intuitive.

The Code Editor is a window specially designed for code writing.

Author Note Although all languages of the Visual Studio programming environment share the Code Editor, once you have started a type of application, the Code Editor is adapted to the language you are using. Its parser (a program used internally to analyze your code) behaves according to the language of your choice. The features and behaviors of the Code Editor are also different, depending on your language.

The Code Editor is divided in 4 sections:

The Code Editor
 

Practical LearningPractical Learning: Introducing the Code Editor

  1. Change the file as follows:
     
    Imports System.Windows.Forms
    
    Public Class Central
    
        Public Shared Function main() As Integer
    
            Application.Run(New Exercise)
            Return 0
    
        End Function
    End Class
  2. Execute the application to see the new form
  3. To create a new class, on the main menu, click Project -> Add Class...
  4. Set the Name to Circle and click Add

The Tabs Bar

The top section of the Code Editor displays tabs of property pages. Each tab represents a file. To add a new file to the project, on the main menu, you can click

  • File -> New -> File...
  • Project -> Add New Item...

Once in the Add New Item dialog box, in the Templates section, click the type of file you want to create, type a name in the Name text box, and press Enter. After the file has been created, it is represented by a tab in the top section of the Code Editor. In the same way, you can add as many files as you judge them necessary. To access a tab:

  • You can click its name in the Tabs Bar
  • On the main menu, you can click Window and click the name of the desired tab

By default, the tabs display in the order their files were created or added to the project, from left to right. If you don't like that arrangement, click and drag its tab either left or right beyond the next tab

Practical LearningPractical Learning: Introducing the Code Editor

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. Set the Name to Square and click Add
  3. To create a new class, on the main menu, click Project -> Add Class...
  4. Set the Name to Triangle  and click Add
  5. To access the Circle tab, on the main menu, click Window -> Circle.vb
  6. Change the file as follows:
     
    Public Class Circle
        Private rad As Double
    
        Public Property Radius() As Double
            Get
                Return rad
            End Get
            Set(ByVal value As Double)
                rad = value
            End Set
        End Property
    
        Public ReadOnly Property Area() As Double
            Get
                Return rad * rad * Math.PI
            End Get
        End Property
    End Class
    
    Public Class Sphere
        Inherits Circle
        Public Overloads ReadOnly Property Area() As Double
            Get
                Return 4 * Radius * Radius * Math.PI
            End Get
        End Property
    End Class
    
    Public Class Cylinder
        Inherits Circle
    
        Private hgt As Double
    
        Public Property Height() As Double
            Get
                Return hgt
            End Get
            Set(ByVal value As Double)
                hgt = value
            End Set
        End Property
    End Class
  7. To access another tab, in tabs section, click Triangle.vb
  8. Change the file as follows:
     
    Public Class Triangle
        Private bas As Double
        Private hgt As Double
    
        Public Property Base() As Double
            Get
                Return bas
            End Get
            Set(ByVal value As Double)
                bas = value
            End Set
        End Property
    
        Public Property Height() As Double
            Get
                Return hgt
            End Get
            Set(ByVal value As Double)
                hgt = value
            End Set
        End Property
    End Class
    
    Public Class Kite
    
    End Class
  9. Save all

The Class Name Combo Box

The top-left section of the Code Editor displays a combo box named Class Name. As its name indicates, this combo box holds a list of the classes ( and structures) that are created in the current file. You can display the list if you click the arrow of the combo box:

Each item of the Class Name combo box displays the name of its type associated with its parent as implemented in the code.

Practical LearningPractical Learning: Using the Types Combo Box

  1. Click the Circle tab
  2. In the Class Name combo box, select Circle

The Method Name Combo Box

The top-right section of the Code Editor displays a combo box named Members. The Members combo box holds a list of the members of classes. The content of the Members combo box depends on the item that is currently selected in the Class Name combo box. This means that, before accessing the members of a particular class, you must first select that class in the Class Name combo box. Then, when you click the arrow of the Method Name combo box, the members of only that class display:

Method Name

If you select an item from the Method Name combo box, the Code Editor jumps to that members and positions the cursor to the left of the member.

Practical LearningPractical Learning: Using the Method Name Combo Box

  1. In the Method Name combo box, select Area
  2. Press the up arrow key and add a Diameter property as follows:
     
    Public Class Circle
        Private rad As Double
    
        Public Property Radius() As Double
            Get
                Return rad
            End Get
            Set(ByVal value As Double)
                rad = value
            End Set
        End Property
    
        Public ReadOnly Property Diameter()
            Get
                Return rad * 2
            End Get
        End Property
    
        Public ReadOnly Property Area() As Double
            Get
                Return rad * rad * Math.PI
            End Get
        End Property
    End Class
    
    . . . No Change
  3. Save all

Code Colors

Code is written in a wide area with a white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text. The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

The Options Dialog Box

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.

 

 

Previous Copyright © 2008 FunctionX, Inc. Home