Wednesday 9 July 2008

VB.NET Niceties

It's not often you hear about VB.NET in a good light. That's mainly for the reason it's a mainly misused tool. It works well for prototyping but not so brilliantly as a RAD tool. However, there are projects I have had to make use of it in and suprisingly there are some good bits.


 


Drag And Drop Made Easy


 


In VB6 (yes, I had to suffer it at school), doing anything remotely interesting required an obtuse call to some DLL. There was also a lot of hoping an praying over file version numbers as well. This made me think it would be excedingly difficult to do drag and drop in VB.NET. Fortunately, I was wrong.


 


To start, you will need both controls to have drag and drop enabled. You'll find the option in the properties box.


 


Secondly, you will need two methods. One for the "from" control. This example is from AllDay DJ's drag and drop player:


 


Private Sub lvResults_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvResults.MouseDown

            lvResults.HitTest(e.X, e.Y).Item.Selected = True
            lvResults.DoDragDrop(Me.selected_item, DragDropEffects.Copy)

End Sub


 


The HitTest call is only there to the item in the listview is selected on mouse down, before doing the drag and drop. You will need to replace Me.selected_item with the object you wish to send to the other control. You'll also need a receiver:


 


Private Sub dndPlayerDragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles btnBackDND.DragEnter...

        ' Check the source

        If (e.Data.GetDataPresent("AllDayDJ2.AudioItem")) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If

End Sub


 


In the above code, the object is type checked then a "copy" is allowed. Note that you could also use move or some other option but they must match.


 


Finally, you will need a third method to perform any action after the drop. Here we simply add the item to the playlist:


 


Private Sub dndPlayerDragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles btnBackDND.DragDrop...

        ' Load the item

        Dim item As AudioItem = DirectCast(e.Data.GetData("AllDayDJ2.AudioItem"), AudioItem)
        item = New AudioItem(item.getTrackId.ToString, item.getDbTable, True)
        Me.loadDNDPlayer(item)

End Sub



Here we simply cast the object (we have already type checked) then use it. The important line in this block of code is the first one. The others simply make a clean copy of an the item dragged over and load it into the player.

 


Feel free to use these examples. It's not too hard to find other examples. Just be sure not to use the AllDayDJ2 namespace for your own classes!


 


Documentation


 


 If you look at the C# examples in previous entries, you'll find comments starting with 3 slashes. These act kind of like the double star (*) comment in Java (aka JavaDocs).


 


However, based on my experience with VB6, I did not expect such a thing from VB.NET. Turns out I was wrong. Just start the comment with 3 apostrophes ('). From there, the Visual Studio IDE will auto-complete a comment section. It's then a matter of filling in the blanks.


 


The up-shot to this over normal comments is that proper documentation can be generated in a similar stye to JavaDocs. Also, just like in Netbeans and Eclipse, the IDE will display the documentation info in a tooltip. Very useful.


The down side. Redocumenting AllDay DJ 2.

No comments:

Post a Comment