Handy Hints Semester 2, 2008

Note: If your Hint is more than a paragraph long, please put it on a separate page.


Atkins, Christopher

Handy Hint 1:
The Collection Object - The Collection is similar to an array but has several advantages over a traditonal array. It holds both basic data types but is mainly used for Objects. The Collection Object looses some of its advantages over an array when using it to store basic data types. The Advantages are:
  • You dont have to declare its size, it grows automatically as needed
  • you can remove items from the middle without leaving
  • You can refer to the objects as a number or by name (a string)

If you wish to keep the functionality of the Collection while using a basic data type a trick is that you can wrap the basic data type in an object

Handy Hint 2:
TryParse Method - The TryParse is the new replacement for the IsNumeric function. IsNumeric is from preivous versions of vb before beginning the use of the .NET Framework and as such is outdated. With the .NET Framework came the TryParse Method. This method is said to be more reliable than the IsNumeric function. It returns a boolean which can then be used to do whatever you wish of it and because its only a Try you dont unneccisarly get an error message.

Using this code we will test if a text box on the from has a numeric value in it:

Dim i As Integer,
Dim oknum As Boolean
oknum = Integer.TryParse(txtTest.Text, i)
MessageBox.Show("Integer is " & i)
If oknum = False Then
MessageBox.Show("Text cannot be converted into a number.")
End If

The TryParse method can be used for any of the basic data types eg String.TryParse

Bakier, Alistaire


Baker, Judith



Bignell, Jason

Click here to see my fileOperration Module, used for saving and loading files

The following sub can be used to load a treeview with a textfile in the format of [parent node], [child node]
'Varibles for use
Dim filename As String = "AnimalKingdomA.txt" 'My exa,ple file Dim filenumber As Integer = FreeFile() Dim temp As String Dim node As TreeNode Dim top As String 'The indicator for the Top level. This is assumed to be the first thing in the file
'Open file FileOpen(filenumber, filename, OpenMode.Input)

While Not EOF(filenumber)

Input(filenumber, temp)

If top = "" Then 'Sets the toplevel label to the first thing available if it hasn't already been set top = temp End If

If temp = top Then 'Check if this is a top level node Input(filenumber, temp) TreeView1.Nodes.Add(temp, temp) 'If so, add it! 'Note the 2 temps. Find looks for the name of the node instead of the text, so i just set them both the same!
Else node = TreeView1.Nodes.Find(temp, True)(0) ' This is the important part. 'Here the nodes are searched and the first result is set as node Input(filenumber, temp) node.Nodes.Add(temp, temp)

End If
End While

Cole, Aaron

Hint 1:

Name Spaces - A powerful feature most which most people don't know about despite being enabled by default for certain reference paths. Name spaces are a way of avoiding having to write out the entire reference path for objects and libraries, to add or change name spaces on your project go to the project properties screen then select the references tab on the side, the bottom box has a list of name spaces as you will probably notice there are already a few enabled by default. By adding the name space for a certain path for example System.Drawing.Drawing2D now when using this items from System.Drawing.Drawing2D all I have to do is type the item/method I wish to use from that path with out having to type the System.Drawing.Drawing2D part.

Hint 2:

Mono - As you have probably noticed before, code written in VB .net does not compile to native code it compiles to CIL so to use the software you have written to you have to have the CLR installed which is an interpretter for CIL, sort of like Java programs requiring the JVM to run. This brings up an interesting point, the CLR is very similar to the JVM in many ways, and the main benifit of the JVM is making applications produced portable in the sense of if the VM exists on the platform the all Java programs should run on that hardware/operating system, there is no difference for the CLR as long as the CLR exists on a platform the code will run, whilst Microsoft have not ported the runtime to Linux or OSX Novell has. Mono is an open source version of the CLR, it runs on Linux, OSX and Windows, programs produced with Mono will run on the MS CLR and programs produced with .Net will run on Mono. Being cross platform is a powerful capability that is not limited to Java.
Read more on mono here http://www.mono-project.com/

Hewton, Grant

Formatting multiple controls of the same type
When you are adding multiple controls of the same type to a form its much easier to select all of these controls and format them all at the time rather than formatting each individual one.
- for example you can change the color and size and type of font for buttons , textboxes, listboxes eg.

Adding multiple components to the ToolBox
It is much more efficient to know all the .NET components you need to add before you right click in the ToolBox and select Choose Items.. you will notice this takes an unnessarily long time to open and yes you can select everything at once rather than enduring the tedious process of individually adding components.

Keating, Tristan

Removing the maximise and minimise buttons on the form

One problem that I have often seen in the forms that we create for Elisabeth Elsmann's exciting and interesting Sets is that people take loads of time creating beautiful forms when they don't know how or forget to remove the maximise button, often ruining the effect of the form. In run or design time, you can set the MaximizeBox, MinimizeBox and ControlBox settings to whatever you desire.

Using TableLayoutPanels

