简体   繁体   English

在Load和数组为空之前加载C#自定义控件属性

[英]C# Custom Control Properties load before Load and arrays are empties

EDITED FOR BETTER UNDERSTANDING 为更好的理解而编辑

I made a custom control with propertise for some global variables. 我做了一些具有全局变量的自定义控件。

private string[] LBTitles = new string[1] { "Rien" };
//...
[CategoryAttribute("WildData"), DescriptionAttribute("Tableau des noms des titres au dessus de le listbox")]
public string[] Titles
{
    get { return LBTitles; }

    set { LBTitles = value; OnColumns(EventArgs.Empty); }
}

OnColums does many things to format the control. OnColums做很多事情来格式化控件。 One among others is: 其中之一是:

int vLongest = 0;
//...
//Si le plus long est plus petit que le titre de la colonne
if (vLongest < LBTitles[i].Length)
{
    vLongest = LBTitles[i].Length;
}

The above is for my control itself. 以上是我自己控制的。 Everything work fine, its a wonderful day, etc. 一切正常,美好的一天,等等。

Now when it comes to add it to a form: - I add it, everything is ok - I modify the properties via the design, everything is ok - I try to run it... there is the problem. 现在,将其添加到表单中:-添加完毕,一切正常-通过设计修改属性,一切正常-我尝试运行它...这是问题。

When I build, it add into InitializeComponent() the following code: 构建时,它将以下代码添加到InitializeComponent()中:

this.wildList1 = new WildList.WildList();
//Which is ok but also it add, everytime I build, this:
            // 
            // wildList1
            // 
            this.wildList1.Colors = new string[] {
        null};
            this.wildList1.Font = new System.Drawing.Font("Courier New", 8F);
            this.wildList1.Location = new System.Drawing.Point(211, 33);
            this.wildList1.Name = "wildList1";
            this.wildList1.Positions = new int[] {
        0};
            this.wildList1.Size = new System.Drawing.Size(238, 224);
            this.wildList1.TabIndex = 16;
            this.wildList1.Titles = new string[] {
        null};

It add lines of code that reset my arrays. 它添加了几行代码来重置我的数组。 Why? 为什么? How can I get ride of them? 我怎么能骑他们呢? Or at least, make them use the values entered by the programmer (aka me) into the designer? 或者至少让它们使用程序员(又名我)输入到设计器中的值?

Because when it goes throu the line that reset it, it also call the property "set", which call OnColumns, which then try to do stuff with empty arrays, which cause a crash. 因为当它经过重置它的行时,它还会调用属性“ set”,该属性将调用OnColumns,然后尝试对空数组进行处理,这会导致崩溃。

You should populate the arrays in the control's constructor. 您应该在控件的构造函数中填充数组。

If you want to see the exact order that the properties are set, look at the form's InitializeComponent method. 如果要查看设置属性的确切顺序,请查看表单的InitializeComponent方法。

Ok, I figured what is the problem. 好的,我想出了问题所在。 It comes from the Form1.Designer.cs 它来自Form1.Designer.cs

In the InitializeComponent(), it does a 在InitializeComponent()中,它执行

this.wildList1 = new WildList.WildList();

which is good and all, but some lines below, it does a: 一切都很好,但是下面几行显示了:

// 
            // wildList1
            // 
            this.wildList1.Colors = new string[] {
        null};
            this.wildList1.Name = "wildList1";
        this.wildList1.Font = new System.Drawing.Font("Courier New", 8F);
        this.wildList1.Location = new System.Drawing.Point(190, 64);
        this.wildList1.Name = "wildList1";
        this.wildList1.Positions = new int[] {
    0};
        this.wildList1.Size = new System.Drawing.Size(238, 224);
        this.wildList1.TabIndex = 16;
        this.wildList1.Titles = new string[] {
    null};

etc... which will call the "set" of the property "Colors", calling the function, with the now empty array. 等等...这将使用现在为空的数组调用属性“ Colors”的“ set”,并调用函数。

Why does it add automatically code to reset my global variables? 为什么它会自动添加代码以重置全局变量? Is there a way to prevent the automatic addition of this code? 有没有办法防止自动添加此代码?

If I understand you correctly, you have properties that shouldn't be altered because they are used as some sort of const alternative internally? 如果我对您的理解正确,那么您具有不应更改的属性,因为它们在内部用作某种const替代项? If so, tag them with DesignerSerializationVisibilityAttribute 如果是这样,请使用DesignerSerializationVisibilityAttribute对其进行标记

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int[] MyArrayProperty
{
    get 
    {
        // get here
    }       
}

void SetMyArrayProperty(int[] a)
{
    // set here
}

This hides them from the designer and prevents it overwriting them. 这将它们隐藏在设计器中,并防止覆盖它们。 Note also removing the set portion of the property prevents the designer and properties window altering the value also. 请注意,删除属性的set部分还可以防止设计器和属性窗口也更改该值。

I found out the problem. 我发现了问题。

It was due to the reference of the dll of the control. 这是由于引用了控件的dll。 It was using a "cached" or I dont know version of my control when I tested it for the first time (and was buggy), even if I was changing the .dll file that it was using. 它使用的是“缓存的”,或者当我第一次测试控件时(它有错误),我不知道控件的版本,即使我正在更改它使用的.dll文件也是如此。 So I completely wiped the reference and made a new one with the unbugged version dll and everything work fine now. 因此,我完全删除了引用,并使用未经调试的dll版本制作了一个新引用,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM