I am working with tabs in a application but I am having problem with the tabs. I want to allow the user to add or delete a tab.

Can any one help

Thanks in advance

Recommended Answers

All 8 Replies

Welcome Fahim.

Try this code:
Drop a TabControl and two buttons namely Button1 and Button2.

'To add a new tab
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As New TabPage
        p.Text = "New"
        TabControl1.TabPages.Add(p)
    End Sub
    ' To delete a selected tab
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
    End Sub

I was just going to show the same example :)

Anyway. Here's a small fix for removing tab page

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  If TabControl1.SelectedIndex >= 0 Then
    TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
  End If

End Sub

You should always check the value of <control>.SelectedIndex before using it. If the user hasn't selected anything, it will have a negative value (-1 usually) and your code will crash. This applies to list view control, tab control etc.

Thank. I missed that one.

Thanks for the code, I also want to allow the user to change the name of the tabs and if you don't mind can you help me.

Thanks

Welcome Fahim.

Try this code:
Drop a TabControl and two buttons namely Button1 and Button2.

'To add a new tab
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As New TabPage
        p.Text = "New"
        TabControl1.TabPages.Add(p)
    End Sub
    ' To delete a selected tab
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
    End Sub

AFAIK you can't use "in-place" renaming like you can do with, for example, tree view control. I used tab control's KeyDown event

Private Sub TabControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
  ' Rename the current tab page
  Dim NewName As String

  ' Check for F2
  If e.KeyCode = Keys.F2 Then
    ' Is a tab page selected
    If TabControl1.SelectedIndex >= 0 Then
      ' Prompt for the tab page name
      NewName = InputBox("Enter tab page's name", "Rename", "")
      ' If user enters an empty string, no renaming
      If Not String.IsNullOrEmpty(NewName) Then
        ' Get selected page for renaming
        TabControl1.TabPages.Item(TabControl1.SelectedIndex).Text = NewName
      End If
    End If
  End If

End Sub

The code uses InputBox for user input which is not an elegant solution, and you may want to use some other method for user input. But this solution works :)

I am using a text box for input. Is the good or not


AFAIK you can't use "in-place" renaming like you can do with, for example, tree view control. I used tab control's KeyDown event

Private Sub TabControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
  ' Rename the current tab page
  Dim NewName As String

  ' Check for F2
  If e.KeyCode = Keys.F2 Then
    ' Is a tab page selected
    If TabControl1.SelectedIndex >= 0 Then
      ' Prompt for the tab page name
      NewName = InputBox("Enter tab page's name", "Rename", "")
      ' If user enters an empty string, no renaming
      If Not String.IsNullOrEmpty(NewName) Then
        ' Get selected page for renaming
        TabControl1.TabPages.Item(TabControl1.SelectedIndex).Text = NewName
      End If
    End If
  End If

End Sub

The code uses InputBox for user input which is not an elegant solution, and you may want to use some other method for user input. But this solution works :)

Yes you can. If you solved your problem then mark this thread as "Solved".

Thanks

You can use text box (or whatever) like adatapost already said. Since tab page control doesn't have "in-place" renaming, the solution will be IMHO more or less clumsy ;) anyway. And please mark this thread as solved, thanks.

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.