Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / Visual Basic

Synchro

Rate me:
Please Sign up or sign in to vote.
4.43/5 (13 votes)
20 May 2009GPL35 min read 74.4K   1K   75  
A control to synchronize folder contents.
Public Class frmMain
    Inherits System.Windows.Forms.Form

    Private mnuRecentFileList() As MenuItem  ' Array of menu items to store the last files

#Region " Code g�n�r� par le Concepteur Windows Form "

    Public Sub New()
        MyBase.New()

        'Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

    End Sub

    'La m�thode substitu�e Dispose du formulaire pour nettoyer la liste des composants.
    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

    'Requis par le Concepteur Windows Form
    Private components As System.ComponentModel.IContainer

    'REMARQUE�: la proc�dure suivante est requise par le Concepteur Windows Form
    'Elle peut �tre modifi�e en utilisant le Concepteur Windows Form.  
    'Ne la modifiez pas en utilisant l'�diteur de code.
    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuQuit As System.Windows.Forms.MenuItem
    Friend WithEvents mnuWindow As System.Windows.Forms.MenuItem
    Friend WithEvents mnuNew As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSaveAs As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOpen As System.Windows.Forms.MenuItem
    Friend WithEvents mnuClose As System.Windows.Forms.MenuItem
    Public WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents mnuCascade As System.Windows.Forms.MenuItem
    Friend WithEvents mnuTile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSave As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSepAfterClose As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSepBeforeMRU As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSepAfterMRU As System.Windows.Forms.MenuItem
    Friend WithEvents mnuH As System.Windows.Forms.MenuItem
    Friend WithEvents mnuHelp As System.Windows.Forms.MenuItem
    Friend WithEvents mnuAbout As System.Windows.Forms.MenuItem
    Friend WithEvents HelpProvider1 As System.Windows.Forms.HelpProvider
    Friend WithEvents mnuTools As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOptions As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.mnuFile = New System.Windows.Forms.MenuItem
        Me.mnuNew = New System.Windows.Forms.MenuItem
        Me.mnuOpen = New System.Windows.Forms.MenuItem
        Me.mnuClose = New System.Windows.Forms.MenuItem
        Me.mnuSepAfterClose = New System.Windows.Forms.MenuItem
        Me.mnuSave = New System.Windows.Forms.MenuItem
        Me.mnuSaveAs = New System.Windows.Forms.MenuItem
        Me.mnuSepBeforeMRU = New System.Windows.Forms.MenuItem
        Me.mnuSepAfterMRU = New System.Windows.Forms.MenuItem
        Me.mnuQuit = New System.Windows.Forms.MenuItem
        Me.mnuTools = New System.Windows.Forms.MenuItem
        Me.mnuOptions = New System.Windows.Forms.MenuItem
        Me.mnuWindow = New System.Windows.Forms.MenuItem
        Me.mnuCascade = New System.Windows.Forms.MenuItem
        Me.mnuTile = New System.Windows.Forms.MenuItem
        Me.mnuH = New System.Windows.Forms.MenuItem
        Me.mnuHelp = New System.Windows.Forms.MenuItem
        Me.mnuAbout = New System.Windows.Forms.MenuItem
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.HelpProvider1 = New System.Windows.Forms.HelpProvider
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuTools, Me.mnuWindow, Me.mnuH})
        Me.MainMenu1.RightToLeft = CType(resources.GetObject("MainMenu1.RightToLeft"), System.Windows.Forms.RightToLeft)
        '
        'mnuFile
        '
        Me.mnuFile.Enabled = CType(resources.GetObject("mnuFile.Enabled"), Boolean)
        Me.mnuFile.Index = 0
        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuNew, Me.mnuOpen, Me.mnuClose, Me.mnuSepAfterClose, Me.mnuSave, Me.mnuSaveAs, Me.mnuSepBeforeMRU, Me.mnuSepAfterMRU, Me.mnuQuit})
        Me.mnuFile.MergeType = System.Windows.Forms.MenuMerge.MergeItems
        Me.mnuFile.Shortcut = CType(resources.GetObject("mnuFile.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuFile.ShowShortcut = CType(resources.GetObject("mnuFile.ShowShortcut"), Boolean)
        Me.mnuFile.Text = resources.GetString("mnuFile.Text")
        Me.mnuFile.Visible = CType(resources.GetObject("mnuFile.Visible"), Boolean)
        '
        'mnuNew
        '
        Me.mnuNew.Enabled = CType(resources.GetObject("mnuNew.Enabled"), Boolean)
        Me.mnuNew.Index = 0
        Me.mnuNew.Shortcut = CType(resources.GetObject("mnuNew.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuNew.ShowShortcut = CType(resources.GetObject("mnuNew.ShowShortcut"), Boolean)
        Me.mnuNew.Text = resources.GetString("mnuNew.Text")
        Me.mnuNew.Visible = CType(resources.GetObject("mnuNew.Visible"), Boolean)
        '
        'mnuOpen
        '
        Me.mnuOpen.Enabled = CType(resources.GetObject("mnuOpen.Enabled"), Boolean)
        Me.mnuOpen.Index = 1
        Me.mnuOpen.Shortcut = CType(resources.GetObject("mnuOpen.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuOpen.ShowShortcut = CType(resources.GetObject("mnuOpen.ShowShortcut"), Boolean)
        Me.mnuOpen.Text = resources.GetString("mnuOpen.Text")
        Me.mnuOpen.Visible = CType(resources.GetObject("mnuOpen.Visible"), Boolean)
        '
        'mnuClose
        '
        Me.mnuClose.Enabled = CType(resources.GetObject("mnuClose.Enabled"), Boolean)
        Me.mnuClose.Index = 2
        Me.mnuClose.Shortcut = CType(resources.GetObject("mnuClose.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuClose.ShowShortcut = CType(resources.GetObject("mnuClose.ShowShortcut"), Boolean)
        Me.mnuClose.Text = resources.GetString("mnuClose.Text")
        Me.mnuClose.Visible = CType(resources.GetObject("mnuClose.Visible"), Boolean)
        '
        'mnuSepAfterClose
        '
        Me.mnuSepAfterClose.Enabled = CType(resources.GetObject("mnuSepAfterClose.Enabled"), Boolean)
        Me.mnuSepAfterClose.Index = 3
        Me.mnuSepAfterClose.Shortcut = CType(resources.GetObject("mnuSepAfterClose.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuSepAfterClose.ShowShortcut = CType(resources.GetObject("mnuSepAfterClose.ShowShortcut"), Boolean)
        Me.mnuSepAfterClose.Text = resources.GetString("mnuSepAfterClose.Text")
        Me.mnuSepAfterClose.Visible = CType(resources.GetObject("mnuSepAfterClose.Visible"), Boolean)
        '
        'mnuSave
        '
        Me.mnuSave.Enabled = CType(resources.GetObject("mnuSave.Enabled"), Boolean)
        Me.mnuSave.Index = 4
        Me.mnuSave.Shortcut = CType(resources.GetObject("mnuSave.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuSave.ShowShortcut = CType(resources.GetObject("mnuSave.ShowShortcut"), Boolean)
        Me.mnuSave.Text = resources.GetString("mnuSave.Text")
        Me.mnuSave.Visible = CType(resources.GetObject("mnuSave.Visible"), Boolean)
        '
        'mnuSaveAs
        '
        Me.mnuSaveAs.Enabled = CType(resources.GetObject("mnuSaveAs.Enabled"), Boolean)
        Me.mnuSaveAs.Index = 5
        Me.mnuSaveAs.Shortcut = CType(resources.GetObject("mnuSaveAs.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuSaveAs.ShowShortcut = CType(resources.GetObject("mnuSaveAs.ShowShortcut"), Boolean)
        Me.mnuSaveAs.Text = resources.GetString("mnuSaveAs.Text")
        Me.mnuSaveAs.Visible = CType(resources.GetObject("mnuSaveAs.Visible"), Boolean)
        '
        'mnuSepBeforeMRU
        '
        Me.mnuSepBeforeMRU.Enabled = CType(resources.GetObject("mnuSepBeforeMRU.Enabled"), Boolean)
        Me.mnuSepBeforeMRU.Index = 6
        Me.mnuSepBeforeMRU.MergeOrder = 10
        Me.mnuSepBeforeMRU.Shortcut = CType(resources.GetObject("mnuSepBeforeMRU.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuSepBeforeMRU.ShowShortcut = CType(resources.GetObject("mnuSepBeforeMRU.ShowShortcut"), Boolean)
        Me.mnuSepBeforeMRU.Text = resources.GetString("mnuSepBeforeMRU.Text")
        Me.mnuSepBeforeMRU.Visible = CType(resources.GetObject("mnuSepBeforeMRU.Visible"), Boolean)
        '
        'mnuSepAfterMRU
        '
        Me.mnuSepAfterMRU.Enabled = CType(resources.GetObject("mnuSepAfterMRU.Enabled"), Boolean)
        Me.mnuSepAfterMRU.Index = 7
        Me.mnuSepAfterMRU.Shortcut = CType(resources.GetObject("mnuSepAfterMRU.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuSepAfterMRU.ShowShortcut = CType(resources.GetObject("mnuSepAfterMRU.ShowShortcut"), Boolean)
        Me.mnuSepAfterMRU.Text = resources.GetString("mnuSepAfterMRU.Text")
        Me.mnuSepAfterMRU.Visible = CType(resources.GetObject("mnuSepAfterMRU.Visible"), Boolean)
        '
        'mnuQuit
        '
        Me.mnuQuit.Enabled = CType(resources.GetObject("mnuQuit.Enabled"), Boolean)
        Me.mnuQuit.Index = 8
        Me.mnuQuit.MergeOrder = 11
        Me.mnuQuit.Shortcut = CType(resources.GetObject("mnuQuit.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuQuit.ShowShortcut = CType(resources.GetObject("mnuQuit.ShowShortcut"), Boolean)
        Me.mnuQuit.Text = resources.GetString("mnuQuit.Text")
        Me.mnuQuit.Visible = CType(resources.GetObject("mnuQuit.Visible"), Boolean)
        '
        'mnuTools
        '
        Me.mnuTools.Enabled = CType(resources.GetObject("mnuTools.Enabled"), Boolean)
        Me.mnuTools.Index = 1
        Me.mnuTools.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuOptions})
        Me.mnuTools.Shortcut = CType(resources.GetObject("mnuTools.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuTools.ShowShortcut = CType(resources.GetObject("mnuTools.ShowShortcut"), Boolean)
        Me.mnuTools.Text = resources.GetString("mnuTools.Text")
        Me.mnuTools.Visible = CType(resources.GetObject("mnuTools.Visible"), Boolean)
        '
        'mnuOptions
        '
        Me.mnuOptions.Enabled = CType(resources.GetObject("mnuOptions.Enabled"), Boolean)
        Me.mnuOptions.Index = 0
        Me.mnuOptions.Shortcut = CType(resources.GetObject("mnuOptions.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuOptions.ShowShortcut = CType(resources.GetObject("mnuOptions.ShowShortcut"), Boolean)
        Me.mnuOptions.Text = resources.GetString("mnuOptions.Text")
        Me.mnuOptions.Visible = CType(resources.GetObject("mnuOptions.Visible"), Boolean)
        '
        'mnuWindow
        '
        Me.mnuWindow.Enabled = CType(resources.GetObject("mnuWindow.Enabled"), Boolean)
        Me.mnuWindow.Index = 2
        Me.mnuWindow.MdiList = True
        Me.mnuWindow.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuCascade, Me.mnuTile})
        Me.mnuWindow.Shortcut = CType(resources.GetObject("mnuWindow.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuWindow.ShowShortcut = CType(resources.GetObject("mnuWindow.ShowShortcut"), Boolean)
        Me.mnuWindow.Text = resources.GetString("mnuWindow.Text")
        Me.mnuWindow.Visible = CType(resources.GetObject("mnuWindow.Visible"), Boolean)
        '
        'mnuCascade
        '
        Me.mnuCascade.Enabled = CType(resources.GetObject("mnuCascade.Enabled"), Boolean)
        Me.mnuCascade.Index = 0
        Me.mnuCascade.Shortcut = CType(resources.GetObject("mnuCascade.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuCascade.ShowShortcut = CType(resources.GetObject("mnuCascade.ShowShortcut"), Boolean)
        Me.mnuCascade.Text = resources.GetString("mnuCascade.Text")
        Me.mnuCascade.Visible = CType(resources.GetObject("mnuCascade.Visible"), Boolean)
        '
        'mnuTile
        '
        Me.mnuTile.Enabled = CType(resources.GetObject("mnuTile.Enabled"), Boolean)
        Me.mnuTile.Index = 1
        Me.mnuTile.Shortcut = CType(resources.GetObject("mnuTile.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuTile.ShowShortcut = CType(resources.GetObject("mnuTile.ShowShortcut"), Boolean)
        Me.mnuTile.Text = resources.GetString("mnuTile.Text")
        Me.mnuTile.Visible = CType(resources.GetObject("mnuTile.Visible"), Boolean)
        '
        'mnuH
        '
        Me.mnuH.Enabled = CType(resources.GetObject("mnuH.Enabled"), Boolean)
        Me.mnuH.Index = 3
        Me.mnuH.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuHelp, Me.mnuAbout})
        Me.mnuH.Shortcut = CType(resources.GetObject("mnuH.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuH.ShowShortcut = CType(resources.GetObject("mnuH.ShowShortcut"), Boolean)
        Me.mnuH.Text = resources.GetString("mnuH.Text")
        Me.mnuH.Visible = CType(resources.GetObject("mnuH.Visible"), Boolean)
        '
        'mnuHelp
        '
        Me.mnuHelp.Enabled = CType(resources.GetObject("mnuHelp.Enabled"), Boolean)
        Me.mnuHelp.Index = 0
        Me.mnuHelp.Shortcut = CType(resources.GetObject("mnuHelp.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuHelp.ShowShortcut = CType(resources.GetObject("mnuHelp.ShowShortcut"), Boolean)
        Me.mnuHelp.Text = resources.GetString("mnuHelp.Text")
        Me.mnuHelp.Visible = CType(resources.GetObject("mnuHelp.Visible"), Boolean)
        '
        'mnuAbout
        '
        Me.mnuAbout.Enabled = CType(resources.GetObject("mnuAbout.Enabled"), Boolean)
        Me.mnuAbout.Index = 1
        Me.mnuAbout.Shortcut = CType(resources.GetObject("mnuAbout.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuAbout.ShowShortcut = CType(resources.GetObject("mnuAbout.ShowShortcut"), Boolean)
        Me.mnuAbout.Text = resources.GetString("mnuAbout.Text")
        Me.mnuAbout.Visible = CType(resources.GetObject("mnuAbout.Visible"), Boolean)
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.Filter = resources.GetString("OpenFileDialog1.Filter")
        Me.OpenFileDialog1.Title = resources.GetString("OpenFileDialog1.Title")
        '
        'HelpProvider1
        '
        Me.HelpProvider1.HelpNamespace = resources.GetString("HelpProvider1.HelpNamespace")
        '
        'frmMain
        '
        Me.AccessibleDescription = resources.GetString("$this.AccessibleDescription")
        Me.AccessibleName = resources.GetString("$this.AccessibleName")
        Me.AllowDrop = True
        Me.AutoScaleBaseSize = CType(resources.GetObject("$this.AutoScaleBaseSize"), System.Drawing.Size)
        Me.AutoScroll = CType(resources.GetObject("$this.AutoScroll"), Boolean)
        Me.AutoScrollMargin = CType(resources.GetObject("$this.AutoScrollMargin"), System.Drawing.Size)
        Me.AutoScrollMinSize = CType(resources.GetObject("$this.AutoScrollMinSize"), System.Drawing.Size)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.ClientSize = CType(resources.GetObject("$this.ClientSize"), System.Drawing.Size)
        Me.Enabled = CType(resources.GetObject("$this.Enabled"), Boolean)
        Me.Font = CType(resources.GetObject("$this.Font"), System.Drawing.Font)
        Me.HelpProvider1.SetHelpKeyword(Me, resources.GetString("$this.HelpKeyword"))
        Me.HelpProvider1.SetHelpNavigator(Me, CType(resources.GetObject("$this.HelpNavigator"), System.Windows.Forms.HelpNavigator))
        Me.HelpProvider1.SetHelpString(Me, resources.GetString("$this.HelpString"))
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.ImeMode = CType(resources.GetObject("$this.ImeMode"), System.Windows.Forms.ImeMode)
        Me.IsMdiContainer = True
        Me.Location = CType(resources.GetObject("$this.Location"), System.Drawing.Point)
        Me.MaximumSize = CType(resources.GetObject("$this.MaximumSize"), System.Drawing.Size)
        Me.Menu = Me.MainMenu1
        Me.MinimumSize = CType(resources.GetObject("$this.MinimumSize"), System.Drawing.Size)
        Me.Name = "frmMain"
        Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.HelpProvider1.SetShowHelp(Me, CType(resources.GetObject("$this.ShowHelp"), Boolean))
        Me.StartPosition = CType(resources.GetObject("$this.StartPosition"), System.Windows.Forms.FormStartPosition)
        Me.Text = resources.GetString("$this.Text")

    End Sub

#End Region

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Display the main window

        Dim blnArgumentError As Boolean = False

        ' Read program options in HKLM
        Dim reg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(conRegistryBaseSubKey)
        ' Read the help path in the registry (set by InstallShield)
        Dim strRegHelpPath As String = reg.GetValue("HelpPath", "").ToString
        ' Try to find the help file in the current culture
        If System.IO.File.Exists(strRegHelpPath & "\" & _
            Microsoft.VisualBasic.Left(Threading.Thread.CurrentThread.CurrentUICulture.Name, 2) & "\" & "Synchro.chm") Then
            ' If the help file  relative to the current UI culture (2 letters) is found, set it to the help provider. 
            Me.HelpProvider1.HelpNamespace = strRegHelpPath & "\" & _
            Microsoft.VisualBasic.Left(Threading.Thread.CurrentThread.CurrentUICulture.Name, 2) & "\" & "Synchro.chm"
        Else
            ' Use the default culture help file
            Me.HelpProvider1.HelpNamespace = strRegHelpPath & "\" & "Synchro.chm"
        End If

        ' Read user options in HKCU
        reg = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(conRegistryBaseSubKey)
        ' Recent File List length
        Dim strRegRecentFileListLength As String = reg.GetValue("RecentFileListLength", "").ToString
        If strRegRecentFileListLength <> "" Then
            Dim bytRegRecentFileListLength As Byte = CType(strRegRecentFileListLength, Byte)
            If (bytRegRecentFileListLength > 0) And (bytRegRecentFileListLength <= 10) Then
                bytRecentFileListLength = bytRegRecentFileListLength
            End If
        End If
        ' Refresh frequency
        Dim strRegRefreshFrequency As String = reg.GetValue("RefreshFrequency", "").ToString
        If strRegRefreshFrequency <> "" Then
            Dim intRegRefreshFrequency As Integer = CInt(strRegRefreshFrequency)
            If (intRegRefreshFrequency >= 100) And (intRegRefreshFrequency <= 1000) Then
                intRefreshFrequency = intRegRefreshFrequency
            End If
        End If

        ' Recent file list
        ReDim mnuRecentFileList(bytRecentFileListLength - 1)
        ' Read the registry and update the File menu
        RefreshMRUList()

        ' Read the command line options
        If Not (CommandLineArgs Is Nothing) Then
            ' If arguments were sent

            Dim strFile As String = cmdArguments(0)
            If (strFile.Chars(0) <> "-") And (strFile.Chars(0) <> "/") Then
                ' The first argument is not an option, so it is a file name
                blnArgumentError = Not OpenTheFile(strFile)
                '   Open it
            Else
                ' The first argument is an option: open a new document
                mnuNew_Click(Me, e)
                '   Run File/new command
            End If

            ' Action
            If blnArgumentError Then
                ' An error happened
                Me.Show()
                MsgBox(LocRM.GetString("strSyntaxTitle") & vbCrLf & LocRM.GetString("strSyntaxDetail") _
                & vbCrLf & vbCrLf & LocRM.GetString("strSyntaxExample"), MsgBoxStyle.Critical, "Synchro")
                ErrorStatus = 1
                '   The application exits with an error
                Application.Exit()
            Else
                ' Eventually change options according to the command line and run
                Dim ActiveDocument As frmDocument = CType(ActiveMdiChild, frmDocument)
                ActiveDocument.SetOptions(CommandLineArgs)
            End If
        End If

    End Sub

#Region "Drag and Drop"

    Private Sub frmMain_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
        ' A file (or several files) can be dropped into the form
        Dim fileList As String() = CType(e.Data.GetData("FileDrop"), String())
        Dim strFileI As String
        For Each strFileI In fileList
            If Not OpenTheFile(strFileI) Then
                ' The file can not be opened. Error message. The user can cancel
                If MsgBox(LocRM.GetString("strErrReadFile") & strFileI, MsgBoxStyle.OKCancel, "Synchro") = MsgBoxResult.Cancel Then
                    Exit For
                End If
            End If
        Next strFileI
    End Sub

    Private Sub frmMain_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
        ' The mouse is entering...
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

#End Region

#Region "File menu"

    Private Sub mnuNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuNew.Click
        ' Create a new synchronisation configuration

        ' Open a window
        Dim NewMDIChild As New frmDocument

        With NewMDIChild
            ' Give it a name
            gintDocNb = gintDocNb + 1
            ' And a caption
            .Text = "Synchro" & gintDocNb.ToString
            ' Set the Parent Form of the Child window.
            .MdiParent = Me
            ' Set the help file
            .HelpProvider1.HelpNamespace = Me.HelpProvider1.HelpNamespace
            ' Display the new form.
            .Show()
            ' Set it clean
            .IsDirty = False
        End With

    End Sub

    Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
        ' Open an existing file

        Dim DialogResult As DialogResult = OpenFileDialog1.ShowDialog()

        If DialogResult = DialogResult.OK Then
            ' Open it
            If Not OpenTheFile(OpenFileDialog1.FileName) Then
                ' Error message
                MsgBox(LocRM.GetString("strErrReadFile") & OpenFileDialog1.FileName, MsgBoxStyle.Critical, "Synchro")
            End If
        End If

    End Sub

    Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClose.Click
        ' Close the MDI child
        ActiveMdiChild.Close()
    End Sub

    Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
        ' Save the file

        Dim ActiveDocument As frmDocument = CType(ActiveMdiChild, frmDocument)

        If ActiveDocument.strDocName = "" Then
            ' If the file has no name, use Save As...
            mnuSaveAs_Click(sender, e)
        Else
            ' Save
            ActiveDocument.Savefile()
        End If
    End Sub

    Private Sub mnuSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveAs.Click
        ' Saves the folder content

        Dim ActiveDocument As frmDocument = CType(ActiveMdiChild, frmDocument)
        ' Do not treat the dialog result
        ActiveDocument.ChooseFileNameAndSave()

    End Sub

    Private Sub mnuMRU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' One of the last files
        Dim mnuItem As MenuItem = CType(sender, MenuItem)
        Dim strFile As String = Mid(mnuItem.Text, 2 + mnuItem.Text.IndexOf(" "))
        ' Open the file with the given name (delete the file number)
        If Not OpenTheFile(strFile) Then
            ' If failed, delete the file from the MRU
            UpdateMRUList(strFile, True)
            MsgBox(LocRM.GetString("strErrReadFile"), MsgBoxStyle.Critical, "Synchro")
            ' Update the file menu
            RefreshMRUList()
        End If
    End Sub

    Private Sub mnuQuit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuQuit.Click
        Me.Close()
    End Sub

    Public Sub UpdateMRUList(ByVal strLastFile As String, Optional ByVal blnFailed As Boolean = False)
        ' Update the registry and the MRU list in the File menu
        ' strLastFile is the last file name, sent by the savefile sub. It must be added.
        ' if blnFailed is true, the last file is not correct (eg deleted). It must be removed.

        ' Read the registry and put the list into a table
        Dim reg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(conRegistryBaseSubKey & "\Recent")
        ' Read the MRU registry key
        Dim strRecentList As String = reg.GetValue("RecentList", "").ToString
        ' Reads the first 6 chars of the string
        ' Read the MRU list, as a 6 character string such as "abcdef". Each letter is a value, containing a file.
        strRecentList = Mid(strRecentList, 1, bytRecentFileListLength)
        Dim intI As Integer                                         ' Each char in the recent file list
        Dim RecentFiles(bytRecentFileListLength - 1, 1) As String   ' Table of 6 recent files, 0=File letter a-f, 1=File name
        For intI = 0 To strRecentList.Length - 1
            ' Read the registry and fill the table
            RecentFiles(intI, 0) = strRecentList.Chars(intI)
            RecentFiles(intI, 1) = reg.GetValue(strRecentList.Chars(intI), "").ToString
        Next

        ' Clean up the list and put it back to the registry
        Dim blnFileIsInMRUList As Boolean = False ' The file has been recorded in the MRU list
        Dim charAvailableLetter As Char = CChar("-") ' An available letter in the MRU list
        strRecentList = ""
        For intI = 0 To bytRecentFileListLength - 1
            ' Read the table and write the registry
            If RecentFiles(intI, 1) = "" Then
                ' If the file name is empty, ignore (e.g. the registry has been modified directly)
                If (RecentFiles(intI, 0) >= "a") And (RecentFiles(intI, 0) < Chr(97 + bytRecentFileListLength)) Then
                    ' The char must be valid. Remember the letter for a future use
                    charAvailableLetter = CChar(RecentFiles(intI, 0))
                End If
            ElseIf RecentFiles(intI, 1) = strLastFile Then
                ' If this file is in the MRU list
                If Not blnFailed Then
                    ' The file must be added. Make it first. Else, do nothing: the file will be forgotten.
                    strRecentList = RecentFiles(intI, 0) & strRecentList
                    blnFileIsInMRUList = True
                End If
            Else
                ' Record it in the last position
                strRecentList = strRecentList & RecentFiles(intI, 0)
            End If
        Next

        ' Add the last file into the registry
        If Not blnFailed Then
            ' Ignore if the file is to be deleted from the list
            If charAvailableLetter = "-" Then
                ' If no available letter was found in the list (usual)
                For intI = 97 To 96 + bytRecentFileListLength
                    ' Try each letter from a to f in the recent list
                    If strRecentList.IndexOf(Chr(intI)) = -1 Then
                        ' The char is not in the list: remember it and exit
                        charAvailableLetter = Chr(intI)
                        Exit For
                    End If
                Next
            End If
            If Not blnFileIsInMRUList Then
                ' The file is not in the MRU list
                If charAvailableLetter = "" Then
                    ' No available letter
                    strRecentList = strRecentList.Chars(bytRecentFileListLength - 1) & strRecentList
                    ' Make the last letter first and cut at the list length
                    strRecentList = Mid(strRecentList, 1, bytRecentFileListLength)
                    ' Save
                    reg.SetValue(strRecentList.Chars(0), strLastFile)
                Else
                    ' Add the first letter
                    strRecentList = charAvailableLetter & strRecentList
                    ' Save
                    reg.SetValue(charAvailableLetter, strLastFile)
                End If
            End If
        End If

        ' Finally, save the new MRU list
        reg.SetValue("RecentList", strRecentList)

    End Sub

    Public Sub RefreshMRUList()
        ' Refresh the File menu last files : read the registry and change the File menu

        Dim intI As Integer

        For intI = 0 To bytRecentFileListLength - 1
            ' For each recent file
            If Not mnuRecentFileList(intI) Is Nothing Then
                ' If the menuitem exists, delete it
                mnuFile.MenuItems.Remove(mnuRecentFileList(intI))
                mnuRecentFileList(intI) = Nothing
            End If
        Next

        Dim reg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(conRegistryBaseSubKey & "\Recent")
        ' Open the MRU registry key
        Dim strRecentList As String = reg.GetValue("RecentList", "").ToString
        ' Reads the first 6 chars of the string
        ' Read the MRU list, as a 6 character string such as "abcdef". Each letter is a value, containing a file.
        strRecentList = Mid(strRecentList, 1, bytRecentFileListLength)
        For intI = 0 To strRecentList.Length - 1
            ' Read the registry and fill the File menu. Create each menu item, and declares its click event handler
            mnuRecentFileList(intI) = New MenuItem("&" & (intI + 1).ToString & " " & reg.GetValue(strRecentList.Chars(intI), "").ToString, AddressOf mnuMRU_Click)
            ' Add the menu item table to the main menu, before the bottom separator
            mnuFile.MenuItems.Add(mnuSepAfterMRU.Index, mnuRecentFileList(intI))
        Next

        ' If there is no recent file, hide the separator
        mnuSepAfterMRU.Visible = (strRecentList.Length > 0)

        Me.Refresh()

    End Sub

    Private Function OpenTheFile(ByRef strFileName As String) As Boolean
        ' Open the chosen file. Must be a valid .syn file.

        Dim blnFileReadOK As Boolean = True
        Dim txtSource, txtTarget, txtOptions, txtLogFile, txtExclude As String
        Dim syncOptions As Synchro.SyncOptions
        Dim sr As System.IO.StreamReader
        Dim frm As frmDocument

        ' Check if the file is already open
        For Each frm In Me.MdiChildren
            If frm.strDocName.ToLower = strFileName.ToLower Then
                ' The file is already open. Continue will recall the disk version
                If MsgBox(LocRM.GetString("strFileAlreadyOpen") & " ( " & strFileName & ")" & vbCrLf _
                & LocRM.GetString("strAskIgnoreChanges"), MsgBoxStyle.YesNo, "Synchro") = MsgBoxResult.Yes Then
                    ' Set it clean to avoid "Save the file ?" message
                    frm.IsDirty = False
                    ' Close it. All changes are lost.
                    frm.Close()
                Else
                    ' Stop opening the file
                    Exit Function
                End If
            End If
        Next

        Try
            sr = System.IO.File.OpenText(strFileName)
            With sr
                ' Read the source, the target and the options
                txtSource = .ReadLine()
                txtTarget = .ReadLine()
                txtOptions = .ReadLine()
                txtLogFile = .ReadLine()
                txtExclude = .ReadLine()
                .Close()
                syncOptions = CType(txtOptions, SyncOptions)
            End With
        Catch
            blnFileReadOK = False
        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
        End Try

        If blnFileReadOK Then
            ' The file was read 
            Dim NewMDIChild As New frmDocument
            '   Open a window
            With NewMDIChild
                ' Give it a name and remember the file name
                .Text = Mid(strFileName, strFileName.LastIndexOf("\") + 2)
                .strDocName = strFileName
                ' Set the Parent Form of the Child window.
                .MdiParent = Me
                ' Set the properties
                .HelpProvider1.HelpNamespace = Me.HelpProvider1.HelpNamespace
                .txtSource.Text = txtSource
                .txtTarget.Text = txtTarget
                .txtLogFile.Text = txtLogFile
                .txtExclude.Text = txtExclude
                .chkS.Checked = (syncOptions And syncOptions.SyncCopySubDirectories) = syncOptions.SyncCopySubDirectories
                .chkE.Checked = (syncOptions And syncOptions.SyncCopyEmptySubDirectories) = syncOptions.SyncCopyEmptySubDirectories
                .chkR.Checked = (syncOptions And syncOptions.SyncOverWriteReadOnly) = syncOptions.SyncOverWriteReadOnly
                .chkD.Checked = (syncOptions And syncOptions.SyncPreserveNewerFiles) = syncOptions.SyncPreserveNewerFiles
                .chkU.Checked = (syncOptions And syncOptions.SyncCopyOnlyExistingFiles) = syncOptions.SyncCopyOnlyExistingFiles
                .chkZ.Checked = (syncOptions And syncOptions.SyncSynchronize) = syncOptions.SyncSynchronize
                .chkN.Checked = (syncOptions And syncOptions.SyncDontPromptBeforeCreatingFiles) = syncOptions.SyncDontPromptBeforeCreatingFiles
                .chkY.Checked = (syncOptions And syncOptions.SyncDontPromptBeforeDeletingFiles) = syncOptions.SyncDontPromptBeforeDeletingFiles
                .chkW.Checked = (syncOptions And syncOptions.SyncPromptBeforeClosingWindow) = syncOptions.SyncPromptBeforeClosingWindow
                .chkG.Checked = (syncOptions And syncOptions.SyncDontStopOnErrors) = syncOptions.SyncDontStopOnErrors
                .chkL.Checked = (syncOptions And syncOptions.SyncLog) = syncOptions.SyncLog
                ' Set isDirty to false because changes have made it true
                .IsDirty = False
                'Display the new form.
                .Show()
            End With
        End If

        Return blnFileReadOK
    End Function

#End Region

#Region "Tools menu"

    Private Sub mnuOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOptions.Click
        ' Open the options

        Dim frmOptions As New frmOptions
        With frmOptions
            ' Help provider
            .HelpProvider1.HelpNamespace = Me.HelpProvider1.HelpNamespace
            ' Prepare the values
            .numRecentLength.Value = CType(bytRecentFileListLength, Decimal)
            .tbRefreshFrequency.Value = intRefreshFrequency

            If .ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
                ' If OK chosen
                Dim reg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(conRegistryBaseSubKey)
                reg.SetValue("RecentFileListLength", .numRecentLength.Value)
                reg.SetValue("RefreshFrequency", .tbRefreshFrequency.Value)
                intRefreshFrequency = .tbRefreshFrequency.Value
            End If
        End With

    End Sub

#End Region

#Region "Windows menu"

    Private Sub mnuCascade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCascade.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub mnuTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuTile.Click
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub frmMain_MdiChildActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MdiChildActivate
        ' A child is activated : activate the document menus

        If ActiveMdiChild Is Nothing Then
            ' No child window
            ' Disable close, save and save as menus
            mnuClose.Enabled = False
            mnuSave.Enabled = False
            mnuSaveAs.Enabled = False
        Else
            ' Enable close, save and save as menus
            mnuClose.Enabled = True
            mnuSave.Enabled = True
            mnuSaveAs.Enabled = True
        End If

    End Sub

#End Region

#Region "Help menu"

    Private Sub mnuHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelp.Click
        Help.ShowHelpIndex(Me, Me.HelpProvider1.HelpNamespace)
    End Sub

    Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        ' About...
        Dim frmAbout As New AboutBox
        With frmAbout
            .AppDescription = LocRM.GetString("strAssemblyDescription")
            .AppMoreInfo = "http://E.Marcon.Free.fr/Synchro"
            .ShowDialog(Me)
        End With
    End Sub

#End Region

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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


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

Comments and Discussions