为什么我的控件的属性不会在其类之外更改?

发布于 2024-10-07 23:59:56 字数 1467 浏览 4 评论 0原文

我是 C# 新手,但对编码并不陌生——已经这样做了近二十年——并且我正在构建的自定义控件中的属性存在问题,该控件继承自面板。当我放置属性时,我可以在设计器属性列表中看到它们,甚至可以设置它们,但是当运行我的小应用程序时,似乎没有使用这些属性值。如果我以编程方式更改属性,情况也是如此:没有错误,但我的控件什么也不做,就像它们没有正确设置一样。但是,如果我在课堂上以编程方式执行此操作,它们就会起作用。我的猜测是我的属性设置/获取内容中的某些内容不正确。请参阅以下代码块,了解我是如何做到这一点的:

public class ColorStrip : Panel
{
    // properties
    // ------------------------------------------
    // size of color clusters (boxes)
    private int _clusterSize = 20;
    // controls if show the buttons panel
    private Boolean _showButtons;

    // property setters/getters
    // ------------------------------------------
    // clusterSize...
    public int clusterSize
    {
        get { return _clusterSize; }
        set { _clusterSize = value; }
    }
    // showButtons...
    public Boolean showButtons
    {
        get { return _showButtons; }
        set { Console.Write(_showButtons); _showButtons = value; }
    }
    ....

因此,在我的表单中,例如在加载中,甚至在某处的单击事件中,如果我将 colorStrip1.showButtons = false;或 colorStrip1.showButtons = true;不管怎样(colorStrip1 将是在设计模式下将控件放入表单后的实例名称)...console.write 总是说“假”;即使我在设计属性列表中将其设置为“true”,它也不会反映设定的值,即使我将其默认为true,它也永远不会在外部发生变化。有什么想法吗?这些方法都没有获得新的和外部确定的属性值,显然 getter/setter 不起作用。在我看来,我在类之外设置或获取属性的方式不正确。它仅在其内部起作用,作为一种魅力...任何帮助...非常感谢!

干杯

p.s.澄清解决方案:

在这种情况下设置属性不起作用,因为我试图在构造函数中使用新的设置值,这似乎无法获取新值,因为它是,嗯,建造这个东西。如果我在设计模式下更改属性值>属性编辑器或对象外部的代码中,例如在其父窗体的加载事件中,它会更改它,但对除构造函数之外的所有方法都可读,当然:)

I'm new in C# but not new to coding --being doing it for almost two decades--, and have a problem with properties in a custom control I'm building, which inherits from a Panel. When I put my properties, I can see them in the Designer properties list and can even set them, but when running my little application, it seems these properties values are not used. The same if I change a property programatically: no error but my control does nothing, it is like they are not properly set. However, if I do it programatically whithin the class, they do work. My guess is that something in my properties set/get stuff is not right. Please see the following code chunk of how I'm doing it:

public class ColorStrip : Panel
{
    // properties
    // ------------------------------------------
    // size of color clusters (boxes)
    private int _clusterSize = 20;
    // controls if show the buttons panel
    private Boolean _showButtons;

    // property setters/getters
    // ------------------------------------------
    // clusterSize...
    public int clusterSize
    {
        get { return _clusterSize; }
        set { _clusterSize = value; }
    }
    // showButtons...
    public Boolean showButtons
    {
        get { return _showButtons; }
        set { Console.Write(_showButtons); _showButtons = value; }
    }
    ....

So in my form, for instance in the load or even in a click event somewhere, if I put colorStrip1.showButtons = false; or colorStrip1.showButtons = true; whatever (colorStrip1 would be the instance name after placing the control in the form in design mode)... console.write says always 'false'; Even if I set it in the design properties list as 'true' it will not reflect the settled value, even if I default it to true, it will never change externally. Any ideas? Non of the methods get the new and externally settled property value neither, obviously the getter/setter thing is not working. Seems to me I'm not doing right the way I set or get my properties outside the class. It works only inside it, as a charm...Any help...very appreciate!

Cheers

lithium

p.s. TO CLARIFY SOLUTION:

Setting the property in this case didn't work because I was trying to use a new set value within the constructor, which seems can't get the new values since it is, well, building the thing. If I change the property value in Design mode > Property editor or in code externally to the object, say in it's parent form's load event, it will change it but readable for all methods except the constructor, of course :)

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

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

发布评论

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

评论(2

春风十里 2024-10-14 23:59:56

这可能是执行顺序的问题。您的属性设置器只是设置一个变量,但实际上不会触发控件上的任何内容来更新与该变量相关的状态(例如添加或显示我假设的按钮)。

当您在其余初始化完成之前设置属性时,将使用该值,否则不会使用该值,因为在初始过程中默认值仍然是属性值。

您需要对设置器执行操作,这里有一些伪代码来说明:

set {
  _showButtons = value;
  if (alreadyInitialized) {
    UpdateButtons();
  }
}

注意:确保首先设置值,然后执行操作 - 否则您最终会使用旧值(就像您的 Console.Write() 正在做)。

It's likely an issue of the order of execution. Your property setter just sets a variable, but doesn't actually trigger anything on the control to update the state related to this variable (e.g. adding or showing the buttons I assume).

When you set the property befre the rest of the initialization is done, the value is being used, otherwise it isn't because during the initial go the default value is still the property value.

You need to act on the setter, here's some pseudocode to illustrate:

set {
  _showButtons = value;
  if (alreadyInitialized) {
    UpdateButtons();
  }
}

Note: make sure to first set the value, then act - otherwise you end up using the old value (just like your Console.Write() is doing).

挖鼻大婶 2024-10-14 23:59:56

引用的代码看起来没有问题。您确定引用的是同一 ColorStrip 实例吗?另外,检查您的 .Designer.cs 文件以确保设置属性的代码在那里。

事实上,尝试通过使用自动实现属性来简化代码:

public int clusterSize { get;set;}
public Boolean showButtons {get;set;}

public ColorStrip() { ... clusterSize = 20; ... }

The quoted code doesn't look problematic. Are you sure you're referencing the same instance of ColorStrip? Also, check your .Designer.cs file to ensure that the code setting the property is there.

In fact, try simplifying your code by using auto-implementing properties:

public int clusterSize { get;set;}
public Boolean showButtons {get;set;}

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