2012-02-01 7 views
1

안녕하세요 사용자 지정 개체를 만드는 것은 널리 게시 된 주제 일 수 있지만 코딩 기술이 부족하여 실제로 수행하려는 작업을 구현하는 데 문제가 있습니다.사용자 지정 개체 만들기 (두 개체 조합)

간단히 말해서 flowpanelLayout에서 런타임에 컨트롤을 추가하고 있습니다. 지금은 그냥 listboxes, 그 코드는 모두 잘 작동합니다. 추가하는 목록 상자에 레이블을 지정하는 방법을 원합니다. 텍스트 레이블을 사용하는 것보다 더 나은 방법을 생각할 수 없습니다. 난 목록 상자 및 다른 또는 뭔가 위에 같은 textlabel있는 사용자 지정 컨트롤 (가능한 경우) 일종의 만드는 것이 매끄러운 것이라고 생각했다. 이 방법은 현재 코드에서 새 사용자 지정 컨트롤을 추가하고 목록 상자 특성 및 레이블 텍스트 등을 모두 한 동작으로 할당 할 수 있습니다.

이것은 내가 생각했던 것입니다. 아마도 이것을 수행하는 더 좋은 방법 일 수도 있습니다.

내 현재 목록보기 생성 코드 :

public void addListView() 
     { 

      ListView newListView = new ListView(); 
      newListView.AllowDrop = true; 
      newListView.DragDrop += listView_DragDrop; 
      newListView.DragEnter += listView_DragEnter; 
      newListView.MouseDoubleClick += listView_MouseDoubleClick; 
      newListView.MouseDown += listView_MouseDown; 
      newListView.DragOver += listView_DragOver; 
      newListView.Width = 200; 
      newListView.Height = 200; 
      newListView.View = View.Tile; 
      newListView.MultiSelect = false; 

      flowPanel.Controls.Add(newListView); 
      numWO++; 

      numberofWOLabel.Text = numWO.ToString(); 
     } 

어쩌면 실제 가장 좋은 대답은 여기에 textLabel라는를 추가하고 일부 세트 넣어 좌표를 정의하는 것입니다. 당신이 무슨 생각을하는지 제게 알려주세요.

맞춤 컨트롤을 사용하는 경우 일부 리소스 나 예제를 제공해주세요. 감사하겠습니다.

답변

1

다음은이를 수행 할 수있는 사용자 정의 사용자 정의 컨트롤입니다. 제목을 설정하기 위해 TitleLabelText를 설정하기 만하면됩니다.

[Category("Custom User Controls")] 
public class ListBoxWithTitle : ListBox 
{ 
    private Label titleLabel; 
    public ListBoxWithTitle() 
    { 
     this.SizeChanged +=new EventHandler(SizeSet); 
     this.LocationChanged +=new EventHandler(LocationSet); 
     this.ParentChanged += new EventHandler(ParentSet); 

    } 
    public string TitleLabelText 
    { 
     get; 
     set; 
    } 
    //Ensures the Size, Location and Parent have been set before adding text 
    bool isSizeSet = false; 
    bool isLocationSet = false; 
    bool isParentSet = false; 
    private void SizeSet(object sender, EventArgs e) 
    { 
     isSizeSet = true; 
     if (isSizeSet && isLocationSet && isParentSet) 
     { 
      PositionLabel(); 
     } 
    } 
    private void LocationSet(object sender, EventArgs e) 
    { 
     isLocationSet = true; 
     if (isSizeSet && isLocationSet && isParentSet) 
     { 
      PositionLabel(); 
     } 
    } 
    private void ParentSet(object sender, EventArgs e) 
    { 
     isParentSet = true; 
     if (isSizeSet && isLocationSet && isParentSet) 
     { 
      PositionLabel(); 
     } 
    } 
    private void PositionLabel() 
    { 
     //Initializes text label 
     titleLabel = new Label(); 
     //Positions the text 10 pixels below the Listbox. 
     titleLabel.Location = new Point(this.Location.X, this.Location.Y + this.Size.Height + 10); 
     titleLabel.AutoSize = true; 
     titleLabel.Text = TitleLabelText; 
     this.Parent.Controls.Add(titleLabel); 
    } 

} 

사용 예 :

public Form1() 
    { 
     InitializeComponent(); 

     ListBoxWithTitle newitem = new ListBoxWithTitle(); 
     newitem.Size = new Size(200, 200); 
     newitem.Location = new Point(20, 20); 
     newitem.TitleLabelText = "Test"; 
     this.Controls.Add(newitem); 
    } 
+0

내가 찾던 정확히. 감사. – ikathegreat

관련 문제