Wednesday, June 20, 2007

IComparer vs. IComparable

Why does the .NET framework insist on being sooooo.... complicated?

Sorting In The .NET Framework

Friday, June 15, 2007

Tuesday, June 12, 2007

Trees... Trees... & MORE Trees...

Source: vbCity

The Basics of a TreeView


You'll be familiar with the look and feel of a treeview from the Windows Explorer. Its name of course refers to a regular tree, that has one trunk and many branches and where each branch can have smaller branches. The stem is the treeview object itself, the "branches" are called nodes. The main difference between a branch and a node is that the thickness of a node doesn't need to decrease; on the contrary: the 'ends' can easily be the thickest part of the whole: that is contain the most items.

In principle the number of items and childnodes is unlimited (read: a number you will not easily reach wink ) Each node can contain an item collection (child nodes) and has a parent. Using the item collection and the parent property, you can go through all branches. If you go 'down', you will know you have reached the end when there are no more childitems (GetNodeCount = 0) and you have reached a toplevel node when the parent is the listview object itself.

Populating the treeview during runtime is very easy: just go to the Collection property and press the "..." button. You can play around with the possibilities. To populate during runtime:

Code:
Dim N As TreeNode

'Method 1: straightforward adding of nodes
With Me.TreeView1.Nodes
'add text
.Add("AddByText")
'since with..end with is used: read TreeView1.Nodes.Add ....

'every add method returns the newly created node. You can use
'this concept set the result to a variable or to directly add
'a childnode:
.Add("AddByText2").Nodes.Add("ChildOfAddByText")

'this, you can take as far as you want
.Add("AddByText3").Nodes.Add("ChildOfAddByText").Nodes.Add("Another child")

'--
N = .Add("AddByText, Attach To Variable")
N.Nodes.Add("Child one")
N.Nodes.Add("Child two")
' --
With .Add("AddByText Use WithTo Add ChildNodes").Nodes
.Add("Child 1")
.Add("Child 2")
.Add("Child 3").Nodes.Add("Subchild 1")
End With
End With

'for clarity, from here on, the treeview1 name will be added.
'In everyday use, you'll probably find the use of with..end with
'a lot easier (I know I do..)

'Method 2: adding by node
'Like virtually every .Net method you can directly assign an object:
Me.TreeView1.Nodes.Add(New TreeNode("AddByNode"))

'check out the overloading possibilities of using New()
'Another advantage of this method is that you can add a complete branch.
'(N is already declared as TreeNode above)
N = New TreeNode("MainNodeToAdd")
N.Nodes.Add("Child 1")
N.Nodes.Add("Child 2")

'you can for instance add this newly created node to all main branches:
Dim enumNode As TreeNode
For Each enumNode In TreeView1.Nodes
enumNode.Nodes.Add(N.Clone) '<- the clone() method is needed
Next

'Adding will always add the the node at the end of the collection.
'Of course you can also insert at a specified location:
Me.TreeView1.Nodes.Insert(2, New TreeNode("I am inserted at the 3th position"))

'removing is done much in the same way:
N = TreeView1.Nodes.Add("I need to be removed").Nodes.Add("and all children too")
TreeView1.Nodes.Remove(N)

'to clear all branches of any node you can use clear()
N.Nodes.Add("This child you will not see")
N.Nodes.Clear()

'if you use Clear on the treeview nodes itself, you
' would once again have an empty treeview

'once an item has been added, it is part of the item collection in nodes
'this means you can access it by its index
TreeView1.Nodes(0).Text = "I have index 0"

'the behaviour of the treenode can be controlled completely in code.
'you can make it expand
TreeView1.Nodes(0).Expand()

'and retract again
TreeView1.Nodes(0).Collapse()

This is just the basic functionality. There are a lot more properties on a treeview. Some are inherited, some are specific to the treeview object. The sample above should give enough information to create your own treeview. Have fun thumb up!

To finish: an example
Code:

'this example will fill a treeview with the hours of the day.
'the hours will be divided in 15 minute blocks and the blocks
'will be divided in minutes
Sub Example()
Dim N As New TreeNode(), I As Integer, J As Integer

'create a 15 minute block
For I = 0 To 3
With N.Nodes.Add((I * 15).ToString & "-" & ((I + 1) * 15 - 1).ToString)
'add the minutes
For J = 0 To 14
.Nodes.Add((J + I * 15).ToString)
Next
End With
Next

'add the hours and immediately add the blocks to the hours as well
Dim NodeToAdd As TreeNode
For I = 1 To 24
NodeToAdd = N.Clone
NodeToAdd.Text = I.ToString
TreeView1.Nodes.Add(NodeToAdd)
Next
End Sub

'the sub when a time has been chosen
Private Sub ItemChosen(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
Dim N As TreeNode = CType(sender, Windows.Forms.TreeView).SelectedNode
If N.GetNodeCount(False) = 0 Then 'it is 'last' in the line
Dim S As String = N.Text
If S.Length = 1 Then S = S.Insert(0, "0")
S = N.Parent.Parent.Text & ":" & S
MessageBox.Show("You have selected: " & S)
End If
End Sub

Thursday, June 7, 2007

Shorties

I'm learning new stuff every day...

Use F7 key for bringing up the code window in VB.NET 2005. F5 for running/testing/debugging the program.

#Region "Name of Region" for categorising your blocks of code into separate expandable & collapsible nodes.

Use For Each loops to run through lists or classes or dictionaries & the like.

Whenever a particular section of code keeps getting reused a considerable number of times, it warrants making it into a separate subprocedure or function of its own.

Put short comment lines into parts of conditional blocks of code where nothing is done/executed. This is for future purposes, to prevent you thinking it's an error of sorts & waste time tweaking the code unnecessarily.