我定义的UserControl导致VS2019崩溃

时间:2019-04-14 09:59:56

标签: c# winforms

我试图用下面的代码创建一个用户控件,并且每次将其添加到我的Main表单时,VS都会崩溃而没有任何跟踪或异常

当我尝试插入时问题开始了

[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Category("u4sSearchBox")]

但是我删除了它,问题仍然存在

我尝试删除bin,obj和.vs文件夹,但是没有用 我已经尝试过使用VS2017,但是没有用

MethodHolderBasic.cs

    public partial class MethodHolderBasic : UserControl
    {
        public Bitmap ExpandedImgage;
        public Bitmap CollaspedImage;
        public bool Expanded
        {
            get
            {
                return Expanded;
            }
            set
            {
                Expanded = value;
                if (Expanded)
                    pbExpand.Image = new Bitmap(ExpandedImgage, 22, 22);
                else
                    pbExpand.Image = new Bitmap(CollaspedImage, 22, 22);
            }
        }
        public MethodHolderBasic()
        {
            InitializeComponent();
            pbDelete.Image = new Bitmap(pbDelete.Image, 22, 22);
            pbExpand.Image = new Bitmap(pbExpand.Image, 22, 22);
        }
    }

1 个答案:

答案 0 :(得分:0)

您正在使用Expanded属性作为Expanded的getter中的返回值,并将其设置在setter中。这会导致堆栈溢出问题,因此您的程序将崩溃。

另一个问题(也许不是):从代码中看,您似乎没有设置Expanded Image和CollaspedImage,这将导致ArgumentNullException异常。

此代码对我有用:

public partial class MethodHolderBasic : UserControl
{
    public Bitmap ExpandedImgage;
    public Bitmap CollaspedImage;

    bool _expanded = false;
    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [Category("u4sSearchBox")]
    public bool Expanded
    {
        get
        {
            return _expanded;
        }
        set
        {
            _expanded = value;
            if (_expanded)
                pbExpand.Image = new Bitmap(ExpandedImgage, 22, 22);
            else
                pbExpand.Image = new Bitmap(CollaspedImage, 22, 22);
        }
    }

    public MethodHolderBasic()
    {
        InitializeComponent();

        pbDelete.Image = new Bitmap(pbDelete.Image, 22, 22);
        pbExpand.Image = new Bitmap(pbExpand.Image, 22, 22);

        // Initialize ExpandedImgage and CollaspedImage
        ExpandedImgage = new Bitmap(pbDelete.Image, 22, 22);
        CollaspedImage = new Bitmap(pbExpand.Image, 22, 22);
    }
}