Important alert: (current site time 7/15/2013 8:24:14 AM EDT)
 

VB icon

ControlSorter

Email
Submitted on: 9/18/2003 9:53:11 PM
By: Scott Greenberg  
Level: Intermediate
User Rating: Unrated
Compatibility: VB.NET
Views: 9693
author picture
(About the author)
 
     Have you ever wasted time looking for your VB classes? Ever wonder why Visual Studio’s class drop-down list is not sorted? ControlSorter is a macro written in VB that automates the sorting process of form control declarations. As a result, the control classes can be found sorted in the class name drop-down box. The following sorting methods are supported: Control Name Ascending, Control Name Descending, Control Type Ascending, and Control Type Descending.

 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :ControlSorter
//**************************************
Copyright (c) SG Technology. All rights reserved.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: ControlSorter
// Description:Have you ever wasted time looking for your VB classes? Ever wonder why Visual Studio’s class drop-down list is not sorted? ControlSorter is a macro written in VB that automates the sorting process of form control declarations. As a result, the control classes can be found sorted in the class name drop-down box. The following sorting methods are supported: Control Name Ascending, Control Name Descending, Control Type Ascending, and Control Type Descending.
// By: Scott Greenberg
//
// Inputs:Setup:
Open Macro IDE, in VSStudio click Tools | Macros | Macros IDE… (Alt+F11)
Add new class, in Macro IDS click File | Add New Item… | select Class | name “ControlSorter”
Paste code into class, Copy code from web | in Macro IDS click Edit | Select All (Ctrl+A) | Edit | Paste
Inputs:
Select all the declarations listed in the region "Windows Form Designer generated code". Execute ControlSorter, specify sorting order, and click ok.
//
// Returns:Each control declarations are re-listed such that all are sorted. ControlSorter progress can be seen in VS statusbar.
//
// Assumes:It is assumed that all controls are visually added to form prior of executing ControlSorter. ControlSorter can be executed on as few as two declarations. There is no limit on how may time ControlSorter can be executed.
//
// Side Effects:Class name drop-down box is sort by name or type. 
If for any reason, ControlSorter closes prematurely there is a risk of the undo event becoming locked. To resolve this issues execute the macro ErrorClose.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1581&lngWId=10//for details.//**************************************

