2012-05-08 4 views
0

디자인 타임을 통해 사용자 지정 컨트롤 속성 값을 올바르게 읽는 방법은 무엇입니까?사용자 지정 컨트롤 속성을 읽으면 생성자에서 0이 반환됩니다.

현재 행 및 열은 컨트롤의 속성 목록 (idenet)으로 설정되지만 컨트롤의 생성자에서 읽을 때 두 속성은 모두 0을 반환합니다. 응용 프로그램을 시작하는 동안 속성이 할당되기 전에 생성자가 실행되고있는 것 같습니다.

올바른 방법은 무엇입니까?

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; 
       } 
      } 
     } 
    } 
+1

물론 생성자는 속성 할당 전에 실행됩니다. 속성 중 하나가 변경되면 그리드가 새로 고쳐 지도록 변경해야합니다. – SimpleVar

+0

Load 이벤트에서 수행 –

+0

duh, 감사합니다. –

답변

1

결국 행/열 속성 중 하나가 변경 될 때마다 표를 리메이크하고 싶습니다. 그리드를 다시 작성하고 속성이 설정 될 때마다 호출하는 메서드가 있어야합니다.

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... 
+0

대단히 감사합니다! –