Public Class EditorForm 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 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 Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar Friend WithEvents TrackBar2 As System.Windows.Forms.TrackBar Public WithEvents Editor As System.Windows.Forms.RichTextBox Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents FileMenu As System.Windows.Forms.MenuItem Friend WithEvents EditMenu As System.Windows.Forms.MenuItem Friend WithEvents FormatMenu As System.Windows.Forms.MenuItem Friend WithEvents FileNew As System.Windows.Forms.MenuItem Friend WithEvents FileOpen As System.Windows.Forms.MenuItem Friend WithEvents FileSave As System.Windows.Forms.MenuItem Friend WithEvents FileSaveAs As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents FileExit As System.Windows.Forms.MenuItem Friend WithEvents EditCopy As System.Windows.Forms.MenuItem Friend WithEvents EditCut As System.Windows.Forms.MenuItem Friend WithEvents EditPaste As System.Windows.Forms.MenuItem Friend WithEvents MenuItem13 As System.Windows.Forms.MenuItem Friend WithEvents EditUndo As System.Windows.Forms.MenuItem Friend WithEvents MenuItem15 As System.Windows.Forms.MenuItem Friend WithEvents AlignMenu As System.Windows.Forms.MenuItem Friend WithEvents AlignLeft As System.Windows.Forms.MenuItem Friend WithEvents AlignCenter As System.Windows.Forms.MenuItem Friend WithEvents AlignRight As System.Windows.Forms.MenuItem Friend WithEvents FormatFont As System.Windows.Forms.MenuItem Friend WithEvents EditSelect As System.Windows.Forms.MenuItem Friend WithEvents EditFind As System.Windows.Forms.MenuItem Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog Friend WithEvents EditRedo As System.Windows.Forms.MenuItem 'Required by the Windows Form Designer Private components As System.ComponentModel.Container '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. Private Sub InitializeComponent() Me.FormatMenu = New System.Windows.Forms.MenuItem() Me.AlignMenu = New System.Windows.Forms.MenuItem() Me.AlignLeft = New System.Windows.Forms.MenuItem() Me.AlignCenter = New System.Windows.Forms.MenuItem() Me.AlignRight = New System.Windows.Forms.MenuItem() Me.FormatFont = New System.Windows.Forms.MenuItem() Me.EditCut = New System.Windows.Forms.MenuItem() Me.FileOpen = New System.Windows.Forms.MenuItem() Me.TrackBar2 = New System.Windows.Forms.TrackBar() Me.EditSelect = New System.Windows.Forms.MenuItem() Me.EditPaste = New System.Windows.Forms.MenuItem() Me.EditMenu = New System.Windows.Forms.MenuItem() Me.EditUndo = New System.Windows.Forms.MenuItem() Me.EditRedo = New System.Windows.Forms.MenuItem() Me.MenuItem15 = New System.Windows.Forms.MenuItem() Me.EditCopy = New System.Windows.Forms.MenuItem() Me.MenuItem13 = New System.Windows.Forms.MenuItem() Me.EditFind = New System.Windows.Forms.MenuItem() Me.FileExit = New System.Windows.Forms.MenuItem() Me.FileSave = New System.Windows.Forms.MenuItem() Me.FileNew = New System.Windows.Forms.MenuItem() Me.FontDialog1 = New System.Windows.Forms.FontDialog() Me.MenuItem8 = New System.Windows.Forms.MenuItem() Me.FileSaveAs = New System.Windows.Forms.MenuItem() Me.Editor = New System.Windows.Forms.RichTextBox() Me.FileMenu = New System.Windows.Forms.MenuItem() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog() Me.TrackBar1 = New System.Windows.Forms.TrackBar() CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'FormatMenu ' Me.FormatMenu.Index = 2 Me.FormatMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.AlignMenu, Me.FormatFont}) Me.FormatMenu.Text = "Format" ' 'AlignMenu ' Me.AlignMenu.Index = 0 Me.AlignMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.AlignLeft, Me.AlignCenter, Me.AlignRight}) Me.AlignMenu.Text = "Align" ' 'AlignLeft ' Me.AlignLeft.Index = 0 Me.AlignLeft.Text = "Left" ' 'AlignCenter ' Me.AlignCenter.Index = 1 Me.AlignCenter.Text = "Center" ' 'AlignRight ' Me.AlignRight.Index = 2 Me.AlignRight.Text = "Right" ' 'FormatFont ' Me.FormatFont.Index = 1 Me.FormatFont.Text = "Font" ' 'EditCut ' Me.EditCut.Index = 4 Me.EditCut.Text = "Cut" ' 'FileOpen ' Me.FileOpen.Index = 1 Me.FileOpen.Text = "Open" ' 'TrackBar2 ' Me.TrackBar2.LargeChange = 2 Me.TrackBar2.Location = New System.Drawing.Point(0, 24) Me.TrackBar2.Maximum = 16 Me.TrackBar2.Name = "TrackBar2" Me.TrackBar2.Size = New System.Drawing.Size(816, 42) Me.TrackBar2.TabIndex = 1 ' 'EditSelect ' Me.EditSelect.Index = 6 Me.EditSelect.Text = "Select All" ' 'EditPaste ' Me.EditPaste.Index = 5 Me.EditPaste.Text = "Paste" ' 'EditMenu ' Me.EditMenu.Index = 1 Me.EditMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.EditUndo, Me.EditRedo, Me.MenuItem15, Me.EditCopy, Me.EditCut, Me.EditPaste, Me.EditSelect, Me.MenuItem13, Me.EditFind}) Me.EditMenu.Text = "Edit" ' 'EditUndo ' Me.EditUndo.Index = 0 Me.EditUndo.Text = "Undo" ' 'EditRedo ' Me.EditRedo.Index = 1 Me.EditRedo.Text = "Redo" ' 'MenuItem15 ' Me.MenuItem15.Index = 2 Me.MenuItem15.Text = "-" ' 'EditCopy ' Me.EditCopy.Index = 3 Me.EditCopy.Text = "Copy" ' 'MenuItem13 ' Me.MenuItem13.Index = 7 Me.MenuItem13.Text = "-" ' 'EditFind ' Me.EditFind.Index = 8 Me.EditFind.Text = "Find" ' 'FileExit ' Me.FileExit.Index = 5 Me.FileExit.Text = "Exit" ' 'FileSave ' Me.FileSave.Index = 2 Me.FileSave.Text = "Save" ' 'FileNew ' Me.FileNew.Index = 0 Me.FileNew.Text = "New" ' 'MenuItem8 ' Me.MenuItem8.Index = 4 Me.MenuItem8.Text = "-" ' 'FileSaveAs ' Me.FileSaveAs.Index = 3 Me.FileSaveAs.Text = "Save As" ' 'Editor ' Me.Editor.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Me.Editor.HideSelection = False Me.Editor.Location = New System.Drawing.Point(12, 56) Me.Editor.Name = "Editor" Me.Editor.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical Me.Editor.Size = New System.Drawing.Size(824, 536) Me.Editor.TabIndex = 2 Me.Editor.Text = "" ' 'FileMenu ' Me.FileMenu.Index = 0 Me.FileMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FileNew, Me.FileOpen, Me.FileSave, Me.FileSaveAs, Me.MenuItem8, Me.FileExit}) Me.FileMenu.Text = "File" ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FileMenu, Me.EditMenu, Me.FormatMenu}) ' 'SaveFileDialog1 ' Me.SaveFileDialog1.FileName = "doc1" ' 'TrackBar1 ' Me.TrackBar1.LargeChange = 2 Me.TrackBar1.Maximum = 16 Me.TrackBar1.Name = "TrackBar1" Me.TrackBar1.Size = New System.Drawing.Size(816, 42) Me.TrackBar1.TabIndex = 0 ' 'EditorForm ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(832, 621) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Editor, Me.TrackBar2, Me.TrackBar1}) Me.Menu = Me.MainMenu1 Me.MinimumSize = New System.Drawing.Size(840, 648) Me.Name = "EditorForm" Me.Text = "RTFPad" CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Dim fName As String Dim fndForm As FindForm Public Shared RTFBox As RichTextBox Private Sub Editor_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Editor.SelectionChanged If Editor.SelectionIndent = Nothing Then TrackBar1.Value = TrackBar1.Minimum TrackBar2.Value = TrackBar2.Minimum Else TrackBar1.Value = Editor.SelectionIndent * TrackBar1.Maximum / Editor.Width TrackBar2.Value = (Editor.SelectionHangingIndent / Editor.Width) * TrackBar2.Maximum + TrackBar1.Value End If End Sub Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll Editor.SelectionIndent = Editor.Width * (TrackBar1.Value / TrackBar1.Maximum) Editor.SelectionHangingIndent = Editor.Width * (TrackBar2.Value / TrackBar2.Maximum) - Editor.SelectionIndent End Sub Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll Editor.SelectionHangingIndent = Editor.Width * (TrackBar2.Value / TrackBar2.Maximum) - Editor.SelectionIndent End Sub Private Sub FileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileNew.Click If DiscardChanges() Then Editor.Clear() End Sub Private Sub FileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileOpen.Click If DiscardChanges() Then OpenFileDialog1.Filter = "RTF Files|*.RTF|DOC Files|*.DOC|Text Files|*.TXT|All Files|*.*" If OpenFileDialog1.ShowDialog() = DialogResult.OK Then fName = OpenFileDialog1.FileName Editor.LoadFile(fName) Editor.Modified = False End If End If End Sub Private Sub FormatFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FormatFont.Click If Not Editor.SelectionFont Is Nothing Then FontDialog1.Font = Editor.SelectionFont Else FontDialog1.Font = Nothing End If FontDialog1.ShowApply = True If FontDialog1.ShowDialog() = DialogResult.OK Then Editor.SelectionFont = FontDialog1.Font End If End Sub Private Sub FileSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSaveAs.Click SaveFileDialog1.Filter = "RTF Files|*.RTF|DOC Files|*.DOC|Text Files|*.TXT|All Files|*.*" SaveFileDialog1.DefaultExt = "RTF" If SaveFileDialog1.ShowDialog() = DialogResult.OK Then fName = SaveFileDialog1.FileName Editor.SaveFile(fName) Editor.Modified = False End If End Sub Private Sub EditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditCut.Click Editor.Cut() End Sub Private Sub AlignRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlignRight.Click Editor.SelectionAlignment = HorizontalAlignment.Right End Sub Private Sub AlignCenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlignCenter.Click Editor.SelectionAlignment = HorizontalAlignment.Center End Sub Private Sub AlignLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlignLeft.Click Editor.SelectionAlignment = HorizontalAlignment.Left End Sub Private Sub EditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditPaste.Click Try Editor.Paste() Catch exc As Exception MsgBox("Can't paste current clipboard's contents") End Try End Sub Private Sub EditCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditCopy.Click Editor.Copy() End Sub Private Sub EditUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditUndo.Click If Editor.CanUndo Then Editor.Undo() End Sub Private Sub EditMenu_Select(ByVal sender As Object, ByVal e As System.EventArgs) Handles EditMenu.Select If Editor.UndoActionName <> "" Then EditUndo.Text = "Undo " & Editor.UndoActionName EditUndo.Enabled = True Else EditUndo.Text = "Undo" EditUndo.Enabled = False End If If Editor.RedoActionName <> "" Then EditRedo.Text = "Redo " & Editor.RedoActionName EditRedo.Enabled = True Else EditRedo.Text = "Redo" EditRedo.Enabled = False End If End Sub Private Sub FileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSave.Click If fName <> "" Then Editor.SaveFile(fName) Editor.Modified = False Else FileSaveAs_Click(sender, e) End If End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If DiscardChanges() Then End Else e.Cancel = True End If End Sub Function DiscardChanges() As Boolean If Editor.Modified Then Dim reply As MsgBoxResult reply = MsgBox("Text hasn't been saved. Discard changes?", MsgBoxStyle.YesNo) If reply = MsgBoxResult.No Then Return False Else Return True End If Else Return True End If End Function Private Sub EditFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditFind.Click If fndForm Is Nothing Then fndForm = New FindForm() End If fndForm.Show() End Sub Private Sub EditorForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load RTFBox = Editor End Sub Private Sub EditSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditSelect.Click Editor.SelectAll() End Sub Private Sub FileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileExit.Click If DiscardChanges() Then End End Sub Private Sub EditRedo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditRedo.Click If Editor.CanRedo Then Editor().Redo() End Sub Private Sub FontDialog1_Apply(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontDialog1.Apply Editor.SelectionFont = FontDialog1.Font End Sub End Class