How do I use buttons outside TabControl in


sorry if the title of my question was wrong.

I am using Visual Studio 2019

My problem is using a button that is not inside TabControl and like umm... The button outside the TabControl should work on selected tab only. But it doesn't, I tried doing like this:


There was no error at all, but when I debug it and press the button, it shows me the code and visual studio shows me something message. Anyways, I don't remember it.

Please help me, advance thanks to the helpers.


Hello, people who don't understand my question correctly, I am going to show more information.

  1. The error is when I press the button, it takes me to visual studio 2019 and then shows the code and shows some error called "Exception Unhandled" and I copied the error details:

System.Runtime.InteropServices.COMException HResult=0x80004005
Message=Error HRESULT E_FAIL has been returned from a call to a COM component. Source=Interop.SHDocVw StackTrace: at SHDocVw.IWebBrowser2.GoBack() at AxSHDocVw.AxWebBrowser.GoBack()
at Browser.Browser.Button1_Click(Object sender, EventArgs e) in C:\Users\Admin\source\repos\Browser\Browser\Browser.vb:line 20 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at Browser.My.MyApplication.Main(String[] Args) in :line 81

  1. Someone answered my question, but that was not the answer I expected. That he gave me the code that runs on each every tab that I should retype again and again, the code I expected is that button code works on every tab. If my question was still not understandable, then here's the whole code:

Public Class Browser

Private Sub Button6_Click(sender As Object, e As EventArgs)
End Sub

Private Sub NewTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewTabToolStripMenuItem.Click
    Dim t As New TabPage
    Dim tab As New Form1
    tab.Dock = DockStyle.Fill
    tab.TopLevel = False
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End Sub

Private Sub CloseTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseTabToolStripMenuItem.Click
End Sub

Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
    End If
End Sub

Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    ContextMenuStrip1.Show(Button6, 0, y:=Button6.Height)
End Sub
End Class
asked on Stack Overflow May 11, 2020 by BHARATHI Tutorials • edited May 12, 2020 by BHARATHI Tutorials

1 Answer


From the official documentation of Microsoft, I guess you wanted similar to .SelectedTab = TabNum, an example's shown below:

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If TabControl1.SelectedTab Is TabPage1 ' choose your required statement
        SpecificActivityForPage1() ' your code

    Else If TabControl1.SelectedTab Is TabPage2
        SpecificActivityForPage2() ' your code
        ' something else...
    End If
End Sub

This should work for you. If it doesn't, please specify your error message thrown by the IDE.

answered on Stack Overflow May 11, 2020 by Rohan Bari • edited May 12, 2020 by Rohan Bari

User contributions licensed under CC BY-SA 3.0