C#用户控件崩溃VS11

本文关键字:VS11 崩溃 控件 用户 | 更新日期: 2023-09-27 18:30:04

我发现自己第一次编写C3代码,并在很长一段时间内第一次使用Visual Studio。

我正在创建一个允许拾取文件/文件夹等的用户控件,以便在未来更容易实现这种控件。然而,每当我将控件拖动到任何窗体时,Visual Studio都会立即崩溃。我已经多次尝试重新构建整个解决方案。错误似乎只在控件中创建公共变量时发生。。。

有人知道如何绕过这个吗?代码正在进行中……;)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BackupReport.tools
{
    public partial class pathchooser : UserControl
    {
        #region "Datatypes"
        public enum DLG { Folder, FileSave, FileOpen };
        #endregion
        #region "public properties"
        public DLG Dtype
        {
            get
            {
                return this.Dtype;
            }
            set
            {
                this.Dtype = value;
            }
        }
        public string labelText
        {
            get
            {
                return this.labelText;
            }
            set
            {
                this.labelText = value;
                label1.Text = this.labelText;
            }
        }
        #endregion
        #region "Constructor"
        public pathchooser()
        {
            InitializeComponent();
            this.Dtype = DLG.Folder;
            this.labelText = "Source:";
            label1.Text = this.labelText;
        }
        #endregion
        private void browse_button_Click(object sender, EventArgs e)
        {
            switch (this.Dtype)
            {
                case DLG.Folder:
                    if (fbd.ShowDialog() == DialogResult.OK)
                    {
                        path_textbox.Text = fbd.SelectedPath;
                    }
                    break;
                case DLG.FileSave:
                    break;
                case DLG.FileOpen:
                    break;
                default:
                    break;
            }
        }
    }
}

如有任何帮助,我们将不胜感激。我也不确定这是否重要,但我使用的是VS11测试版。

//Martin

C#用户控件崩溃VS11

public DLG Dtype
    {
        get
        {
            return this.Dtype;
        }
        set
        {
            this.Dtype = value;
        }
    }

您有一个引用自身的属性,因此在getter和setter内部(分别)调用自己的getter和set。更合适的做法是要么有空的访问者:

public DLG DType{get; set;}

或者让访问者引用私有变量:

private DLG dtype;
public DLG Dtype
    {
        get
        {
            return this.dtype;
        }
        set
        {
            this.dtype = value;
        }
    }

我认为您的属性导致了StackOverflowException,因为getter和setter在一个无休止的循环中调用自己(Dtype->Dtype->D…)。

试试这个代码:

private string labelText;
public DLG Dtype { get; set; }
public string LabelText 
{
  get { return this.labelText; }
  set
  {
    this.labelText = value;
    label1.Text = value;
  }
}