|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionADCollectionsVisualizer is a Visual Studio 2005 Visualizer which allows a wide range of collection and dictionary-style objects to be easily examined. BackgroundOne of the fantastic new features of the Visual Studio 2005 IDE is the ability to use Visualizers. These are utility windows that attach themselves to various object types within the VS2005 environment. When you hover the mouse over a variable of one of the supported object types, a magnifying glass icon appears within the popup tooltip, and clicking it opens the Visualizer window for that particular variable, allowing the variable content to be viewed much more easily. Several Visualizers are included with VS2005 by default, to allow text to be viewed (as plain text, HTML or XML) and to allow DataTables to be examined more closely. The IDE allows further Visualizers to be written, however, such as ADCollectionsVisualizer. ADCollectionsVisualizer allows a whole variety of collection and dictionary classes (and objects for all classes that inherit from these classes) to be viewed in a grid, and also copied to the clipboard and saved to disk if required. Using the CodeThe visualizer can be installed by either running the installation executable, or by unzipping the DLL and placing it into your My Documents\Visual Studio 2005\Visualizers directory. Note that if you are compiling the visualizer from its source code, you can use a Build Event to automatically copy the visualizer to the installation directory. You can find this by double-clicking the My Project icon, then selecting the Compile tab and clicking the Build Events button (such a build event is configured within the provided project file — you will most likely need to change the path to wherever your My Documents directory is located). After this has been done, simply hover the mouse over a collection object within the IDE whilst in break mode. A small magnifying glass icon should appear; click on this to open the visualizer.
The visualizer supports a wide range of collection-type objects, including:
The code itself consists of three main classes:
<Assembly: DebuggerVisualizer(GetType(CollectionVisualizer),
GetType(ObjectSource), Target:=GetType(Collections.ArrayList),
Description:="Collection Visualizer")>
<Assembly: DebuggerVisualizer(GetType(CollectionVisualizer),
GetType(ObjectSource), Target:=GetType(Collections.BitArray),
Description:="Collection Visualizer")>
<Assembly: DebuggerVisualizer(GetType(CollectionVisualizer),
GetType(ObjectSource), Target:=GetType(Collections.Hashtable),
Description:="Collection Visualizer")>
[...etc...]
Notice that two of the project classes are referenced within every one of the attributes: the first is the If you wish to know more about how to write visualizers, you will find other articles within The Code Project that explain this in great detail. The visualizer uses a custom Finally the Points of InterestThere are a couple of areas of code that might prove useful or interesting to anyone that hasn't encountered them before — they certainly had me guessing for awhile. The first is the code that accesses the values within an object derived from I used a rather hacky piece of reflection to gain access to these functions. I definitely would not recommend using techniques such as this in production code, but in the context of what the visualizer is trying to do I think they are an acceptable technique. Without them, I would be unable to support collections based upon this class. The second thing that took some experimentation and research to get right is the customisation of the DataGrid. By default the grid allows the values to be edited (which is not something that the visualizer supports at present) and highlights the selected cell with a gray background. I created a class named The code that implements the '''
''' A custom DataGridTextBoxColumn that provides some changed functionality,
''' required by the visualizer
'''
Public Class CustomGridTextBoxColumn
Inherits DataGridTextBoxColumn
'''
''' When the grid attempts to go into Edit mode, this method will return
''' without calling into the base class, and so prevents the edit from
''' starting.
'''
Protected Overrides Sub Edit(ByVal source As CurrencyManager,
ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle,
ByVal [readOnly] As Boolean, ByVal displayText As String,
ByVal cellIsVisible As Boolean)
Return
End Sub
'''
''' The grid normally paints the active cell with a different colour to
''' the other cells. As our grid is read-only, this isn't a desirable
''' effect. This overridden Paint method
''' ensures that the cell is always painted to look the same, regardless
''' of whether it is active or not. We will also render Null values in
''' grey to distinguish them from the string "(null)".
'''
Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle,
ByVal source As System.Windows.Forms.CurrencyManager,
ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush,
ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)
Try
'Create a brush for the background
backBrush = New SolidBrush(SystemColors.Window)
'Retrieve the value for this cell
Dim o As Object = Me.GetColumnValueAtRow(source, rowNum)
'Is this a Null value?
If o Is DBNull.Value Then
'Yes, so we'll use grey text
foreBrush = New SolidBrush(SystemColors.GrayText)
Else
'This is a normal value, we'll use the standard foreground
'text colour
foreBrush = New SolidBrush(SystemColors.ControlText)
End If
'Call into the base class to do the work
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight)
Finally
'Clean up
If backBrush IsNot Nothing Then backBrush.Dispose()
If foreBrush IsNot Nothing Then foreBrush.Dispose()
End Try
End Sub
End Class
As you can see, two of the class procedures are overridden. The If you wish to learn more about changing and extending the behaviour of the DataGrid, you can find a huge amount of information in the DataGrid section of George Shepherd's Windows Forms FAQ. HistoryVersion 1.1 (2007-09-14)
Version 1.0 (2007-06-03)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||