Arokh Posted February 8, 2009 Posted February 8, 2009 Hi I'm having a bit of trouble with removing a tab from a tabcontrol. The user can dynamically add tab, so obviously a way to close them is needed. First I tried to somehow a button to the tablabel but that has proven to be a little difficult. Now I just want to close a tab when the user makes a doubleclick on the tablabel. But the doubleclick event doesn't tell me which tab was clicked, it doesn't even tell me the mouse position. The clicked event passes the mouse position but I still don't know which tab was clicked. Any idea how I can realize this? Quote
Leaders snarfblam Posted February 9, 2009 Leaders Posted February 9, 2009 (edited) I found a mediocre solution on CodeProject. As far as I can see, the biggest problem is that there is no way to obtain the bounding rectangle for each tab unless you enable owner drawing, which means you lose visual styles. [Edit]Looking around Google, there seem to be plenty of free custom-made tab controls that support close buttons, but you still lose the native OS appearance.[/edit] Edited February 9, 2009 by snarfblam Quote [sIGPIC]e[/sIGPIC]
DPrometheus Posted February 10, 2009 Posted February 10, 2009 Hi I'm having a bit of trouble with removing a tab from a tabcontrol. The user can dynamically add tab, so obviously a way to close them is needed. First I tried to somehow a button to the tablabel but that has proven to be a little difficult. Now I just want to close a tab when the user makes a doubleclick on the tablabel. But the doubleclick event doesn't tell me which tab was clicked, it doesn't even tell me the mouse position. The clicked event passes the mouse position but I still don't know which tab was clicked. Any idea how I can realize this? You can add event handlers to your tabpages / tabcontrol dynamically as well First I tried adding event handlers to the tabpages but this will only trigger when you actually click the page instead of the label. So here is another workaround: I made a form with a tabcontrol called tabcontrol1 As you click the tab the selectedTab changes as well. This way you know what tab is clicked, since it gets focus before it gets removed. Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. 'For Each p As TabPage In TabControl1.TabPages ' AddHandler p.MouseDoubleClick, AddressOf TabPage_Close 'Next AddHandler TabControl1.MouseDoubleClick, AddressOf TabPage_Close End Sub Private Sub TabPage_Close(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) TabControl1.TabPages.Remove(CType(sender, TabControl).SelectedTab) End Sub ~ DP Quote My Development System Intel Core i7 920 @2.66Ghz 6 GB DDR3 SDRAM Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot GeForce GTX295 1.8 GB 3.5 TB HD
Arokh Posted February 10, 2009 Author Posted February 10, 2009 Oh, something so simple. Thanks DPrometheus. You've just saved me alot of trouble. :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.