6

我正在創建一個自定義控件,用於設置單選按鈕(不,它不必是單選按鈕,我只是想學習如何做到這一點,所以我可以製作更復雜的東西,可能包含多個控件列表),這些東西通過Items屬性添加(類似於其他控件)。自定義控件不會在Visual Studio Designer中更新

我可以構建項目,將其拖到組件面板的表單上,並通過Items屬性添加單選按鈕。不幸的是,這是不更新設計師除非您:

  • 重建項目的2-3倍
  • 關閉並重新打開窗體在設計

起初,我有這些對放邏輯在Initialize之後的構造函數中包含的表單,但沒有工作,所以我轉移到了Form_Load。

我錯過了什麼?以上選項只是短期解決方法,而不是解決方案。

RBLTest.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WFCL_Library 
{ 
    public partial class RBLTest : UserControl 
    { 
     private List<RadioButton> _items; 

     private int leftSpacing = 100; 
     private int topSpacing = 25; 

     public RBLTest() 
     { 
      _items = new List<RadioButton>(); 
      InitializeComponent(); 
     } 

     private void RadioButtonList_Load(object sender, EventArgs e) 
     { 
      int curLeftPos = 0; 
      int curTopPos = 0; 
      foreach (RadioButton rb in _items) 
      { 
       rb.Location = new Point(curLeftPos, curTopPos); 
       rb.Size = new Size(85, 17); 

       curLeftPos += leftSpacing; 

       if (curLeftPos > this.Width) 
       { 
        curLeftPos = 0; 
        curTopPos += topSpacing; 
       } 

       this.Controls.Add(rb);     
      } 
     } 

     [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
     public List<RadioButton> Items 
     { 
      get 
      { 
       return _items; 
      } 
      set 
      { 
       _items = value; 
      } 
     } 
    }  
} 

RBLTest.Designer.cs

namespace WFCL_Library 
{ 
    partial class RBLTest 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Component Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 
      // 
      // RBLTest 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.Name = "RBLTest"; 
      this.Size = new System.Drawing.Size(407, 44); 
      this.Load += new System.EventHandler(this.RadioButtonList_Load); 
      this.ResumeLayout(false); 

     } 

     #endregion 

    } 
} 

回答

4

您should't使用Load事件或構造,因爲當你與設計師工具創建UserControl的實例,並添加控件Load事件被觸發。在你遇到這種情況時_item仍然是空的。另一個問題是,有一些問題,序列化的列表,所以我會用一個數組:

public partial class RBLTest : UserControl { 
    private RadioButton[] _items; 

    private int leftSpacing = 100; 
    private int topSpacing = 25; 

    public RBLTest() { 
     InitializeComponent(); 
    } 

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    public RadioButton[] Items { 
     get { 
      return _items; 
     } 
     set { 
      _items = value; 

      int curLeftPos = 0; 
      int curTopPos = 0; 
      foreach (RadioButton rb in _items) { 
       rb.Location = new Point(curLeftPos, curTopPos); 
       rb.Size = new Size(85, 17); 

       curLeftPos += leftSpacing; 

       if (curLeftPos > this.Width) { 
        curLeftPos = 0; 
        curTopPos += topSpacing; 
       } 

       this.Controls.Add(rb); 
      } 
     } 
    } 
} 

在窗體設計結果:

enter image description here

+1

Ohhhhhhhhhhhh我只是想抱抱你的權利現在,我花了這麼長的時間與這個沒有運氣發現任何關於這個問題的東西,我的可憐的谷歌搜索。你碰巧有一篇關於討論序列化設計器中的泛型列表的問題的文章嗎? – Mohgeroth

+1

哈哈:)我很高興幫助你! –

+2

@Mohgeroth你不需要另一個集合,你已經在Controls集合中有一個集合,這是你的子控件不管怎麼生活的地方。 – Tergiver

相關問題