Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I m trying to fire click event of two dynamic tabcontrols. i am not able to fire click event in the first asign tabcontrol. (without array)


VB
Public Class Form1
    Friend WithEvents MaxTab As TabControl

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ''dataentry
        MaxTab = New TabControl
        MaxTab.Name = "tabd"
        MaxTab.Top = 1
        MaxTab.Size = New System.Drawing.Size(Me.Width - 15, Me.Height - 80)
        MaxTab.TabPages.Add("dTAB1")
        MaxTab.TabPages.Add("dTAB2")

        AddHandler MaxTab.Click, AddressOf MaxTab_Click
        Me.Controls.Add(MaxTab)

        ''Report
        MaxTab = New TabControl
        MaxTab.Name = "tabr"
        MaxTab.Top = 1
        MaxTab.Size = New System.Drawing.Size(Me.Width - 15, Me.Height - 80)
        MaxTab.TabPages.Add("rTAB1")
        MaxTab.TabPages.Add("rTAB2")
        AddHandler MaxTab.Click, AddressOf MaxTab_Click
        Me.Controls.Add(MaxTab)

        Me.Controls("tabr").Visible = False
        Me.MaxTab.Refresh()
    End Sub

    Private Sub MaxTab_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaxTab.Click
        MessageBox.Show(MaxTab.SelectedTabPage.Text)
    End Sub
End Class
Posted
Updated 20-Aug-11 2:12am
v2

1 solution

You need to handle the MouseDown event for the TabControl, and check to see which tab contained the the mouse cursor. This is done by checking the bounding rectangle of each tab (the corresponding tab, not the TabPage itself). Here's some modified code that should help. When you press a L or R mouse button over a tab, a MsgBox will give you the proper TabPage.

VB
Public Class Form1

  Friend WithEvents MaxTab As TabControl

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ''dataentry
    MaxTab = New TabControl
    MaxTab.Name = "tabd"
    MaxTab.Top = 1
    MaxTab.Size = New System.Drawing.Size(Me.Width - 15, Me.Height - 80)
    MaxTab.TabPages.Add("dTAB1")
    MaxTab.TabPages.Add("dTAB2")
    AddHandler MaxTab.MouseDown, AddressOf tabs_MouseDown

    Me.Controls.Add(MaxTab)

    ''Report
    MaxTab = New TabControl
    MaxTab.Name = "tabr"
    MaxTab.Top = 1
    MaxTab.Size = New System.Drawing.Size(Me.Width - 15, Me.Height - 80)
    MaxTab.TabPages.Add("rTAB1")
    MaxTab.TabPages.Add("rTAB2")
    Me.Controls.Add(MaxTab)

    Me.Controls("tabr").Visible = False
    Me.MaxTab.Refresh()
  End Sub

'here's where you get the tab page...
  Private Sub tabs_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MaxTab.MouseDown

    Dim r As Rectangle
    Dim ll As Integer

    For ll = 0 To MaxTab.TabPages.Count - 1
      r = MaxTab.GetTabRect(ll)
      If e.X >= r.Left And e.X <= r.Right And e.Y >= r.Top And e.Y <= r.Bottom Then
        MsgBox(MaxTab.TabPages(ll).Text)
        Exit For
      End If
    Next
  End Sub

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900