Link to home
Start Free TrialLog in
Avatar of fastburst
fastburst

asked on

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

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

Open in new window


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.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fastburst
fastburst

ASKER

Well I will be ... That works perfectly.

Thanks Idle_Mind.
One last question. I have the status label set when the file is open.

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Dim tp As TabPage = TabControl1.SelectedTab
            txtLabel1.Text = tp.Name
    End Sub

When I change between 2 tabs I want it to redisplay the info again. The above just blanks it out.
I noticed if I change it to this
txtLabel1.Text = TabControl1.SelectedTab.ToString

it works somewhat however displays
TapPage: {filename}

How can I change it to just show the filename? or Tab Name I should say?
Nevermind I finally figured it out.

txtLabel1.Text = TabControl1.SelectedTab.Text

thats what I needed and works fine now.

Sorry...been gone all morning!...glad you figured it out!  =D