我定义的UserControl导致VS2019崩溃

问题描述 投票:0回答:1

我正在尝试使用下面的代码创建一个用户控件,每次我将它添加到我的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);
        }
    }
c# winforms
1个回答
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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.