OwnerDrawn TabControl behaving differently in Design Mode [modified] [solved]
-
Hi All. I have been developing my own OwnerDrawn TabControl so I can have a close button on the right hand side of the tab. All was going well thanks to some of the articles on CP and now it has stopped working in Design mode. When the control is used in the test form when the app is running, everything works as expected. In design mode I cannot switch tabs to add controls to them. Any ideas?
public class ClosableTabCtrl : TabControl { #region Private Members private System.Windows.Forms.ImageList buttonImageList; private bool showCloseButton = true; private bool hideCloseButtonWhenOnlyOneTab; private const string tabText = " "; private int buttonImageIndex; #endregion public ClosableTabCtrl() : base() { buttonImageList = new ImageList(); buttonImageList.Images.Add((Image)new Bitmap(Resources.InactiveCross)); buttonImageList.Images.Add((Image)new Bitmap(Resources.ActiveCross)); buttonImageList.Images.Add((Image)new Bitmap(Resources.ClickedCross)); this.DrawMode = TabDrawMode.OwnerDrawFixed; this.DrawItem += new DrawItemEventHandler(ClosableTabCtrl\_DrawItem); this.MouseMove += new MouseEventHandler(ClosableTabCtrl\_MouseMove); this.MouseLeave += new EventHandler(ClosableTabCtrl\_MouseLeave); this.MouseClick += new MouseEventHandler(ClosableTabCtrl\_MouseClick); this.MouseDown += new MouseEventHandler(ClosableTabCtrl\_MouseDown); this.MouseUp += new MouseEventHandler(ClosableTabCtrl\_MouseUp); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); } void ClosableTabCtrl\_MouseUp(object sender, MouseEventArgs e) { if (MouseIsOverButton(e) && buttonImageIndex != 1) { buttonImageIndex = 1; this.Invalidate(); } } void ClosableTabCtrl\_MouseDown(object sender, MouseEventArgs e) { if (MouseIsOverButton(e) && buttonImageIndex != 2) { buttonImageIndex = 2; this.Invalidate(); } } void ClosableTabCtrl\_MouseClick(object sender, MouseEventArgs e) { if (MouseIsOverButton(e)) { // TODO: raise the event }
-
Hi All. I have been developing my own OwnerDrawn TabControl so I can have a close button on the right hand side of the tab. All was going well thanks to some of the articles on CP and now it has stopped working in Design mode. When the control is used in the test form when the app is running, everything works as expected. In design mode I cannot switch tabs to add controls to them. Any ideas?
public class ClosableTabCtrl : TabControl { #region Private Members private System.Windows.Forms.ImageList buttonImageList; private bool showCloseButton = true; private bool hideCloseButtonWhenOnlyOneTab; private const string tabText = " "; private int buttonImageIndex; #endregion public ClosableTabCtrl() : base() { buttonImageList = new ImageList(); buttonImageList.Images.Add((Image)new Bitmap(Resources.InactiveCross)); buttonImageList.Images.Add((Image)new Bitmap(Resources.ActiveCross)); buttonImageList.Images.Add((Image)new Bitmap(Resources.ClickedCross)); this.DrawMode = TabDrawMode.OwnerDrawFixed; this.DrawItem += new DrawItemEventHandler(ClosableTabCtrl\_DrawItem); this.MouseMove += new MouseEventHandler(ClosableTabCtrl\_MouseMove); this.MouseLeave += new EventHandler(ClosableTabCtrl\_MouseLeave); this.MouseClick += new MouseEventHandler(ClosableTabCtrl\_MouseClick); this.MouseDown += new MouseEventHandler(ClosableTabCtrl\_MouseDown); this.MouseUp += new MouseEventHandler(ClosableTabCtrl\_MouseUp); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); } void ClosableTabCtrl\_MouseUp(object sender, MouseEventArgs e) { if (MouseIsOverButton(e) && buttonImageIndex != 1) { buttonImageIndex = 1; this.Invalidate(); } } void ClosableTabCtrl\_MouseDown(object sender, MouseEventArgs e) { if (MouseIsOverButton(e) && buttonImageIndex != 2) { buttonImageIndex = 2; this.Invalidate(); } } void ClosableTabCtrl\_MouseClick(object sender, MouseEventArgs e) { if (MouseIsOverButton(e)) { // TODO: raise the event }
I did something similar some time ago (although I don't think I actually inherited from tabControl), and implementing the design time functionality was a little problematic. You can try to have a look at the souce to my Scroll Selector here: ScrollSelector[^] That might give you some pointers... Good luck
-
I did something similar some time ago (although I don't think I actually inherited from tabControl), and implementing the design time functionality was a little problematic. You can try to have a look at the souce to my Scroll Selector here: ScrollSelector[^] That might give you some pointers... Good luck
Thanks Johnny J I've had a look through and to be honest I'm not sure how it all works together. I'm having trouble getting it to work in 2008. I'm going to try and build it again from scratch by pasting in the code bit by bit and testing along the way. I was able to select the different tabs in design mode before lunch. I wonder what has happened. If I find out I will post it here. Cheers
The FoZ
-
Hi All. I have been developing my own OwnerDrawn TabControl so I can have a close button on the right hand side of the tab. All was going well thanks to some of the articles on CP and now it has stopped working in Design mode. When the control is used in the test form when the app is running, everything works as expected. In design mode I cannot switch tabs to add controls to them. Any ideas?
public class ClosableTabCtrl : TabControl { #region Private Members private System.Windows.Forms.ImageList buttonImageList; private bool showCloseButton = true; private bool hideCloseButtonWhenOnlyOneTab; private const string tabText = " "; private int buttonImageIndex; #endregion public ClosableTabCtrl() : base() { buttonImageList = new ImageList(); buttonImageList.Images.Add((Image)new Bitmap(Resources.InactiveCross)); buttonImageList.Images.Add((Image)new Bitmap(Resources.ActiveCross)); buttonImageList.Images.Add((Image)new Bitmap(Resources.ClickedCross)); this.DrawMode = TabDrawMode.OwnerDrawFixed; this.DrawItem += new DrawItemEventHandler(ClosableTabCtrl\_DrawItem); this.MouseMove += new MouseEventHandler(ClosableTabCtrl\_MouseMove); this.MouseLeave += new EventHandler(ClosableTabCtrl\_MouseLeave); this.MouseClick += new MouseEventHandler(ClosableTabCtrl\_MouseClick); this.MouseDown += new MouseEventHandler(ClosableTabCtrl\_MouseDown); this.MouseUp += new MouseEventHandler(ClosableTabCtrl\_MouseUp); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); } void ClosableTabCtrl\_MouseUp(object sender, MouseEventArgs e) { if (MouseIsOverButton(e) && buttonImageIndex != 1) { buttonImageIndex = 1; this.Invalidate(); } } void ClosableTabCtrl\_MouseDown(object sender, MouseEventArgs e) { if (MouseIsOverButton(e) && buttonImageIndex != 2) { buttonImageIndex = 2; this.Invalidate(); } } void ClosableTabCtrl\_MouseClick(object sender, MouseEventArgs e) { if (MouseIsOverButton(e)) { // TODO: raise the event }
I rebuilt it from scratch and here is what I found. In the DrawItem method, the design mode does not like it when I change the text of the tab. The line I found to cause the trouble is
this.TabPages[e.Index].Text = this.TabPages[e.Index].Text.Replace(tabText, string.Empty);
Looks like I am going to have to actively calculate the size of the tab which is probably the best way to go about it. If anyone can shed some light on the reason for this so I know why I shouldn't do it my original way, I would be grateful. Cheers
The FoZ