0

How does one properly read values of custom control properties set via design time?

Currently, rows and columns are set in the properties list of the control (vs.net as ide), however both properties return 0 when read from the constructor of the control. I suspect the constructor is executing before the properties are assigned during start of application.

What is the correct way of doing this?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;

    namespace HexagonalLevelEditor.HexLogic{

        class HexGrid : System.Windows.Forms.Panel {

            public HexGrid() {
                for(int c = 0; c < this.Columns; ++c) {
                    for(int r = 0; r < this.Rows; ++r) {
                        HexCell h = new HexCell();
                        h.Width = 200;
                        h.Height = 200;
                        h.Location = new System.Drawing.Point(c*200, r*200);
                        this.Controls.Add(h);
                    }
                }
            }

            private void InitializeComponent() {
                this.SuspendLayout();
                this.ResumeLayout(false);
            }

            private int m_rows;
            [Browsable(true), DescriptionAttribute("Hexagonal grid row count."), Bindable(true)]
            public int Rows {
                get {
                    return m_rows;
                }

                set {
                    m_rows = value;
                }
            }

            private int m_columns;
            [Browsable(true), DescriptionAttribute("Hexagonal grid column count."), Bindable(true)]
            public int Columns { 
                get{
                    return m_columns;
                }

                set {
                    m_columns = value;
                }
            }
        }
    }
Private Void
  • 307
  • 1
  • 3
  • 14
  • 1
    Of course the constructor is executed before the properties assignments. You should make it so the grid is refreshed when either of the properties is changed. – SimpleVar May 08 '12 at 03:21

1 Answers1

1

Ultimately, you really want to remake the grid whenever either of the properties Rows / Columns change. So you should have a method which remakes the grid, and call it whenever the properties are being set.

public void RemakeGrid()
{
    this.ClearGrid();

    for(int c = 0; c < this.Columns; ++c)
    {
        for(int r = 0; r < this.Rows; ++r)
        {
            HexCell h = new HexCell();
            h.Width = 200;
            h.Height = 200;
            h.Location = new System.Drawing.Point(c*200, r*200);
            this.Controls.Add(h);
        }
    }
}

private int m_rows;

[Browsable(true), DescriptionAttribute("Hexagonal grid row count."), Bindable(true)]
public int Rows
{
    get
    {
        return m_rows;
    }
    set
    {
        m_rows = value;
        this.RemakeGrid();
    }
}

// Same goes for Columns...
SimpleVar
  • 14,044
  • 4
  • 38
  • 60