c# - UserControl - 将值设置为属性

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

我正在尝试设计我的定制 UserControl对于 WinForms应用程序。我曾经创建过完美的自定义枚举属性并创建了一个 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<bool> 时和编辑器默认 Boolean Collection Editor .

然后CheckBox没有出现,甚至放在set属性中的debbuger断点也不止于此......

问题出在哪里?

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

编辑

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 上的面板。

最佳答案

控件仅在设置了 Parent 时出现。您应该将 _tempCheck 的父级设置为此。

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

关于c# - UserControl - 将值设置为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32044654/

相关文章:

c# - 如何将 Silverlight OOB 附加到 Winforms 面板?

c# - 将 ToListAsync() 与 Automapper ProjectToQueryable() 结合使用

C# 运算符 ?并抛出异常

c# - 条件资源编译

winforms - 如何在 C++ winforms 中将水印文本添加到我的文本框(例如 "type here...")? (VS2012)

c# 设置 2 种颜色的树节点文本

ruby-on-rails - 代表用户进行 Stripe 付款

C# 序列化不会向 xml 文件中的属性添加值

asp.net - 为什么我的 Webform 上的复选框对控制循环代码不可见?

c# - 带有浏览按钮作为用户控件的 WPF 路径文本框