To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you also consent to allowing this site to collect anonymised information about your visit. For more information or to opt-out of analytics, read this page.

Basic output methods



There are several very simple ways of outputting information to the user or to the debugger. Some forms are obtrusive outputs and some are unobtrusive.

An obtrusive output means that the program stops running the main sequence whilst the output is displayed whereas an unobtrusive output does not stop the main execution.

Console outputs

The console is a form of collecting multiple outputs for reference. Output here is unobtrusive.

Console.Write

The Console.Write method is used to output any information of any of 17 types (for instance, boolean, integer, single) on to a line without taking a new line.

Below is a sample write command:

VB.NET
Console.Write("Hello world")
Console.Write("This is a test")
		
ConsoleWrite

Console.WriteLine

The Console.WriteLine method is used to output some text and take a line break after it. This way multiple outputs do not get mixed up as easily as shown above.

VB.NET
Console.WriteLine("Hello world")
Console.WriteLine("This is a test")
		
ConsoleWriteLine

Dialog based output

Dialog based output such as message boxes tends to be obtrusive output - it breaks execution of the program.

MsgBox

One of the simple forms of output is to use MsgBox which simply takes up to three arguements as follows:

MsgBox(prompt As String, [Buttons As MsgBoxStyle] [title As String])

The prompt parameter is used to contain a message as a string and title is an optionalMeaning it does not need to be included, instead the compiler gives it a default value. parameter. Below is a sample:

VB.NET
MsgBox("Hello world")
		
Hello world

The other option that was given was the Buttons option. This option is used to specify how the message box looks. Below is a table showing all types of message box. All samples are from Windows 7.

ParameterSample
MsgBoxStyle.AbortRetryIgnoreARIProvides an Abort, Retry or Ignore dialog
MsgBoxStyle.ApplicationModalModalDesigned to be in keeping with application style.
MsgBoxStyle.CriticalCriticalShows the critical status and makes a Windows beep
MsgBoxStyle.DefaultButton1DB1Sets button 1 as the default button
MsgBoxStyle.DefaultButton2DB2Sets button 2 as the default button
MsgBoxStyle.DefaultButton3DB3Sets button 3 as the default button
MsgBoxStyle.ExclamationExclamationWarns the user of a possible issue
MsgBoxStyle.InformationInfoProvides information using a dialog
MsgBoxStyle.MsgBoxHelpMsgBoxHelpProvides a Help button on the dialog
MsgBoxStyle.MsgBoxRightRightRight aligns the text in the dialog
MsgBoxStyle.MsgBoxRtlReadingRTLMakes the dialog follow RTL (right-to-left) reading used by some countries
MsgBoxStyle.MsgBoxSetForegroundForegroundThis message dialog will be in the foreground when created.
MsgBoxStyle.OkCancelOkCancelProvides a dialog with both Ok and Cancel buttons on it
MsgBoxStyle.OkOnlyDB3Provides a dialog with just an Ok button it.
MsgBoxStyle.QuestionQuestionProvides the user with a question mark dialog.
MsgBoxStyle.RetryCancelRetryCancelA dialog with a Retry and a Cancel button.
MsgBoxStyle.SystemModalSystemDesigned to be in keeping with the system style.
MsgBoxStyle.YesNoYesNoProvides simple Yes and No buttons.
MsgBoxStyle.YesNoCancelYesNoCancelProvides Yes, No and Cancel buttons

MessageBox

The MsgBox method is quite limited, whereas the MessageBox class is more feature rich and customisable. One of the lacking features of the MsgBox method is that it does not support both an icon and special buttons.

As MessageBox is not a default included method but a class it does not work the same way. The following sample illustates this.

VB.NET
MessageBox.Show("Hello world!", "Sample program", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
		
MessageBox

Both of these dialog outputs return a DialogResult as a result, which means using an if statement, the result can be used to determine what events happen on each button.

Other output styles

There are other ways of outputting information such as directly putting the information on to the user interface through the use of a control such as a Label or ListBox.