如何向组上的表单添加属性,并在属性窗口中显示并可编辑

发布于 2025-01-05 17:38:42 字数 913 浏览 0 评论 0原文

我有这个类:

public class MyProps
{
    public MyProps()
    {

    }

    protected string myVar;
    public string MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }

    protected int myOtherVar;
    public int MyOtherProperty
    {
        get { return myOtherVar; }
        set { myOtherVar = value; }
    }
}

我想将其添加到我的表单中,因此当我继承它时,我将能够填充 MyPropsX 属性中的属性。

我的表单中有以下代码:

    protected  MyProps propsX = new MyProps();

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public  MyProps MyPropsX
    {
        get
        {
            return propsX;
        }
        set
        {
            propsX = value;
        }
    }

现在,属性 MyProperty 和 MyOtherProperty 很好地显示在“属性”窗口中,我可以直接在那里设置它们的值。

但是,当我关闭表单并再次打开它时,所有更改都会丢失,属性被重置为显示零和空字符串。

我缺少什么? 我应该从某些特殊类或接口继承 MyProps 类吗? 或者有什么特殊属性?

I have this class:

public class MyProps
{
    public MyProps()
    {

    }

    protected string myVar;
    public string MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }

    protected int myOtherVar;
    public int MyOtherProperty
    {
        get { return myOtherVar; }
        set { myOtherVar = value; }
    }
}

That I want to add to my Form, so when I inherit from it I will be able to fill the properties in the MyPropsX property.

I have this code in my form:

    protected  MyProps propsX = new MyProps();

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public  MyProps MyPropsX
    {
        get
        {
            return propsX;
        }
        set
        {
            propsX = value;
        }
    }

Now, the properties MyProperty and MyOtherProperty are nicely shown in the Properties Window, and I can set their values directly there.

But when I close my form and I open it again, all my changes are lost, the properties being reset to show zero and an empty string.

What am I missing?
Should I inherit my MyProps class from certain special class or interfase?
Or some special attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2025-01-12 17:38:42

这对于评论和也许你的解决方案来说有点太多了,所以我用一个答案来回答你的评论,而不是用另一个评论:

当我将属性直接放在表单上时不会发生你意思是,您正在使用设计器来设置表单的某些属性。这些将被写入 MyForm.designer.cs 文件中。当您进入类的代码时,您会在构造函数中找到一个方法 InitializeComponent()。将光标置于其上并按 F12。在这里你可以看到设计师在所有属性中写入了什么。您应该尊重上述方法上面的注释,并且不要开始使用代码编辑器修改代码,除非您确实了解设计者如何以及何时在此处读取和编写代码(这是我可以在需要时解释的另一章)。否则,在代码更改后尝试使用设计器打开表单将导致错误消息或代码丢失。

如果您还想设置一些默认值,则应该返回构造函数并在 InitializeComponent() 函数下方添加所需的初始化代码,一切都应该按预期工作。

更新

正如您在评论中所写,您已经知道设计器如何与 *.designer.cs 文件交互。所以我真的无法理解您的具体问题,但也许其中一篇文章可以让您更深入地了解 Microsoft 如何编写其组件:

This is a little bit much for a comment and maybe your solution, so i'm answering to your comment with an answer instead with another comment:

With does not happen when I put properties directly on a form you mean, you are using the designer to set some property of the form. These will be written into the MyForm.designer.cs file. When you go into the code of your class you'll find within the constructor a method InitializeComponent(). Set the cursor on it an press F12. Here you can see what the designer has written into all the properties. You should respect the comment above the mentioned method and not start to modify the code with the code editor unless you really have understand how and when the designer will read and write code here (which is another chapter i can explain if needed). Otherwise it will happen that trying to opening your form with the designer after the code change will lead to an error message or code loss.

If you like to set some default value also, you should go back into the constructor and add the needed initialization code below the InitializeComponent() function and everything should work as expected.

Update

As you wrote in your comment you already know how the Designer interacts with the *.designer.cs file. So i really can't understand your concrete problem but maybe one of these articles can give you a more insight about how Microsoft wrote their components:

多孤肩上扛 2025-01-12 17:38:42

这是很正常的,因为每次关闭表单并再次打开它时,您都会从 MyPropsX 表单中获得一个新实例,因此最好的方法是将属性保存在任何类型的数据库中(sql、access、文本文件) ,...)

This is very normal, since each time you are closing the form and opening it again you are having a new instance from the form MyPropsX, so the best way would be to save your properties in any kind of a database (sql, access, textfiles,...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文