I'm having hard time looking for a solution. :(
How can i add tabs without duplicating it on the tabpages.
I'm using list view to add tabs to the tabcontrol.

These are my codes:

Public Class frmMain

    Private Sub treeviewMain_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles treeviewMain.NodeMouseDoubleClick
        Dim page As Integer = tabMain.TabPages.Count
        Dim tabpage As New TabPage
        Dim id As Integer
        tabpage.Text = treeviewMain.SelectedNode.Text.ToString()

            Select Case tabpage.Text

                Case "Return"

                    tabMain.TabPages.Add(tabpage)
                    Dim frm As New frmReturn
                    frm.TopLevel = False
                    frm.Parent = tabMain.TabPages(page)
                    frm.Show()

                Case "Borrow"

                    tabMain.TabPages.Add(tabpage)
                    Dim frm As New frmBorrow
                    frm.TopLevel = False
                    frm.Parent = tabMain.TabPages(page)
                    frm.Show()
                    id = 2

                Case "OPAC"

                    tabMain.TabPages.Add(tabpage)
                    Dim frm As New frmOPAC
                    frm.TopLevel = False
                    frm.Parent = tabMain.TabPages(page)
                    frm.Show()

            End Select
    End Sub
End Class

THANK YOU!

Recommended Answers

All 14 Replies

I'm guessing that what you are trying to say is:

I have a form with a treeview control and a tab control. When the user double-clicks on a child node in the treeview I want to create a Tab page with the selected child node name but only if that Tab page does not already exist.

If that is correct then you can do

Private Sub TreeView1_DoubleClick(sender As Object, e As System.EventArgs) Handles TreeView1.DoubleClick

    Dim trv As TreeView = sender

    Select Case trv.SelectedNode.Text
        Case "Return", "Borrow", "OPAC"
            Dim newtab As String = trv.SelectedNode.Text
            If Not TabControl1.TabPages.ContainsKey(newtab) Then
                TabControl1.TabPages.Add(newtab, newtab)
            End If
    End Select

End Sub

Alternately, you can make the code more general by doing

Private Sub TreeView1_DoubleClick(sender As Object, e As System.EventArgs) Handles TreeView1.DoubleClick

    Dim trv As TreeView = sender

    If Not trv.SelectedNode Is trv.TopNode Then
        Dim newtab As String = trv.SelectedNode.Text
        If Not TabControl1.TabPages.ContainsKey(newtab) Then
            TabControl1.TabPages.Add(newtab, newtab)
        End If
    End If

End Sub

This allows you to add more childnodes and not have to change the double click handler accordingly.

Thanks a lot.
But now my problem is how to put forms on the pages?
I had tried this. But the pages only shows the frmBorrow.

    Private Sub treeviewMain_DoubleClick(sender As Object, e As System.EventArgs) Handles treeviewMain.DoubleClick
        Dim trv As TreeView = sender
        Select Case trv.SelectedNode.Text
            Case "Return", "Borrow", "OPAC"
                Dim newtab As String = trv.SelectedNode.Text
                If Not tabMain.TabPages.ContainsKey(newtab) Then
                    tabMain.TabPages.Add(newtab, newtab)
                    Dim frm As New frmBorrow
                    frm.TopLevel = False
                    frm.Parent = tabMain.TabPages(newtab)
                    frm.Show()
                End If
        End Select
    End Sub

You don't put a form on the page. A form is a top level control. You put controls on the page. There are several examples of how to create controls at run time in this forum. Have a look here and here.

I don't get it.

Try this

Private Sub TreeView1_DoubleClick(sender As Object, e As System.EventArgs) Handles TreeView1.DoubleClick

    Dim trv As TreeView = sender
    Dim tab As String = trv.SelectedNode.Text
    Dim newform As Form = Nothing

    If trv.SelectedNode Is trv.TopNode Then Exit Sub
    If TabControl1.TabPages.ContainsKey(tab) Then Exit Sub

    Select Case tab
        Case "Return"
            newform = New frmReturn
        Case "Borrow"
            newform = New frmBorrow
        Case "OPAC"
            newform = New frmOPAC
    End Select

    newform.TopLevel = False
    newform.Dock = DockStyle.Fill
    newform.Show()
    TabControl1.TabPages(tab).Controls.Add(newform)