The TableLayoutPanel is an alternative way to sort out the controls on a form. I'm not going to tell you how to use them (you know the drill, learn, run, test and understand your application!) but I will let you know where one problem can arise that I encountered and how to solve it. I had a form with a TreeView on the left and four buttons on the right. This layout would be achieved with two columns and four rows, however the problem is that the TreeView would only take the first row! After searching in the TableLayoutPanel for ages, I finally found that the TreeView itself had suddenly spawned more properities and the RowSpan property had appeared there! With the RowSpan property set to four the form looked wonderful no matter if it was maximised, minimised or restored.

Kenneth, Adam


Kenny, Owen



Kirkpatrick, Keiran



Ledo, Aiden



Morrow, Alex



Nolan, AJ

Handy Hint 1

When writing large applications, for example our games projects, that use alot of different sub routines things can get pretty messy pretty quickly. And even worse things can get hard to read. Lately i have found thay #Region has been very useful in keeping code cleaner and easier to read. Here is a demo in how to use it:

#Region "Form_Load"
Sub Form_Load()
txtBox1.Text = "Hello World, This Is How you use regions!"
End Sub
#End Region

The block of code between region then becoms collapsable allowign you to view just one sub at a time withotu all the other subs surrounding it.

Hand Hint 2

Another usefull tool i have found is strucrtures. Structures treat a set of variables as an Object. For example you a re writing a diary. A diary has an array of entries (entries is the object here) Entries need a date, the diary it belongs to an, and then an event or some other useful piece of text. We can make this diary a structure. This structure is as follows:

Structure Diary_Entry
Dim date as String
Dim Diary as String
Dim entry as String
End Structure

To use the structure, first dim a variable as your structure as friend and then you can access the objects variables.
Eg. Using the diary

Friend entries as Diary_entry

entries.date = "3/12/08"
entries.Diary = "My Diary"
entries.entry = "Hello, This Is my first entry"

Pagnoni, Steven


Rowney, John

Stokes, Ross



Tidball, Jim

Handy Hint 1 - AutoComplete:

In a lot of modern programs you'll find some sort of autocomplete functionality. That is, when you type into a text box, a small panel will open up with a number of options based on what you have already entered. You can achieve this functionality in your visual basic program fairly easily.

The combobox has two properties that help with this, AutoCompleteSource and AutoCompleteMode. There are too many variations of options to include them all here, but as an example, after loading your combobox with values using a for loop, setting AutoCompleteSource to ListItems will use those items as the source for autocompletion. Setting AutoCompleteMode to suggest append after this will use that list of items to make suggestions as the user is typing, in the text entry field.

_End Handy Hint 1_

Handy Hint 2 - Inheritance and Controls:

In visual basic it is possible and often beneficial to inherit from a control such as a picture box or a button. In order to do this you must use create a new class. After the default initialisation of the class, "Public Class Example" enter a new line and type "Inherits". Now after this inherits statement you can type the name of inheritable classes, not all classes can be inherited and it's not wise to inherit from some classes, however the added functionality you gain from this can be extremely useful. In order to make this inheritance work beyond this, you must write a public sub with this signature. Public Sub New([Options]). You can have any number of optional inputs and this can allows you for example to create a button that will set it's own text or a picturebox that has an event handler built in. In order to make inheritance work best for controls, it is wise to make a call to MyBase.New() which will perform the initialisation needed by the parent class.

_End Handy Hint 2_

Wood, Chris


Handy Hint 1:

Visual Studio 2005 is a powerful program, with many features available to it. But it is missing one feature, which even VB 6 has, and that is the ability to draw shapes. A pack known as the Visual Basic Power Pack adds the feature of creating shapes in Visual Studio 2005 at design time. Ovals, circles, rectangles or even just lines can be created just by selecting it in the Toolbox and dragging it to size on the form. These shapes can even be treated as objects, with the .Click, .MouseOver events for example are available.

Though there might be many cases where these objects are not needed, there are cases when they can be used as a moving object, or even just a border to make your form look that bit more neater. This 2.7 MB file can be downloaded from this website:
http://www.microsoft.com/downloads/details.aspx?FamilyID=371368a8-7fdc-441f-8e7d-fe78d96d4063&DisplayLang=en

Handy Hint 2:

A useful feature avaible in VB.NET that isn't used that much is Tool Tips. These are small boxes which appear when, usually, a user moves their mouse over a item, and this box generally displays information about the control. For example, say there is a Tool Tip on a List Box, this tells the user to select an item from the list, or even select an item from a Combo Box. To include a Tool Tip into your form, you can simply click and drag the ToolTip control to the form, from the Common Controls section in the Toolbox.

Once inserted, select the control you would like to display this tool tip, and in the properties window, there will be an extra field called ToolTip on (then the name of the ToolTip, i.e. ToolTip1, ToolTip2 etc). In this section type in the message which will appear when the mouse moves over the control, make sure this message is not to big. This is one of the three options which should be configured, after determing the message, select the ToolTip control, change the ToolTip title to something appropiate, then the ToolTipIcon to either None, Info, Warning or Error. Run your program and check the results.