Home

Introduction

 

Fundamentals

 

Introduction

 

 

     
 
 

 

   
   
 

Previous Copyright © 2015 FunctionX Next

Home

Introduction to Microsoft Visual F#

 

Microsoft Visual Studio Fundamentals

 

Introduction to F#

A functional computer language, also called a functional programming language, also called a functional language, is a language that treats an assignment as an object. That is, a function is something that has a name, holds a value, and can perform actions.

To support functional languages and functional programming, Microsoft developed a language named F# (pronounced F sharp).

In our lessons, we will learn how to create graphical Windows applications. Although not required, w will create our application mostly using Microsoft Visual Studio.

Microsoft Visual F# Fundamentals

Microsoft Visual Studio is a programming environment used to create graphical user interface (GUI) applications for the Microsoft Windows family of operating systems. To follow these lessons, you can use Microsoft Visual Studio 2014 Professional or F# tools in Microsoft Visual Studio Express. If you don't have access to it, you can download a trial version free from the Microsoft web site.

After installing it, to use Microsoft Visual Studio, you must launch it. You can do this by clicking Start -> (All) Programs -> Visual Studio 2015 -> VS Express 2015 for Desktop or Start -> (All) Programs -> Visual Studio 2015 -> Visual Studio 2015:

Microsoft Visual Studio Express 2013 for Windows Desktop

The Toolbars

Microsoft Visual Studio's menus and toolbars can be customized. You can customize a menu category on the main menu by adding a menu item to it. You can customize a toolbar by adding a button to it. To start, right-click anything on the main menu or on any toolbar and click Customize... For example, imagine you want to add an item named Start Without Debugging so that it would let you easily execute your projects and you want to add its button to the Standard toolbar, and imagine you want to position it on the left side of the Start button. To proceed:

  1. Right-click the main menu or a toolbar and click Customize

    Cutomize

  2. On the Customize dialog box, click the Commands tab
  3. Click the Toolbar radio button
  4. In the Toolbars combo box, select the toolbar that will host the button. For our example, that would be Standard

    Cutomize

  5. Click Add Command...

    Add Command

  6. In the Categories list, click the desired option. For our example, that would be Debug
  7. In the Commands list, click the desired button or menu item. For our example, that would be Start Without Debugging

    Add Command

  8. Click OK
  9. Click the Move Down and Move Up buttons until the new position is positioned where you want:

    Customize

  10. On the Customize dialog box, click Close:
  11. The position where the button has been added

    In the same way, you can customize any toolbar and any menu category.

A Project

 

Introduction to the Project Interface

Besides the windows and functionalities we reviewed earlier, when you work on a project, there are other features that become available.

The Solution Explorer

The Solution Explorer is a window that displays the file names and other items used in your project:

Solution Explorer

The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button. To explore an item, you can double-click it. The result depends on the item you double-clicked.

The Solution Explorer can be used to create a new class, a new folder, or a reference. To perform any of these operations, you can right-click a folder node such as the name of the project, position the mouse on Add and select the desired operation. You can also perform any of these operations from the Project category of the main menu.

Besides adding new items to the project, you can also use the Solution Explorer to build the project or change its properties. If you add one or more other project(s) to the current one, one of the projects must be set as the default. That project would be the first to come up when the user opens the application. By default, the first project created is set as the default. If you have more than one project, to set the default, right-click the name of the desired project in Solution Explorer and click Set As StartUp Project.

The Solution Explorer also allows you to rename or delete some of the items that belong to your project.

Creating a Project

To create a computer program, also called an application, you create one or more files and group them in what is called a project. A project can contains various types of files.

To create a Microsoft Visual F# project, you can display the New Project dialog box. To open the New Project dialog box:

In the New Project dialog box, In the Installed Templates left list, click Visual F#. The list of projects may not display in the middle list:

New Project

In this case, click Install Visual F# Tools and click OK:

Install Missing Features

Click Install:

Install Missing Features

ApplicationTopic Applied: Executing a Project

  1. On the main menu, click File -> New -> Project...
  2. In the left section, click Visual F# (or Other Languages and click Visual F#)
  3. In the middle list, click Console Application
  4. Set the Name to Exercise1

    New Project

  5. Click OK

Compiling and Executing a Project

The instructions created for an F# program are written in plain English. After creating the file(s) of a project, you would compile the project to get an executable that becomes ready to be distributed to your users.

To compile and execute a project

Opening a Project

As opposed to creating a new project, you can open a project that either you or someone else created. To open an existing project:

This action would display the Open Project dialog box. This allows you to select a project and open it.

Using Various Projects in the Same Solution

With Microsoft Visual Studio, you can create many projects on one solution. To add a project to a solution:

Any of these actions would display the Add New Project dialog box. You can then select the type of project in the middle list, give a name to the project, and click OK. In the same way, you can add as many projects as you judge necessary to your solution. When a solution possesses more than one project, the first node in the Solution Explorer becomes Solution 'ProjectName' (X Projects). The ProjectName represents the name of the first project and X represents the current number of projects.

When you are using more than one project in the same solution, one of the projects must be set as the startup. The project that is set as the startup has its name in bold characters in the Solution Explorer. You can change and use any project of your choice as the startup. To do this, in the Solution Explorer, you can right-click the desired project and click Set As StartUp Project.

