mike mcintyre's

.N e t J o u r n a l

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

JulAugust 2004Sep
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

Archives

Topics

Source Code

Sunday, August 29, 2004 #

Here is one approach to extending the My namespace. Using this approach an object is served up as a property in Intellisense when My. is typed.

INSTRUCTIONS

Create a Visual Studio 2005 project.

Add the code below to a new project file named:    My Extensions Example.vb

Add a Button1 to Form1.

Add a Button1 click handler to the code behind Form1.

Type  My. into the click handler code area to experiment with the new My.MyExtensions property.

' To the My namespace add a Module named MyExtensionExample.

' In the MyExtensionExample module use a Property to expose

' a MyExtensions object.

Namespace My

    ' Apply the HideModuleName attribute to the MyExtensionExample module.

    ' This will prevent 'MyExtensionExample' from appearing in intellisense.

    ' Instead client code will see only the

    _

    Module MyExtensionExample

        ' Declare a field named _MyExtensions of type MyExtensions.

        ' Instantiate a new MyExtensions object and assign it

        ' to the _MyExtensions variable.

        Private _MyExtensions As New MyExtensions

        ' Declare a property names MyExtensions of type MyExtensions.

        Friend Property MyExtensions() As MyExtensions

            Get

                Return _MyExtensions

            End Get

            Set(ByVal value As MyExtensions)

                _MyExtensions = value

            End Set

        End Property

    End Module

End Namespace

 

' MyExensions Class

' Exposed through MyExtensions property

' in the MyExtensionExample module.

Public Class MyExtensions

 

    ' Example member for MyExtensions class.

    Public ReadOnly Property GetPathToTextFile() As String

        Get

            ' Declare a variable named textOpenFiledDialog of type OpenFileDialog.

            ' Instantiate a new OpenFileDialog object and assign

            ' it to the textOpenFileDialog variable.

            Dim textOpenFileDialog As New OpenFileDialog

            ' Set the CheckFileExists property of the OpenFileDialog to True

            ' to ensure a valid file path is returned.

            textOpenFileDialog.CheckFileExists = True

            ' Set the Title property of the OpenFileDialog.

            textOpenFileDialog.Title = "Open a Text File"

            ' Set the Filter property of the OpenFileDialog so that

            ' .txt files will be shown in the dialog.

            textOpenFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"

            ' Show the OpenFileDialog.

            ' If the user clicks the OK button on the OpenFileDialog...

            If textOpenFileDialog.ShowDialog = DialogResult.OK Then

                ' Return the FileName property of the OpenFileDialog; this

                ' will be a path to a file as a string.

                Return textOpenFileDialog.FileName

            Else

                ' User did not pick a file so return Nothing.

                Return Nothing

            End If

        End Get

    End Property

 

End Class

posted @ 1:01 PM

FYI - Here is a code example that expands on an example found on the 'Shortcuts to Visual Basic Development with My' help topic found in help for Visual Studio 2005 Beta 1.  To try it out create a Windows Forms application, add a Button to Form1, add the code below to the button handler.

        ' Code below demostrate these Visual Basic 2005 features:

        '   1.  The generic ReadOnlyCollection.

        '   2.  The My.Computer.FileSystem.GetDirectories Method.

        '   3.  The My.Computer.FileSystem.SpecialDirectories.MyDocuments Property.

 

        ' Get and display directoryPaths using My.Computer object.

        ' Declare a variable named directoryPaths of generic type ReadOnlyCollection

        ' of type String.

        Dim directoryPaths As Generic.ReadOnlyCollection(Of String)

        ' Call the My.Computer.FileSystem.GetDirectories method passing it

        ' these parameters:

        '   1.  Directory Path: My.Computer.FileSystem.SpecialDirectories.MyDocument

        '       property returns a string containing the path to the special

        '       directory MyDocuments.

        '   2.  Recurse: True to ask GetDirectories to recurse the sub directories

        '       of the directory path (1) provided.

        '   3.  Parameter Array of File Wildcards: "*Logs*", "My Pictures" - indicate

        '       the GetDirectories method should look for directories whose names

        '       contain 'Logs' and directories named 'My Pictures'.

        '  Assign the ReadOnlyCollection object returned from the GetDirectories call

        ' to the directoryPaths variable.

        directoryPaths = My.Computer.FileSystem.GetDirectories _

        (My.Computer.FileSystem.SpecialDirectories.MyDocuments, True, "*Logs*", "My Pictures")

 

        ' Declare a variable of type string named directoryReport.

        Dim directoryReport As String = "Directory Paths Found" & vbCrLf & vbCrLf

        ' Declare a varialbe of type string named directoryPath.

        Dim directoryPath As String

        ' Use the directoryPath variable to loop through the directoryPath

        ' ReadOnlyCollection object.

        For Each directoryPath In directoryPaths

            ' Append the directorPath string and a line feed to the

            ' directoryReport string.

            directoryReport &= directoryPath & vbCrLf

        Next

 

        MsgBox(directoryReport)

posted @ 1:00 PM