views:

332

answers:

3

I have a tabcontrol used to display multiple image files in an application. I would like to remove the tabpage title when there is only one tabpage displayed, so I can use that screen space for the image. (This is similar to deselecting "Always show the tab bar" in Firefox.)

Is this possible to do with the tabcontrol? Or am I better off using a panel control when only one file (tab) is open?

A: 

I do not recall any means to hide the tab label. My recommendation:

Have your tab contents in panels. When only one tab, move the panel put to replace the tabcontrol or something of that nature.

o.k.w
+6  A: 

Yes, this is possible. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

using System;
using System.Windows.Forms;

public class MyTabControl : TabControl {
  private int mPages = 0;
  private void checkOnePage() {
    if (IsHandleCreated) {
      int pages = mPages;
      mPages = this.TabCount;
      if ((pages == 1 && mPages > 1) || (pages > 1 && mPages == 1))
        this.RecreateHandle();
    }
  }
  protected override void OnControlAdded(ControlEventArgs e) {
    base.OnControlAdded(e);
    checkOnePage();
  }
  protected override void OnControlRemoved(ControlEventArgs e) {
    base.OnControlRemoved(e);
    checkOnePage();
  }
  protected override void WndProc(ref Message m) {
    // Hide tabs by trapping the TCM_ADJUSTRECT message
    if (m.Msg == 0x1328 && !DesignMode && this.TabCount == 1) m.Result = (IntPtr)1;
    else base.WndProc(ref m);
  }
}
Hans Passant
It works! (Even in vb.)
xpda
Good one there! +1
o.k.w
This seems to work fine without calling checkOnePage. Is there a reason I should keep that?
xpda
Yes. That makes sure that it works when you add or remove tab pages at runtime.
Hans Passant
I thought that is what it is for, but it works at runtime properly without it. Maybe the handle is recreated every time a tab page is added or removed now. (vb.net 2008)
xpda
You're right. It appears TCM_ADJUSTRECT is sent every time the control is painted. Adding/removing a page will cause a paint event.
Hans Passant
A: 

try using the answer given here :) .. setting the region of the tab

http://stackoverflow.com/questions/25158/building-c-net-windows-application-with-multiple-views

Amitd