troubleshooting Question

VB.Net save textfile from tab that was dynamically added with control.

Avatar of fastburst
fastburst asked on
.NET Programming
6 Comments1 Solution383 ViewsLast Modified:
I have an internesting situation that I have not found an answer too. I have a form that dynamically adds a new tab when a file is opened to a control (basically a richtext box that highlights syntax and adds line numbers to it.)

That part works fine ai can open as many files as I want no issues.

The problem is when I try to save any file on whatever tab it saves as a blank file. I believe this is because the control was added dynamically.

How can I make it save the file on the currently selected tab and get the control name as well that was created dynamically.

Here is the code below.
Imports System.IO
Imports System.IO.FileStream

Public Class Form2
    'Dim editor As New Alsing.Windows.Forms.SyntaxBoxControl
    Dim doc As New Alsing.SourceCode.SyntaxDocument
    Dim TabCount As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Removes the selected tab:
        TabControl1.TabPages.Remove(TabControl1.SelectedTab)
        ' Removes all the tabs:
        'TabControl1.TabPages.Clear()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim editor As New Alsing.Windows.Forms.SyntaxBoxControl
        Dim target = TabControl1.SelectedIndex
        editor.Dock = DockStyle.Fill
        'Me.TabControl1.TabPages.Add("New Page")
        'Dim tp As TabPage = Me.TabControl1.TabPages(Me.TabControl1.TabPages.Count - 1)
        Dim tp As New TabPage("TabPage" & (Me.TabControl1.TabCount + 1))
        tp.Controls.Add(editor)
        Me.TabControl1.TabPages.Add(tp)
        tp.Padding = New Padding(3)
        'page.Margin = New Padding(3)
        'tp.Controls.Add(editor)
        Me.TabControl1.SelectedTab = tp
    End Sub

#Region "Functions"

    Private Sub SaveFile(Optional ByVal editor As String = Nothing)
        'Dim editor As New Alsing.Windows.Forms.SyntaxBoxControl
        Dim FileWriter As StreamWriter
        Dim results As DialogResult
        SaveFileDialog1.Filter = "All Files(*.*)|*.*|Cold Fusion (*.cfm;*.cfc)|*.cfm;*.cfc|Text files (*.txt)|*.txt"
        results = SaveFileDialog1.ShowDialog
        If results = DialogResult.OK Then
            FileWriter = New StreamWriter(SaveFileDialog1.FileName, False)
            FileWriter.Write(editor)
            FileWriter.Close()
        End If
    End Sub

    Private Sub OpenFile()
        Dim editor As New Alsing.Windows.Forms.SyntaxBoxControl
        Dim target = TabControl1.SelectedIndex
        editor.Dock = DockStyle.Fill
        editor.Document.SyntaxFile = My.Application.Info.DirectoryPath.Trim("\") & "\SyntaxFiles\PHP.syn"
        'Me.TabControl1.TabPages.Add("New Page")
        'Dim tp As TabPage = Me.TabControl1.TabPages(Me.TabControl1.TabPages.Count - 1)
        Dim FileReader As StreamReader
        Dim results As DialogResult
        Dim OPD As New OpenFileDialog
        OPD.Title = "Select a File to Open"
        OPD.Filter = "All Files(*.*)|*.*|Cold Fusion(*.cfm;*.cfc)|*.cfm;*.cfc|Text files (*.txt)|*.txt"
        results = OPD.ShowDialog

        If results = DialogResult.OK Then
            Dim MyFile As FileInfo = New FileInfo(OPD.FileName)
            Dim TabTitle = MyFile.Name
            Dim tp As New TabPage(TabTitle)
            'Dim tp As New TabPage("TabPage" & (Me.TabControl1.TabCount + 1))
            tp.Controls.Add(editor)
            Me.TabControl1.TabPages.Add(tp)
            tp.Padding = New Padding(3)
            Me.TabControl1.SelectedTab = tp

            FileReader = New StreamReader(OPD.FileName)
            txtLabel1.Text = OPD.FileName
            TabControl1.SelectedTab = TabControl1.SelectedTab
            editor.Document.Text = FileReader.ReadToEnd()
            FileReader.Close()
        End If
    End Sub

    Private Sub CloseApp()
        Me.Close()
    End Sub

#End Region

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFile()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFile(editor.document.text)
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        CloseApp()
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        txtLabel1.Text = TabControl1.Tag
    End Sub
End Class

Please show the changes. Also as you can see in the code i have tried adding the Editor (Control) to the class form first that didnt work out too well as it worked, however, when it would create a new tab the previous tabs control would disappear completely. Thus why I commented it out.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros