Re: Windows Forms Tab Control - Identifying Which Tab

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 21/05/2006 Kevin Spencer wrote:

A few (helpful, I hope) comments on Micks (excellent) sample:

First, Jeff, your problem was due to the fact that the tabs themselves (not the TabPages) are not members of the TabPages, but of the TabControl. So, when you right-click on a tab, the corresponding TabPage is not clicked.

Yes, got there by trial and (mostly) error :-(

I did use the help, which is better than average, and I read it as saying that if you click on a tab then the tab control will raise a click event for the tab page, if that were true it would have been a great help!

This sample uses the MouseMove event of the TabControl. This means that every time the Mouse changes position over the TabControl, the function is called. If all you need is to identify the TabPage when the mouse is right-clicked, you should be about to handle the MouseClick event, identify which button was pressed, and if it is the right button, activate the Context Menu for the appropriate tab using Mick's code.

I'll try that once my joy at getting it working has subdued :-)


A further optimization could be used as well. Assuming that the Tabs in the TabControl maintain a constant width, that is, that the text contents of them do not change, and assuming that no TabPages are removed or added, you could create an array of rectangles when the TabControl loads, calculating the client rectangle of each Tab, so that this would only need to be calculated one time, and then use that array of rectangles to perform the calculations in Mick's example.

I had started to think about that but hadn't tried it. It's a small app with tabbed browsing, I use Firefox normally but I like to save files in mht format.

It will become redundant once IE7 is mainstream!

Many thanks :-)

--
Jeff Gaines
.


Quantcast