Question

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?

Was it helpful?

Solution

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);
  }
}

OTHER TIPS

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

Building C# .NET windows application with multiple views

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top