views:

117

answers:

4

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. 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:

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.

A: 

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.

SLaks
A: 

Ok, I figured what is the problem. It comes from the Form1.Designer.cs

In the InitializeComponent(), it does a

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.

Why does it add automatically code to reset my global variables? Is there a way to prevent the automatic addition of this code?

Wildhorn
Still not sure it's what you need, but see my answer: http://stackoverflow.com/questions/3054059/c-custom-control-properties-load-before-load-and-arrays-are-empties/3054476#3054476
Codesleuth
A: 

If I understand you correctly, you have properties that shouldn't be altered because they are used as some sort of const alternative internally? If so, tag them with 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.

Codesleuth
A: 

I found out the problem.

It was due to the reference of the dll of the control. 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. So I completely wiped the reference and made a new one with the unbugged version dll and everything work fine now.

Wildhorn
Look liked the reference problem was the assembly version being 1.0.0.* ... I changed it to 1.0.0.0 and now it work like a charm.
Wildhorn