【问题标题】:Visual Studio Designer not happy with my TabControl, anything I can do? (.NET / C#)Visual Studio Designer 对我的 TabControl 不满意,我能做些什么? (.NET / C#)
【发布时间】:2010-07-30 09:42:50
【问题描述】:

我有一个扩展 TabControl 的类,基本上是为了让标签位于左侧而不是顶部。为此,我已将其设置为自定义绘制。

问题是当它通过设计器放到表单上时,它很容易让设计器失去对自己的跟踪,然后放弃并且什么都不显示。要修复它,我必须关闭设计器并重新打开它,然后一切都很好,直到我点击调试,或者我进入代码窗口一段时间然后回来,它再次放弃。

我有什么可以帮助视觉工作室的吗?虽然它没有抛出错误,但它现在变得有点乏味。我正在使用 Visual Studio 2008。

这是扩展选项卡控件的代码,如果有人发现任何可能导致此问题的问题,我们将不胜感激。

public class VerticalTabControl : TabControl
{
    private Color tabColour1 = Color.AliceBlue;

    public Color TabColour1
    {
        get { return tabColour1; }
        set { tabColour1 = value; this.Refresh(); }
    }

    private Color tabColour2 = Color.White;

    public Color TabColour2
    {
        get { return tabColour2; }
        set { tabColour2 = value; this.Refresh(); }
    }

    private Color selectedTabColor1 = Color.AliceBlue;

    public Color SelectedTabColor1
    {
        get { return selectedTabColor1; }
        set { selectedTabColor1 = value; this.Refresh(); }
    }

    private Color selectedTabColor2 = Color.White;

    public Color SelectedTabColor2
    {
        get { return selectedTabColor2; }
        set { selectedTabColor2 = value; this.Refresh(); }
    }

    private Color backgroundColour = Color.White;

    public Color BackgroundColour
    {
        get { return backgroundColour; }
        set { backgroundColour = value; this.Refresh(); }
    }

    private Color tabTextColour = Color.Black;

    public Color TabTextColour
    {
        get { return tabTextColour; }
        set { tabTextColour = value; this.Refresh(); }
    }

    protected override void OnParentChanged(EventArgs e)
    {
        base.OnParentChanged(e);
        this.Parent.Resize += new EventHandler(Parent_Resize);
    }

    void Parent_Resize(object sender, EventArgs e)
    {
        this.Refresh();
    }


    public VerticalTabControl()
        : base()
    {
        this.Alignment = TabAlignment.Left;
        this.SizeMode = TabSizeMode.Fixed;
        this.ItemSize = new Size(50, 120);
        this.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(VerticalTabControl_DrawItem);          
    }

    void VerticalTabControl_DrawItem(object sender, DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        TabControl ctrl = sender as TabControl;
        String sText = ctrl.TabPages[e.Index].Text;

        Rectangle r = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);

        if (e.Index == ctrl.SelectedIndex)
        {
            using (LinearGradientBrush gb = new LinearGradientBrush(r, this.selectedTabColor1, this.selectedTabColor2, LinearGradientMode.Horizontal))
            {
                e.Graphics.FillRectangle(gb, r);
            }
        }
        else
        {
            using (LinearGradientBrush gb = new LinearGradientBrush(r, this.tabColour1, this.tabColour2, LinearGradientMode.Horizontal))
            {
                e.Graphics.FillRectangle(gb, r);
            }
        }

        // Set up the page and the various pieces.
        TabPage page = ctrl.TabPages[e.Index];

        // Set up the offset for an icon, the bounding rectangle and image size and then fill the background.
        int iconOffset = 0;
        Rectangle tabBackgroundRect = e.Bounds;

        // If we have images, process them.
        if (this.ImageList != null)
        {
            // Get sice and image.
            Size size = this.ImageList.ImageSize;
            Image icon = null;
            if (page.ImageIndex > -1)
              icon = this.ImageList.Images[page.ImageIndex];
            else if (page.ImageKey != "")
              icon = this.ImageList.Images[page.ImageKey];

            // If there is an image, use it.
            if (icon != null)
            {
             Point startPoint = new Point(tabBackgroundRect.X + 6,
                   tabBackgroundRect.Y + 2 + ((tabBackgroundRect.Height - size.Height) / 2));
               e.Graphics.DrawImage(icon, new Rectangle(startPoint, size));
                iconOffset = size.Width + 4;
            }
        }

        // Draw out the label.
        SizeF sizeText = g.MeasureString(sText, ctrl.Font);
        int iX = e.Bounds.Left + 6 + iconOffset;
        int iY = e.Bounds.Top + (e.Bounds.Height / 2) - (int)(sizeText.Height / 2);

        using (Brush ForeBrush = new SolidBrush(tabTextColour))
        {
            g.DrawString(sText, ctrl.Font, ForeBrush, iX, iY);
        }

        Rectangle rec = ctrl.GetTabRect(ctrl.TabPages.Count - 1);
        Rectangle recF = new Rectangle(0, rec.Bottom, this.ItemSize.Height, ctrl.Height - rec.Bottom);
        using (SolidBrush bb = new SolidBrush(backgroundColour))
        {
            g.FillRectangle(bb, recF);
        }
    }

}

【问题讨论】:

    标签: c# .net visual-studio winforms visual-studio-2008


    【解决方案1】:

    原来这是有问题的代码

    protected override void OnParentChanged(EventArgs e)
    {
        base.OnParentChanged(e);
        this.Parent.Resize += new EventHandler(Parent_Resize);
    }
    

    'this.Parent' 有时为 null,因此会引发异常,导致设计器失败。

    现在已经解决了这个问题,还整理了事件挂钩,所以我没有到处留下挂钩。现在似乎工作正常。感谢您的帮助,无论出于何种原因回答此问题并删除了他们的答案。

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2015-10-23
      • 2014-02-27
      • 1970-01-01
      • 2010-12-17
      • 2021-07-06
      • 2013-05-08
      • 1970-01-01
      • 2019-09-19
      相关资源
      最近更新 更多