''' Copyright (c) SG Technology. All rights reserved.
'''
Option Strict Off
Option Explicit On 
Imports EnvDTE
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
''' ControlSorter checks the selected text for control declarations. 
''' Declarations are then sorted such that they are display in a sorted 
''' matter in the module drop-down list.
''' Setup:
''' Open Macro IDE, in VSStudio click Tools | Macros | Macros IDE… (Alt+F11)
''' Add new class, in Macro IDS click File | Add New Item… | select Class | name “ControlSorter”
''' Paste code into class, Copy code from web | in Macro IDS click Edit | Select All (Ctrl+A) | Edit | Paste
Public Module ControlSorter
Sub SortControls()
Dim Progress As Integer = 0
Dim Controls As New Controls()
Dim selection As TextSelection = DTE.ActiveDocument.Selection
Dim start As EditPoint = selection.TopPoint.CreateEditPoint()
Dim endpt As TextPoint = selection.BottomPoint
Dim objActive As VirtualPoint = selection.ActivePoint
Dim TS As TextSelection = DTE.ActiveDocument.Selection
Dim Insertion As String, Line As String
Dim Lines() As String = TS.Text.Split(vbNewLine)
If GetFileType = "none" Then
MsgBox(Prompt:="This macro is only valid for use with .net code files.", Title:=DTE.ActiveDocument.Name)
Exit Sub
End If
DTE.ActiveDocument.Windows.DTE.SuppressUI = True
UpdateProgress(Progress, Lines.Length * 2)
DTE.UndoContext.Open("Sort Control")
Try
For Each Line In Lines
Dim NewControl As New Control(Line)
Controls.Add(NewControl)
Next
Select Case InputBox("How would you like your controls sorted?" & vbNewLine & vbNewLine & _
"Enter 1 for NameAsc, 2 for NameDesc, 3 for TypeAsc, 4 for TypeDesc, or 5 to cancel.", "ControlSorter", "1")
Case "1"
Controls.Sort(enuSortOrder.NameAsc)
Case "2"
Controls.Sort(enuSortOrder.NameDesc)
Case "3"
Controls.Sort(enuSortOrder.TypeAsc)
Case "4"
Controls.Sort(enuSortOrder.TypeDesc)
Case Else
Exit Sub
End Select
Dim Control As Control
Dim SortedTest As String = ""
For Each Control In Controls
If InStr(Control.Deleration, Chr(10)) = 0 Then Control.Deleration = Chr(10) & Control.Deleration
'SortedTest += Control.Deleration
DTE.ActiveDocument.Selection.Text = Control.Deleration
Progress += 1
UpdateProgress(Progress, Lines.Length)
Next
'DTE.ActiveDocument.Selection.Text = SortedTest
TS.EndOfLine()
Finally
DTE.StatusBar.Progress(False)
DTE.ActiveDocument.Windows.DTE.SuppressUI = False
DTE.UndoContext.Close()
End Try
End Sub
'
'// In rare cases this sub should be executed to regain control of undo
'
Sub ErrorClose()
DTE.UndoContext.Close()
End Sub
'
'
'
Private Sub UpdateProgress(ByRef Progress As Integer, ByVal total As Integer)
Dim Title As String = "ControlSorter"
If Progress > total Then
Progress = total
Title = "Completed ControlSorter successfully"
End If
DTE.StatusBar.Progress(True, Title, Progress, total)
End Sub
Public ReadOnly Property GetFileType() As String
Get
Dim Type As String
With DTE.ActiveDocument
Type = .Name.Remove(0, InStrRev(.Name, ".", -1, CompareMethod.Text))
End With
Select Case Type
Case "vb"
Return Type
Case Else
Return "none"
End Select
End Get
End Property
End Module
#Region " Control"
Public Class CControlSort
Implements IComparer
Dim lngCompType As Integer
Public Sub New(ByVal xlngCompType As Integer)
lngCompType = xlngCompType
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim xName, yName As String
Dim xType, yType As String
Select Case lngCompType
Case enuSortOrder.NameAsc
Compare = CType(x, Control).Name.ToString < CType(y, Control).Name.ToString
Case enuSortOrder.NameDesc
Compare = CType(x, Control).Name.ToString > CType(y, Control).Name.ToString
Case enuSortOrder.TypeAsc
Compare = CType(x, Control).Type.ToString < CType(y, Control).Type.ToString
Case enuSortOrder.TypeDesc
Compare = CType(x, Control).Type.ToString > CType(y, Control).Type.ToString
Case Else
Compare = CType(x, Control).Name.ToString < CType(y, Control).Name.ToString
End Select
End Function
End Class
Public Enum enuSortOrder
NameAsc = 0
NameDesc = 1
TypeAsc = 2
TypeDesc = 3
End Enum
Public Class Control
Public Deleration As String
Public Name As String
Public Type As String
Public Sub New(ByVal value As String)
MyBase.New()
Deleration = value
Name = Left(value, Strings.InStr(value, "As", CompareMethod.Text) - 2)
Name = Trim(Name)
Name = Right(Name, Name.Length - Strings.InStrRev(Name, " "))
Name = Trim(Name)
Type = Trim(value)
Type = Right(Type, Type.Length - Strings.InStrRev(Type, " "))
Type = Trim(Type)
End Sub
End Class
Public Class Controls
Inherits CollectionBase
Public Function Add(ByRef value As Control) As Control
InnerList.Add(value)
Add = value
End Function
Public Function Remove(ByRef value As Control) As Control
InnerList.Remove(value)
Remove = value
End Function
Public Sub Sort(Optional ByVal xlngSortOrder As enuSortOrder = enuSortOrder.NameAsc)
InnerList.Sort(New CControlSort(xlngSortOrder))
End Sub
End Class
#End Region


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.