Click here to Skip to main content
15,895,740 members
Home / Discussions / C#
   

C#

 
QuestionURGENT HELP PLZ: many erros!!! Pin
Jassim Rahma16-Jun-10 4:06
Jassim Rahma16-Jun-10 4:06 
AnswerRe: URGENT HELP PLZ: many erros!!! Pin
harold aptroot16-Jun-10 4:13
harold aptroot16-Jun-10 4:13 
AnswerRe: URGENT HELP PLZ: many erros!!! Pin
Luc Pattyn16-Jun-10 4:16
sitebuilderLuc Pattyn16-Jun-10 4:16 
AnswerRe: URGENT HELP PLZ: many erros!!! Pin
Abhinav S16-Jun-10 4:20
Abhinav S16-Jun-10 4:20 
QuestionDirectshow problem Pin
TimSWatson16-Jun-10 3:32
TimSWatson16-Jun-10 3:32 
AnswerRe: Directshow problem Pin
TheyCallMeMrJames16-Jun-10 4:37
TheyCallMeMrJames16-Jun-10 4:37 
GeneralRe: Directshow problem Pin
Eduard Keilholz16-Jun-10 4:51
Eduard Keilholz16-Jun-10 4:51 
QuestionOwnerDrawn TabControl behaving differently in Design Mode [modified] [solved] Pin
TheFoZ16-Jun-10 3:11
TheFoZ16-Jun-10 3:11 
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
        }

    }

    void ClosableTabCtrl_MouseLeave(object sender, EventArgs e)
    {
        if (buttonImageIndex != 0)
        {
            buttonImageIndex = 0;
            this.Invalidate();
        }
    }

    void ClosableTabCtrl_MouseMove(object sender, MouseEventArgs e)
    {
        if (MouseIsOverButton(e))
        {
            if (buttonImageIndex != 1)
            {
                buttonImageIndex = 1;
                this.Invalidate();
            }
        }
        else
        {
            if (buttonImageIndex != 0)
            {
                buttonImageIndex = 0;
                this.Invalidate();
            }
        }
    }

    public bool ShowCloseButton
    {
        get { return showCloseButton; }
        set { showCloseButton = value; }
    }

    public bool HideCloseButtonWhenOnlyOneTab
    {
        get { return hideCloseButtonWhenOnlyOneTab; }
        set { hideCloseButtonWhenOnlyOneTab = value; }
    }

    bool MouseIsOverButton(MouseEventArgs e)
    {
        Rectangle r = this.GetTabRect(this.SelectedIndex);
        r.Offset(2, 2);
        Rectangle b = new Rectangle(new Point(r.X + (this.GetTabRect(this.SelectedIndex).Width - 20), this.GetTabRect(this.SelectedIndex).Height / 2 - 7),
                                    new Size(17, 17));

        if (b.Contains(e.Location))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    void ClosableTabCtrl_DrawItem(object sender, DrawItemEventArgs e)
    {
        Rectangle r = this.GetTabRect(e.Index);
        string title = String.Empty;

        r.Offset(2, 2);
        Brush TitleBrush = new SolidBrush(Color.Black);
        Font f = this.Font;

        if (e.State == DrawItemState.Selected)
        {
            if (!this.TabPages[e.Index].Text.EndsWith(tabText))
            {
                this.TabPages[e.Index].Text += tabText;
            }
            title = this.TabPages[e.Index].Text + tabText;


            if (showCloseButton)
            {
                if (!(this.hideCloseButtonWhenOnlyOneTab && this.TabPages.Count == 1))
                {
                    e.Graphics.DrawImage(buttonImageList.Images[buttonImageIndex],
                                         new Point(r.X + (this.GetTabRect(e.Index).Width - 20),
                                             this.GetTabRect(e.Index).Height / 2 - 7));
                }
            }
        }
        else
        {
            if (this.TabPages[e.Index].Text.EndsWith(tabText))
            {
                this.TabPages[e.Index].Text = this.TabPages[e.Index].Text.Replace(tabText, string.Empty);
            }

            title = this.TabPages[e.Index].Text + tabText;
        }
        // center the text in the tab
        e.Graphics.DrawString(title, f, TitleBrush, new PointF(r.X, (r.Height - f.Height) / 2 + 1));

        TitleBrush.Dispose();
    }
}


Thanks in advance.
The FoZ
modified on Wednesday, June 16, 2010 11:46 AM

AnswerRe: OwnerDrawn TabControl behaving differently in Design Mode Pin
Johnny J.16-Jun-10 3:49
professionalJohnny J.16-Jun-10 3:49 
GeneralRe: OwnerDrawn TabControl behaving differently in Design Mode Pin
TheFoZ16-Jun-10 4:32
TheFoZ16-Jun-10 4:32 
AnswerRe: OwnerDrawn TabControl behaving differently in Design Mode - solution Pin
TheFoZ16-Jun-10 5:51
TheFoZ16-Jun-10 5:51 
Questioncombobox inside datagridview Pin
Rajee Maharjan16-Jun-10 1:40
Rajee Maharjan16-Jun-10 1:40 
QuestionSerializing a Collection - Inheritance problem Pin
lukeer16-Jun-10 1:39
lukeer16-Jun-10 1:39 
AnswerRe: Serializing a Collection - Inheritance problem Pin
riced16-Jun-10 3:05
riced16-Jun-10 3:05 
AnswerRe: Serializing a Collection - Inheritance problem Pin
Pete O'Hanlon16-Jun-10 3:07
mvePete O'Hanlon16-Jun-10 3:07 
GeneralRe: Serializing a Collection - Inheritance problem Pin
lukeer16-Jun-10 4:09
lukeer16-Jun-10 4:09 
AnswerRe: Serializing a Collection - Inheritance problem Pin
Rob Graham19-Jun-10 3:21
Rob Graham19-Jun-10 3:21 
QuestionPrinting Quality Image Vs Text Pin
Anubhava Dimri16-Jun-10 0:05
Anubhava Dimri16-Jun-10 0:05 
AnswerRe: Printing Quality Image Vs Text Pin
Nuri Ismail16-Jun-10 0:26
Nuri Ismail16-Jun-10 0:26 
GeneralRe: Printing Quality Image Vs Text Pin
Anubhava Dimri17-Jun-10 20:09
Anubhava Dimri17-Jun-10 20:09 
AnswerRe: Printing Quality Image Vs Text Pin
Luc Pattyn16-Jun-10 2:25
sitebuilderLuc Pattyn16-Jun-10 2:25 
QuestionWhich is better and why? Pin
Hum Dum15-Jun-10 22:53
Hum Dum15-Jun-10 22:53 
AnswerRe: Which is better and why? Pin
Bigdeak15-Jun-10 23:22
Bigdeak15-Jun-10 23:22 
GeneralRe: Which is better and why? Pin
Hum Dum16-Jun-10 0:31
Hum Dum16-Jun-10 0:31 
AnswerRe: Which is better and why? Pin
Abhinav S16-Jun-10 0:36
Abhinav S16-Jun-10 0:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.