【问题标题】:UserControl - set value to propertyUserControl - 将值设置为属性
【发布时间】:2015-08-17 07:13:56
【问题描述】:

我正在尝试为WinForms 应用程序设计我的自定义UserControl。当用户在设计时更改属性值时,我曾经创建完美的自定义枚举属性并创建一个CheckBox

    private SearchOptionsEnum _searchAreas;
    //private List<bool> _searchAreas = new List<bool>();
    [Description(""), Category("GostcompSettings")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(Utils.FlagEnumUIEditor), typeof(UITypeEditor))]
    public SearchOptionsEnum SearchAreas
    //public List<bool> SearchAreas
    {
        get
        {
            return _searchAreas;
        }
        set
        {

            _searchAreasChceckBoxList.Clear();
            pPanelWithCheckboxies.Controls.Clear();
            int x = 10;
            int y = 10;

            CheckBox _tempCheck = new CheckBox();
            _tempCheck.Checked = true;
            _tempCheck.Location = new Point(x, y);

            _searchAreasChceckBoxList.Add(_tempCheck);
            pPanelWithCheckboxies.Controls.Add(_tempCheck);

            MessageBox.Show("zmiana");
            _searchAreas = value;
        }
    }

我在代码中使用自定义值编辑器UITypeEditor,它工作正常。

我在设计时得到 MessageBox 并出现 CheckBox。问题是当我将 SearchOptionsEnum 更改为 List&lt;bool&gt; 并将编辑器更改为默认 Boolean Collection Editor 时。

然后CheckBox就没有出现了,甚至debbuger断点放在set属性里也不会停在那里……

问题出在哪里?

此外:当我在编辑器中编辑布尔值时,它会记住它并保留值。即使在下一个调试会话中,之前设置的值也会保留。

编辑

public partial class StudySearchAndView : UserControl
{
    private List<CheckBox> _searchAreasChceckBoxList = new List<CheckBox>();

    private SearchOptionsEnum _searchAreas;
    //private List<bool> _searchAreas = new List<bool>();
    [Description(""), Category("GostcompSettings")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(Utils.FlagEnumUIEditor), typeof(UITypeEditor))]
    public SearchOptionsEnum SearchAreas
    //public List<bool> SearchAreas
    {
        get
        {
            return _searchAreas;
        }
        set
        {

            _searchAreasChceckBoxList.Clear();
            pPanelWithCheckboxies.Controls.Clear();
            int x = 10;
            int y = 10;

            CheckBox _tempCheck = new CheckBox();
            _tempCheck.Checked = true;
            _tempCheck.Location = new Point(x, y);

            _searchAreasChceckBoxList.Add(_tempCheck);
            pPanelWithCheckboxies.Controls.Add(_tempCheck);

            MessageBox.Show("zmiana");
            _searchAreas = value;
        }
    }

}

pPanelWithCheckboxies 只是放在 UserControl 上的一个面板。

【问题讨论】:

  • 能否展示更多代码以便更好地理解?
  • 稍后我会尝试 UITypeEditor 的 BooleanCollectionEditor,可能会有一些错误(我猜)。但是,如果您在设计模式的属性页中编辑某些值,则过去通过将这些值写入InitializeComponent() 方法来记忆。还是我误解了你的最后一段?
  • @J.C 是的,就像你说的那样
  • @XaweryWiśniowiecki 如果是这样,这些值将保留用于不同的会话,就像您更改标准控件的属性(例如标签、文本框)一样
  • 这里不是这样

标签: c# winforms user-controls remote-debugging design-time


【解决方案1】:

只有在设置了 Parent 时才会出现控件。您应该将 _tempCheck 的 Parent 设置为此。

        CheckBox _tempCheck = new CheckBox();
        _tempCheck.Parent = this;
        _tempCheck.Checked = true;
        _tempCheck.Location = new Point(x, y);

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多