Creating A Text Editor In C# And Visual Basic (Part B Coding)

PART B - CODING


SETTING UP REGIONS AND METHODS

We will first setup our coding regions and methods, and there will be a few methods to create. In code view add the following regions and methods (to see code, click View > Code).

  #region Methods

      #region Tabs

        private void AddTab()
        {
        }

        private void RemoveTab()
        {
        }

        private void RemoveAllTabs()
        {
        }

        private void RemoveAllTabsButThis()
        {
        }

      #endregion

      #region SaveAndOpen

        private void Save()
        {
        }

        private void SaveAs()
        {
        }

        private void Open()
        {
        }

      #endregion

  #endregion
#Region "Methods"

#Region "Tabs"

    Private Sub AddTab()

    End Sub

    Private Sub RemoveTab()

    End Sub

    Private Sub RemoveAllTabs()

    End Sub

    Private Sub RemoveAllTabsButThis()

    End Sub
#End Region

#Region "SaveAndOpen"

    Private Sub Save()

    End Sub

    Private Sub SaveAs()

    End Sub

    Private Sub Open()

    End Sub

#End Region

#End Region

Sponsored Links

All the methods will remain private since we will not use them outside of the current class. Next create a global variable of data type integer and set the value to 0.

private int TabCount = 0;
Dim TabCount As Integer = 0

CREATING PROPERTIES

To avoid repetitive code, we will add a property which will provide us with the RichTextBox methods and properties. Below the Region Method add this:

        #region Properties

        private RichTextBox GetCurrentDocument
        {
            get { return (RichTextBox)tabControl1.SelectedTab.Controls["Body"]; }
        }

        #endregion
#Region "Properties"

    Private ReadOnly Property GetCurrentDocument() As RichTextBox
        Get
            Return CType(TabControl1.SelectedTab.Controls.Item("Body"), RichTextBox)
        End Get
    End Property

#End Region

The code will simply find the RichTextbox in the selected tab control (we will create the RichTextBox next).

CREATING THE METHODS

AddTab

            RichTextBox Body = new RichTextBox();

            Body.Name = "Body";
            Body.Dock = DockStyle.Fill;
            Body.ContextMenuStrip = contextMenuStrip1;
            
            TabPage NewPage = new TabPage();
            TabCount += 1;

            string DocumentText = "Document " + TabCount;
            NewPage.Name = DocumentText;
            NewPage.Text = DocumentText;
            NewPage.Controls.Add(Body);

            tabControl1.TabPages.Add(NewPage);
        Dim Body As New RichTextBox()

        Body.Name = "Body"
        Body.Dock = DockStyle.Fill;
        Body.ContextMenuStrip = ContextMenuStrip1

        Dim NewPage As New TabPage()
        TabCount += 1

        Dim DocumentText As String = "Document " & TabCount
        NewPage.Name = DocumentText
        NewPage.Text = DocumentText
        NewPage.Controls.Add(Body)

        TabControl1.TabPages.Add(NewPage)

The RichTextBox (from now on called RTB) is added to a new TabPage, and then the TabPage is added to the TabControls TabPages collection. Each time a new TabPage is added we increment TabCount by 1.

RemoveTab

            if (tabControl1.TabPages.Count != 1)
            {
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }
            else
            {
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
                AddTab();
            }
        If Not TabControl1.TabPages.Count = 1 Then
            TabControl1.TabPages.Remove(TabControl1.SelectedTab)
        Else
            TabControl1.TabPages.Remove(TabControl1.SelectedTab)
            AddTab()
        	End If

Here we check the amount of tabs in the TabControl. If it’s 1 then we remove the current tab and add another; otherwise remove the tab.

RemoveAllTabs

            foreach (TabPage Page in tabControl1.TabPages)
            {
                tabControl1.TabPages.Remove(Page);
            }

            AddTab();
        For Each Page As TabPage In TabControl1.TabPages
            TabControl1.TabPages.Remove(Page)
        Next

        AddTab()

 RemoveAllTabsButThis

foreach (TabPage Page in tabControl1.TabPages)
{
       if (Page.Name != tabControl1.SelectedTab.Name)
        {
                    tabControl1.TabPages.Remove(Page);
        }
}
        For Each Page As TabPage In TabControl1.TabPages
            If Not Page.Name = TabControl1.SelectedTab.Name Then
                TabControl1.TabPages.Remove(Page)
            End If
        Next

Similar to the above, this time we have an If Statement to check if the TabPage name is not equal to the selected TabPage and remove them all.

Save

            saveFileDialog1.FileName = tabControl1.SelectedTab.Name;
            saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "RTF|*.rtf";
            saveFileDialog1.Title = "Save";

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (saveFileDialog1.FileName.Length > 0)
                {
                    GetCurrentDocument.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
                }
            }
        SaveFileDialog1.FileName = TabControl1.SelectedTab.Name
        SaveFileDialog1.Filter = "RTF|*.rtf"
        SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog1.Title = "Save"

        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            If SaveFileDialog1.FileName.Length > 0 Then
                GetCurrentDocument.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
            End If

        End If

Firstly we set the saveFileDialog properties and then show it; if it’s OK (the user has clicked OK), the code inside the If Statement executes and saves the file.

Save As

            saveFileDialog1.FileName = tabControl1.SelectedTab.Name;
            saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "Text Files|*.txt|VB Files|*.vb|C# Files|*.cs|All Files|*.*";
            saveFileDialog1.Title = "Save As";

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (saveFileDialog1.FileName.Length > 0)
                {
                    GetCurrentDocument.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                }
            }
        SaveFileDialog1.FileName = TabControl1.SelectedTab.Name
        SaveFileDialog1.Filter = "Text Files|*.txt|VB Files|*.vb|C# Files|*.cs|All Files|*.*"
        SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog1.Title = "Save As"

        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            If SaveFileDialog1.FileName.Length > 0 Then
                GetCurrentDocument.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            End If

        End If

This is similar to the above code except we have changed filter and stream type.

Open

            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            openFileDialog1.Filter = "RTF|*.rtf|Text Files|*.txt|VB Files|*.vb|C# Files|*.cs|All Files|*.*";

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (openFileDialog1.FileName.Length > 9)
                {
                    GetCurrentDocument.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                }
            }
        OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog1.Filter = "RTF|*.rtf|Text Files|*.txt|VB Files|*.vb|C# Files|*.cs|All Files|*.*"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If OpenFileDialog1.FileName.Length > 0 Then
                GetCurrentDocument.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            End If

This is similar to the Save and SaveAs methods, but in reverse.

[ ]