jump to navigation

Console Applications August 1, 2008

Posted by tuse in : Console , add a comment

Visual Studio lets you develop Console Applications as well. Console Applications are command-line applications similar to C or Java programs. These applications allow you to take input, display characters on a DOS window.

In order to develop a Console Application, start Visual Studio, point to New Project and select ‘Console Application’.

The System.Console namespace supports the creation of Console Applications.

As with C and Java, the program execution begins with the Sub Main() - the entry point of the program.

Module Module1
Sub Main()
Dim nm As String
System.Console.Write("Enter your name: ")
nm = System.Console.ReadLine()
System.Console.WriteLine("Your name is: " & nm)
System.Console.ReadLine()
End Sub
End Module

This simple program accepts a string from the user and displays it on the console. As you can see, the code is quite similar to a program written in Java. In order to execute this program, press F5/ Debug->Start.

A DOS window pops up asking you to enter your name. This is due to the ‘System.Console.Write()’‘ method.

Similarly, ‘System.Console.ReadLine()’ is used to read a line from the console. The final ‘System.Console.ReadLine()’ is like the getch() used in C programs so that the console ‘waits’ for a keypress before termination.

Saying Hello World in VB.NET June 24, 2008

Posted by tuse in : Technical, VB.NET , 2 comments

VB is all about event programming.

The programmer codes for the various events that are likely to be encountered when an application is running. The programming is handled using the Object Oriented paradigm.

So in this example, we will make an application with a Button, which on click brings up a Message Box with the good old message “Hello World”.

Let us begin-

1.    Open Visual Studio -> File -> New Project -> Windows Form Application

2.    Make sure the ‘Toolbox’ and ‘Properties’ palettes are visible. If not, choose them from the View Menu.

3.    Now from the Toolbox, double click the ‘Button’ control which is available under the ‘All Windows Forms’ category.

4.    You can now see the Button with the text ‘Button 1’ on your form. On building your project, this form will become your application.

5.    Move the Button to the location you desire on the form.

6.    The text displayed on the button can be changed by typing in the required text under the ‘Text’ property for the button in the Properties palette. Make sure the button is selected when you change the text property else the text property for the form will be changed (Ok, so you now also know how to change the name of the form from the default name Form1). The name of the selected control also appears in the Properties palette as you can see in the image (Click on it to enlarge).

Name of the Control in Propert Palette

7.    Now we need to code for the event- Button Click. To do so, double click on the button which can be seen in the form (Aliter- Right Click on the button -> View Code).

8.    You should now see the following window. This is where all the code goes in.

Code Window

9.    The cursor is blinking inside the ‘Button_Click’ method. Whatever code you write before the ‘End Sub’ is meant for the button click event.

10.  Write the following code-

  Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Hello World", MsgBoxStyle.Information, "My First Application")
    End Sub
End Class

11.  As you can see from the screenshot, Visual Studio helps you out with the code as you type with prompts and method definitions. This feature is called Intelli Sense. (BTW Visual Studio is much better than other IDE’s I’ve worked with)

Intelli Sense

12.  Once you are done with the code, simply click on the green run button or hit F5 to commence debugging.

13.  If all goes right, you should be able to see the following window. Click on the button (Yeah I changed the text for the button and the form) and you should be able to see a message box with the text “Hello World”. Notice that similar looking message boxes are often encountered in Windows (For reporting errors and that sort).

Hello World

In case you are wondering where to find the  .exe (executable) file for the application you just created, go to the  bin directory under the Project directory you created. Visual Studio also allows you to make an installer for your application which is an elegant way to distribute your application.

Welcome to the world of programming in VB.NET !


Warning: stristr() [function.stristr]: Empty delimiter in /home/tekyt17/public_html/dotnet/wp-content/plugins/wassup/wassup.php on line 2093