因于对系统自带的ProgressBar外观不满意,自己写了一个ProgressBar,并把它封进了ToolStrip里:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace Net.Ctrl
{
[ToolStripItemDesignerAvailability(
ToolStripItemDesignerAvailability.ContextMenuStrip |
ToolStripItemDesignerAvailability.MenuStrip |
ToolStripItemDesignerAvailability.StatusStrip |
ToolStripItemDesignerAvailability.ToolStrip)]
public partial class ToolStripProgBar : ToolStripControlHost
{
public ToolStripProgBar()
: base(new Net.Ctrl.ProgressBar())
{
this.AutoSize = false;//开始时由于没有加这句代码,导致了运行中出现异常
}
public Net.Ctrl.ProgressBar _ProgBar //申明一个属性来获取该控件
{
get
{
return Control as Net.Ctrl.ProgressBar;
}
}
}
}如果把我自己写的ProgressBar放进panel中,运行正常。但添加到toolStrip后,却一运行就报异常。经查是用来包装ProgressBar的容器其autoSize属性默认true,把我的控件宽度给auto成0了,也不知道winform是咋想的。我设置的宽度值被无视,以至绘图中暴出异常。把autoSize设为false后,一切就正常了。
本文介绍了一个自定义的进度条控件实现过程,该控件被封装进ToolStrip中使用。作者发现将自定义ProgressBar添加到ToolStrip时会出现异常,原因是ToolStripControlHost的AutoSize属性默认为True,导致自定义控件宽度被自动调整为0。通过将AutoSize设置为False解决了此问题。
5490

被折叠的 条评论
为什么被折叠?



