Click here to Skip to main content
15,886,639 members
Articles / Programming Languages / Visual Basic

GUI Observer - Using the Observer pattern to handle data updates to complex linked controls

Rate me:
Please Sign up or sign in to vote.
4.09/5 (8 votes)
31 May 200613 min read 67.9K   404   51  
Use the Observer pattern to encapsulate control update code (adding items to lists etc.) in seperate classes outside of the form, and handle it when the code is called.
Imports System.IO
Public Class TraditionalForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        myInit()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lstDrives As System.Windows.Forms.ListBox
    Friend WithEvents lstFolders As System.Windows.Forms.ListBox
    Friend WithEvents lstFiles As System.Windows.Forms.ListBox
    Friend WithEvents grpFileInfo As System.Windows.Forms.GroupBox
    Friend WithEvents lstInfo As System.Windows.Forms.ListBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lstDrives = New System.Windows.Forms.ListBox
        Me.lstFolders = New System.Windows.Forms.ListBox
        Me.lstFiles = New System.Windows.Forms.ListBox
        Me.grpFileInfo = New System.Windows.Forms.GroupBox
        Me.lstInfo = New System.Windows.Forms.ListBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.grpFileInfo.SuspendLayout()
        Me.SuspendLayout()
        '
        'lstDrives
        '
        Me.lstDrives.Location = New System.Drawing.Point(32, 24)
        Me.lstDrives.Name = "lstDrives"
        Me.lstDrives.Size = New System.Drawing.Size(128, 160)
        Me.lstDrives.TabIndex = 0
        '
        'lstFolders
        '
        Me.lstFolders.Location = New System.Drawing.Point(176, 24)
        Me.lstFolders.Name = "lstFolders"
        Me.lstFolders.Size = New System.Drawing.Size(120, 160)
        Me.lstFolders.TabIndex = 1
        '
        'lstFiles
        '
        Me.lstFiles.Location = New System.Drawing.Point(312, 24)
        Me.lstFiles.Name = "lstFiles"
        Me.lstFiles.Size = New System.Drawing.Size(120, 160)
        Me.lstFiles.TabIndex = 2
        '
        'grpFileInfo
        '
        Me.grpFileInfo.Controls.Add(Me.lstInfo)
        Me.grpFileInfo.Location = New System.Drawing.Point(32, 192)
        Me.grpFileInfo.Name = "grpFileInfo"
        Me.grpFileInfo.Size = New System.Drawing.Size(400, 120)
        Me.grpFileInfo.TabIndex = 3
        Me.grpFileInfo.TabStop = False
        Me.grpFileInfo.Text = "File Info"
        '
        'lstInfo
        '
        Me.lstInfo.Dock = System.Windows.Forms.DockStyle.Fill
        Me.lstInfo.Location = New System.Drawing.Point(3, 16)
        Me.lstInfo.Name = "lstInfo"
        Me.lstInfo.Size = New System.Drawing.Size(394, 95)
        Me.lstInfo.TabIndex = 0
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(32, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(128, 16)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Drives"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(176, 8)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(128, 16)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Top Level Folders"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(312, 8)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(136, 16)
        Me.Label3.TabIndex = 6
        Me.Label3.Text = "Files In Top Level Folder"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 326)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.grpFileInfo)
        Me.Controls.Add(Me.lstFiles)
        Me.Controls.Add(Me.lstFolders)
        Me.Controls.Add(Me.lstDrives)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.grpFileInfo.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub myInit()
        populateDrives()
    End Sub

    Private Sub populateDrives()
        ' Initalise the drives box only
        Dim drives() As String
        Dim drive As String
        drives = IO.Directory.GetLogicalDrives()
        lstDrives.Items.Clear()
        For Each drive In drives
            lstDrives.Items.Add(drive)
        Next
        populateFolders()
    End Sub
    Private Sub populateFolders()
        Dim folders() As String
        Dim folder As String
        Dim drive As String

        lstFolders.Items.Clear()
        If (lstDrives.SelectedIndex >= 0) Then
            drive = lstDrives.SelectedItem
            folders = Directory.GetDirectories(drive)
            ' Initalise the drives box only
            For Each folder In folders
                lstFolders.Items.Add(folder)
            Next
        End If
        populateFiles()
    End Sub
    Private Sub populateFiles()
        Dim files() As String
        Dim file As String
        Dim folder As String

        lstFiles.Items.Clear()
        If (lstFolders.SelectedIndex >= 0) Then
            folder = lstFolders.SelectedItem
            files = Directory.GetFiles(folder)
            ' Initalise the drives box only
            For Each file In files
                lstFiles.Items.Add(file)
            Next
            If files.Length > 0 Then lstFiles.SelectedIndex = 0
        End If
        populateInfo()
    End Sub
    Private Sub populateInfo()
        Dim file As String
        Dim info As FileInfo
        lstInfo.Items.Clear()
        If (lstFiles.SelectedIndex >= 0) Then
            file = lstFiles.SelectedItem
            info = New FileInfo(file)
            ' Initalise the drives box only
            lstInfo.Items.Add("Created: " & info.CreationTime)
            lstInfo.Items.Add("Size: " & info.Length)
            lstInfo.Items.Add("Attributes: " & info.Attributes.ToString())
        End If
    End Sub

    Private Sub lstDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDrives.SelectedIndexChanged
        populateFolders()
    End Sub

    Private Sub lstFolders_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFolders.SelectedIndexChanged
        populateFiles()
    End Sub

    Private Sub lstFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFiles.SelectedIndexChanged
        populateInfo()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions