2014-01-17 3 views
0

C#에서 사용자 지정 컨트롤을 만들었습니다. 그것의 매우 단순화 된 버전은 다음과 같습니다양식에서 렌더링되도록 다른 사용자 지정 컨트롤에 사용자 지정 컨트롤을 제대로 추가하려면 어떻게합니까?

나는 폼에 추가
class WellControl : Control 
    {     
     Pen circlePen = new Pen(Color.Black, 5); 
     Brush wellShader = new SolidBrush(Color.BlueViolet); 
     Brush wellBlanker = new SolidBrush(Color.LightGray); 

     public WellControl(int wellNum) 
     { 
      InitializeComponent(); 
     } 

     private void DrawWell() 
     { 
      using (Graphics well = this.CreateGraphics()) 
      { 
       this.Size = new Size(WellSize, WellSize); 
       if (this.selected) 
       { 
        well.FillEllipse(wellShader, ellipseCoords); 
       } 
       else 
       { 
        well.FillEllipse(wellBlanker, ellipseCoords); 
       } 

       well.DrawEllipse(circlePen, ellipseCoords); 
       using (Font wellNumberFont = new Font("Arial", 14, FontStyle.Bold)) 
       { 
        well.DrawString(WellNum.ToString(), wellNumberFont, Brushes.Black, new Point(13, 13)); 
       } 
      } 
     } 

     private void WellPaintEventHandler(object sender, EventArgs e) 
     { 
      DrawWell(); 
     } 

     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 
      this.Paint += new System.Windows.Forms.PaintEventHandler(WellPaintEventHandler); 
      this.ResumeLayout(); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      base.Dispose(disposing); 
      if (disposing) 
      { 
       //dispose all the custom stuff 
      } 
     } 
    } 

, 그것은 (다시, 간단한 예)가 제대로 렌더링 : 나는 다른 사용자 정의 컨트롤이

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      for (int i = 0; i < 4; i++) 
      { 
       WellControl newWell = new WellControl(i + 1);    
       newWell.Location = new Point(15, 50 * i); 
       newWell.Size = new System.Drawing.Size(45, 45); 
       this.Controls.Add(newWell); 
      } 
     } 
    } 

" Plate "는 많은"Wells "를 보유하기위한 것입니다. 접시에 걸쳐 균등하게 우물을 끌기 위해 의도 된 코드는 아마 지금 짜증 난 그냥 뭔가 보려고 해요 : 내가 this.Controls.Add를 사용하여의 WinForm에이 컨트롤을 추가 할 경우 새 (

class PlateControl : Control 
    { 

     Pen blackPen = new Pen(Color.Black, 3); 

     public PlateControl() 
     { 
      this.Size = new Size(600, 800); 
      List<WellControl> plateWells = new List<WellControl>(); 
      int column = 1; 
      int row = 0; 
      for (int i = 1; i <= 96; i++) 
      { 
       column = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(i/8))); 
       row = i % 8; 
       WellControl newWell = new WellControl(i + 1); 
       newWell.Name = "wellControl" + i; 
       newWell.Location = new Point(column * 50, row * 50); 
       newWell.Size = new System.Drawing.Size(45, 45); 
       newWell.TabIndex = i; 
       newWell.WellSize = 45; 
       plateWells.Add(newWell); 
       newWell.Visible = true; 
      } 
      InitializeComponent(); 
     } 

     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 
      this.Paint += new System.Windows.Forms.PaintEventHandler(PlatePaintEventHandler); 
      this.ResumeLayout(); 
     } 

     private void DrawPlate() 
     { 
      using (Graphics plate = this.CreateGraphics()) 
      { 
       Point topLeft = new Point(0, 0); 
       Point topRight = new Point(600, 0); 
       Point bottomRight = new Point(600, 400); 
       Point bottomLeft = new Point(0, 400); 

       plate.DrawLine(blackPen, topLeft, topRight); 
       plate.DrawLine(blackPen, topRight, bottomRight); 
       plate.DrawLine(blackPen, bottomRight, bottomLeft); 
       plate.DrawLine(blackPen, bottomLeft, topLeft); 
      } 
     } 

     private void PlatePaintEventHandler(object sender, EventArgs e) 
     { 
      DrawPlate(); 
     } 
    } 

을 PlateControl()), 사각형이 렌더링되지만 WellControls가 생성자 루프의 PlateControl에 추가되지 않습니다. 내가 뭘 잘못하고 있니?

+1

단순화 된 코드를 표시했지만 단순화 된 것으로 표시되어 일부 변수에 대한 정의가 누락되었습니다. –

+0

WellControl에 컴파일 오류가 있습니다. – Ramashankar

답변

2

플레이트 컨트롤에 WallControl 목록을 추가해야합니다.

public PlateControl() 
     { 
      this.Size = new Size(600, 800); 
      List<WellControl> plateWells = new List<WellControl>(); 
      int column = 1; 
      int row = 0; 
      for (int i = 1; i <= 96; i++) 
      { 
       column = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(i/8))); 
       row = i % 8; 
       WellControl newWell = new WellControl(i + 1); 
       newWell.Name = "wellControl" + i; 
       newWell.Location = new Point(column * 50, row * 50); 
       newWell.Size = new System.Drawing.Size(45, 45); 
       newWell.TabIndex = i; 
       newWell.WellSize = 45; 
       plateWells.Add(newWell); 
       newWell.Visible = true; 
      } 
      this.Controls.AddRange(plateWells.ToArray()); 
      InitializeComponent(); 
     } 
+0

Doh, 당신도 저를 때려 눕 힙니다. 느린 타이핑. – Amleth

+0

그랬어. 고맙습니다! – DeeDee

관련 문제