HelpMulti-Tab Controls

Posts 1–3 of 3 · Page 1 of 1
Multi-Tab Controls
I never figured this out yet

I want this:
While using the program, create a new tab and be able to use universal controls, to the controls of the new tab.

I tried using 3 methods, didn't quite work out :/
I typically use a combination of a TabControl (or any variant thereof) and a UserControl when building multiple-tabbed applications. You can create a UserControl in Visual Studio (Project >> Add User Control) which will give you a designer view, where you can create the look of your UserControl (i.e the layout you want in each tab), and it allows you to add instance-specific event handlers which refer only to the current UserControl (so each tab controls itself and nothing else).

Adding it is fairly straightforward, I just overcomplicated the shit out of this function to make it generic. ENJOY!

Code:
Private Shared Function AddUserTab(Of T As {New, UserControl})(ByVal title As String, ByVal control As TabControl) As TabPage

    Dim page As New TabPage(title) With {.Name = GetType(TabPage).Name & control.TabPages.Count.ToString()} 'basic naming
    Dim uc As New T() With {.Name = GetType(T).Name & "1"} 'initialize a new instance of the generic type (hence the New() constraint)
    uc.Dock = DockStyle.Fill 'fill the whole tab

    page.Controls.Add(uc) 'add the UserControl to the page (obviously)
    control.TabPages.Add(page) 'finally, add the TabPage to the UserControl.

    Return page
End Function
Fuck VB.NET is ugly as shit.

To use it is fairly straightforward:
Code:
Dim tab As TabPage = AddUserPage(Of <Name_Of_Your_UserControl_Class_Here>)("I like boys", Me.TabControl1)
Now I'm trying to get control of a selected tab (Like richtextbox of tab#)


I tried this,
Code:
 Private Shared _MainWriter As UserControl
    Public Shared Property MainWriter() As Writer
        Get
            Return _MainWriter
        End Get
        Set(ByVal value As Writer)
            _MainWriter = value
        End Set
    End Property
but I get an error (Object reference not set to an instance of an object.)


My code

Code:
Private Sub Open()
        ' Check()

        Dim Odlg As New OpenFileDialog
        With Odlg
            .Title = "Save"
            .Filter = "Text files |*.txt|Richtext files |*.rtf|All files |*.*"
            .RestoreDirectory = True
        End With

        If Odlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Filename = Odlg.FileName
            Safefilename = System****.Path.GetFileName(Filename)
            Dim ext As String = System****.Path.GetExtension(Odlg.FileName)
            ext = ext.ToUpper()

            Dim tab As TabPage = AppManager.AddUserTab(Of Writer)("Untitled " & tabnumber, Me.TabControl1)
            tabnumber += 1
            TabControl1.SelectedTab = tab

            Select Case ext
                Case ".RTF"
                    AppManager.MainWriter.MyRichTextBox.LoadFile(Filename)
                    AppManager.MainWriter.MyRichTextBo*****lectionStart = 0
                    AppManager.MainWriter.MyRichTextBo*****lectionLength = 0
                Case ".TXT"
                    Dim txtReader As System****.StreamReader
                    txtReader = New System****.StreamReader(Odlg.FileName)
                    AppManager.MainWriter.MyRichTextBox.Text = txtReader.ReadToEnd
                    txtReader.Close()
                    txtReader = Nothing
                    AppManager.MainWriter.MyRichTextBo*****lectionStart = 0
                    AppManager.MainWriter.MyRichTextBo*****lectionLength = 0
                Case Else
                    MessageBox.Show("File type is not supported", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Select
        End If
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us