Imports System.IO
Imports Docs.Word



Public Class Form1
	Friend DTW_Process As Process
	Friend OpenFiles As Boolean = True

	Public Shared ResultFolder As String = "..\..\_DocSamples\Result\"
	Public Shared SourceFolder As String = "..\..\_DocSamples\Source\"
	Public Shared ImagesFolder As String = "..\..\_Images\"
	Public Shared SourceCodeFld As String = "..\..\Sources\"
	Dim SampleNames As New List(Of String)(New String() _
	 {"HelloWorld", _
	 "OpenEditSave", _
	 "Sections - PageSetup", _
	 "Sections - Columns", _
	 "Sections - SectionBreaks", _
	 "Sections - HeadersFooters", _
	 "Paragraphs - Style", _
	 "Paragraphs - TabStops", _
	 "Paragraphs - CreateList", _
	 "Textruns - Style", _
	 "Textruns - Hyperlinks", _
	 "Textruns - Footnotes", _
	 "Textruns - Comments", _
	 "Textruns - InsertImage", _
	 "Tables - CreateTable", _
	 "Tables - NestedTable", _
	 "Tables - Style", _
	 "Documents - Merge", _
	 "Documents - SetProtection", _
	 "FormFields - Create", _
	 "FormFields - Fill", _
	 "Tools - Search", _
	 "Tools - Replace", _
	 "Tools - SaveAllImages"})



    Public Sub New()
        InitializeComponent()
        listBox1.SelectedIndex = 0
        listBox1.Focus()
    End Sub




	'	Get sample with given name as user select it in menu of samples
    Friend Sub GetSample(ByVal SampleName As String)
        Dim FileName As String = ImagesFolder & SampleName
        If File.Exists(FileName & "_Source.png") Then
            pictureBox1.Image = System.Drawing.Image.FromFile(FileName & "_Source.png")
        End If
        If File.Exists(FileName & "_Result.png") Then
            pictureBox2.Image = System.Drawing.Image.FromFile(FileName & "_Result.png")
        End If
        TabPage3.Text = "Source code - " & SampleName & ".vb"
        Code_TB.Enabled = False
        Code_TB.Text = "//  Also you can check the source code of this example here:" & vbLf & "//  Sources\" & SampleName & ".vb" & vbLf & vbLf
        Code_TB.Text += New StreamReader(SourceCodeFld & SampleName & ".vb").ReadToEnd().Replace(vbTab, "    ")

        '	Hightlight comments
        Dim k As Integer = Code_TB.Text.IndexOf("//", 70)
        While k <> -1
            Dim k2 As Integer = Code_TB.Text.IndexOf(vbLf, k + 1)
            Code_TB.[Select](k, k2 - k)
            Code_TB.SelectionColor = Color.DarkGreen
            k = Code_TB.Text.IndexOf("//", k2)
        End While
        Code_TB.Enabled = True
    End Sub



	'	Sample selection
	Friend Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles listBox1.SelectedIndexChanged
        GetSample(SampleNames(listBox1.SelectedIndex))
        tabControl1.SelectTab(1)
	End Sub



	'	Open Result File
    Friend Sub OpenResultFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OpenResultFile.Click
        Dim N As Integer = listBox1.SelectedIndex
        Select Case N
            Case 0 : WriteResults(Hello_World.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 1 : WriteResults(Open_Edit_Save.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 2 : WriteResults(Sections_PageSetup.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 3 : WriteResults(Sections_Columns.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 4 : WriteResults(Sections_SectionBreaks.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 5 : WriteResults(Sections_HeadersFooters.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 6 : WriteResults(Paragraphs_Style.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 7 : WriteResults(Paragraphs_TabStops.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 8 : WriteResults(Paragraphs_CreateList.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 9 : WriteResults(Textruns_Style.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 10 : WriteResults(Textruns_Hyperlinks.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 11 : WriteResults(Textruns_Footnotes.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 12 : WriteResults(Textruns_Comments.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 13 : WriteResults(Textruns_InsertImage.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 14 : WriteResults(Tables_CreateTable.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 15 : WriteResults(Tables_NestedTable.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 16 : WriteResults(Tables_Style.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 17 : WriteResults(Documents_Merge.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 18 : WriteResults(Documents_SetProtection.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 19 : WriteResults(FormFields_Create.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 20 : WriteResults(FormFields_Fill.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 21 : WriteResults(Tools_Search.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 22 : WriteResults(Tools_Replace.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
            Case 23 : WriteResults(Tools_SaveAllImages.Main1(), SampleNames(N), OpenFiles, True) : Exit Select
        End Select
    End Sub



	'	Write result document into DOC/DOCX/RTF files
	Friend Sub WriteResults(ByVal Doc As Document, ByVal FileName As String, ByVal OpenResultFile As Boolean, ByVal OpenDOCX As Boolean)
		Dim SrcFile As String = SourceFolder & FileName
		Dim ResFile As String = ResultFolder & FileName
		If Directory.Exists(ResultFolder) = False Then
			Directory.CreateDirectory(ResultFolder)
		End If

		'	Write the result document to the file with given names
		Doc.WriteDoc(ResFile & ".doc")
		Doc.WriteDocX(ResFile & ".docx")
		Doc.WriteRTF(ResFile & ".rtf")

		'	Open both files to compare
		'	You can change the extension of result file to check DOCX or RTF version
		If OpenResultFile = True Then
			If File.Exists(SrcFile & ".doc") = True Then
				Process.Start(SrcFile & ".doc")
			ElseIf File.Exists(SrcFile & ".docx") = True Then
				Process.Start(SrcFile & ".docx")
			End If
			If Not OpenDOCX And File.Exists(ResFile & ".doc") Then
				Process.Start(ResFile & ".doc")
			End If
			If OpenDOCX And File.Exists(ResFile & ".docx") Then
				Process.Start(ResFile & ".docx")
			End If
		End If
	End Sub



	'	When changing tabs between Source and Result - refresh properties if needed
	Private Sub tabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tabControl1.SelectedIndexChanged
        listBox1_SelectedIndexChanged(sender, e)
        tabControl1.SelectTab(1)
	End Sub



	'	Open Doc Tree Viewer
    Private Sub SeeDetails_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SeeDetails.Click
        OpenFiles = False
        OpenResultFile_Click(sender, e)
        OpenFiles = True
        Dim Index As Integer = listBox1.SelectedIndex
        Dim FN As String = ResultFolder + SampleNames(Index) & ".docx"
        DTW_Process = New Process()
        DTW_Process.StartInfo = New ProcessStartInfo("..\..\..\..\..\Bin\Doc_Tree_Viewer.exe", """" & FN & """")
        DTW_Process.Start()
    End Sub



	'	Close all threads when closing main Sample Manager form
	Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
		If DTW_Process IsNot Nothing Then
			If DTW_Process.HasExited = False Then
				DTW_Process.CloseMainWindow()
				DTW_Process.Close()
			End If
		End If
	End Sub

End Class