When a solution possesses more than one project, you can build any project of your choice and ignore the others. To build one particular project, you can right-click it in the Solution Explorer and click Build.

The Code Colors of the Code Editor

Code is written in a wide area with a white background. In that 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.

Introduction to Windows Forms Applications

 

Windows Application Configuration

To indicate that you are creating a graphical application, open the properties of the project. In the Output Type combo box, select Windows Application.

ApplicationTopic Applied: Configuring a Windows Application

  1. On the main menu, click Project -> Exercise1 Properties...
  2. On the left side, make sure Application is selected.
    In the right section, click the arrow of the Output Type combo box and select Windows Application
  3. Close the window

Forms Fundamentals

Windows Forms is a technique of creating computer applications based on the common language runtime (CLR). It offers a series of objects called Windows Controls or simply, controls. These controls are already created in the .NET Framework through various classes. Application programming consists of taking advantage of these controls and customizing them for a particular application. To exploit these controls and other features of the .NET Framework, there are various types of applications you can create, including graphical applications (Windows Forms Application), web-based applications (ASP.NET Web Application), console applications (Console Application), etc.

The objects used in a Windows application are stored in libraries also called assemblies. As normal libraries, these assemblies have the extension .dll (which stands for dynamic link library). In order to use one of these objects, you must know the name of the assembly in which it is stored. If you are working from Microsoft Visual Studio, you must add a reference to the assembly (or assemblies) to your project.

To add a reference to an assembly in Microsoft Visual Studio, on the main menu, you can click Project -> Add Reference... You can also right-click the automatically created References node in Solution Explorer and click Add Reference... Any of these actions would display the Reference Manager dialog box. From there, you can click the check box of the library you want and click OK. If you don't see the library you are looking for, you can locate it on another drive or directory using the Browse button.

There are two broad categories of objects used in a graphical application: the forms and the controls. A form is the most fundamental object used in an application. It is a rectangular object that uses part of the computer desktop to represent an application. A form is based on the Form class that is defined in the System.Windows.Forms namespace created in the System.Windows.Forms.dll assembly.

The Application Class

The form is the object that gives presence to an application. Once you have created the (primary) form of your application, you can get it ready to display on the screen. This is taken care of by an object named Application that is equipped to start an application, process its messages or other related issues, and stop the application. The Application object uses something named Run that can be used to start a program. It is followed by the name of the form.

Based on these descriptions, the minimum code we need for an application is as follows:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text <- "Something"

Application.Run(exercise)

Although this seems complex or overwhelming, throughout our lessons, we will explain what all those words mean and we will find out the roles of all the operators used:

ApplicationTopic Applied: Using the Application Class

  1. On the main menu, click Project -> Add Reference...
  2. In the left list, make sure Framework is selected under Assemblies. In the middle list, click the check box of System.Windows.Forms

    Reference Manager

  3. Click Add
  4. To prepare the application for starting, change the document as follows:
    open System
    open System.Windows.Forms
    
    let exercise = new Form()
    
    Application.Run(exercise)
  5. To test the application, press Ctrl + F5. You may receive a dialog box announcing the project is out of date:

    Microsoft Visual Studio

    In this case, click Yes:

    Using the Application Class

  6. Close it by clicking its system Close button and return to your programming environment

Accessories for Programming

 

Comments

A comment is section of code that is not processed in a program. F# supports two types of comment. To create a comment on one line, start the line with //. Anything on the right side of // will be ignored. This means that // supports comments on only one line.

To create a commented section that covers one or more lines, start the section with (* and end it with *). Anything inside will be ignored in the program.

The .NET Framework Library

A computer library is a set of objects (classes), functions, and functionalities used to create computer applications. There are many libraries from various companies.

At one time, Microsoft created the Microsoft Windows (3.0 to 95) operating system. To allow people to create applications that can run on those operating systems, Microsoft created a library named Win32. That library has always been available to create applications for all Microsoft Windows versions. The Win32 library was primarily created for C and C++ programmers. C++ is not an easy language with its arrays and pointers, etc. As an alternative, in the late nineties and early 2000s, Microsoft created a new library named the .NET Framework.

Part of the features you will use in F# depend on the .NET Framework.

In our lessons, when a feature we need to use is mostly part of the .NET Framework, we will point it out; but as much as possible, when a feature is available in both the F# language and the .NET Framework, we will prefer F#.

F# Libraries

To implement the F# language, Microsoft created a set of libraries. Those libraries were added in the .NET Framework. This means that you need the .NET Framework installed on your computer in order to fully use F#. If you install Microsoft Visual Studio, it would take care of the .NET Framework.

Attributes

One of the features of the .NET Framework is called an attribute. An attribute is a technique used to extend the functionality of something. You will hardly need to create attributes (in fact in our lessons, we will not create them). Most or all of the attributes you will need have already been created and are part of the .NET Framework. You can simply use them when necessary.

An attribute appears as follows:

[<Attribute-Name>]

When necessary, we will indicate when and where to (and why) use an attribute.


Home Copyright © 2009-2015 FunctionX Home