End Sub

I've never done this myself but a quick test seems to show that's what I think you want. Please disregard my earlier You don't put a form on the page. Let's just chalk it up as a "senior" moment.

this is the error :D

I had created a code. But the code was too long. There are a lot of nodes wich will be added later on.

Private Sub treeviewMain_NodeMouseDoubleClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles treeviewMain.NodeMouseDoubleClick

        Dim trv As TreeView = sender
        Dim newform As Form = Nothing

        Select Case trv.SelectedNode.Text
            Case "Return"
                newform = New frmReturn
                Dim newtab As String = trv.SelectedNode.Text
                If Not tabMain.TabPages.ContainsKey(newtab) Then
                    tabMain.TabPages.Add(newtab, newtab)
                    newform.TopLevel = False
                    newform.Parent = tabMain.TabPages(newtab)
                    newform.Show()
                End If
            Case "Borrow"
                newform = New frmBorrow
                Dim newtab As String = trv.SelectedNode.Text
                If Not tabMain.TabPages.ContainsKey(newtab) Then
                    tabMain.TabPages.Add(newtab, newtab)
                    newform.TopLevel = False
                    newform.Parent = tabMain.TabPages(newtab)
                    newform.Show()
                End If
            Case "OPAC"
                newform = New frmOPAC
                Dim newtab As String = trv.SelectedNode.Text
                If Not tabMain.TabPages.ContainsKey(newtab) Then
                    tabMain.TabPages.Add(newtab, newtab)
                    newform.TopLevel = False
                    newform.Parent = tabMain.TabPages(newtab)
                    newform.Show()
                End If
        End Select
    End Sub

The output was correct but my problem is that. The code was too long :) is there any way i can manage to make it short?

THANKS!! #Reverend Jim

Another problem of mine.
I can't use tabMain.SelectedTab = newtab.
I wanted the tab to be selected after double clicked from the treeviewNode.

DONE!!!!!!!!!!!!!!!!!!!!!
THANKS!

I got the problem :D

Awww. How to remove replies?
My problem is still. The codes are too long. :D

I appear to be suffering from a few problems

  1. fumbly fingers
  2. really bad internet connection that keeps dropping out
  3. frisky lap cat

Let me try again with actual working code with no missing lines.

Private Sub TreeView1_DoubleClick(sender As Object, e As System.EventArgs) Handles TreeView1.DoubleClick

    Dim trv As TreeView = sender
    Dim tab As String = trv.SelectedNode.Text
    Dim newform As Form = Nothing

    If trv.SelectedNode Is trv.TopNode Then Exit Sub
    If TabControl1.TabPages.ContainsKey(tab) Then Exit Sub

    TabControl1.TabPages.Add(tab, tab)

    Select Case tab
        Case "Return" : newform = New frmReturn
        Case "Borrow" : newform = New frmBorrow
        Case "OPAC" : newform = New frmOPAC
    End Select

    newform.TopLevel = False
    newform.Dock = DockStyle.Fill
    newform.Show()
    TabControl1.TabPages(tab).Controls.Add(newform)
    TabControl1.SelectedTab = TabControl1.TabPages(tab)

End Sub

This code should have most, if not all the redundancies removed. Please let me know if this doesn't work for you. I don't want to embarrass myself further.

Thank You so much sir!
I'll use the codes for our project, library system.

Hi Sir Reverend Jim,

How to do this:
    If I close the forms the tab will be closed too?

    Thanks. I tried. But I don't think its good. Here are my codes:



Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
        Dim tab As TabPage = frmMain.tabMain.SelectedTab
        frmMain.tabMain.TabPages.Remove(tab)
    End Sub


    My problem is that closing the forms using the close button. Doesn't close/remove the tab.


    THANKS!!!

Set the FormBorderStyle to none and the form in the tab page will not have any close/max/min gadgets. If you want the user to be able to remove a tab then you'll have to add a main menu, hotkey or context menu to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.