6

Sto creando un controllo personalizzato che espone i pulsanti di opzione (No, non è necessario che siano pulsanti di opzione. Sto solo cercando di imparare come farlo può rendere più complicate le cose che possono contenere più elenchi di controlli) che vengono aggiunte tramite la proprietà Items (simile ad altri controlli).Controllo personalizzato non si aggiorna in Visual Studio Designer

Posso creare il progetto, trascinarlo su un modulo dal pannello componenti e aggiungere pulsanti di opzione tramite la proprietà Items. Purtroppo questo non viene aggiornato nel design a meno che non sia:

  • Rigenerare il progetto 2-3 volte
  • Chiudere e riaprire il modulo in Designer

In un primo momento ho avuto la logica che mette questi su il modulo contenuto nel costruttore dopo Initialize ma che non funzionava, quindi mi sono spostato a Form_Load.

Cosa mi manca? Le opzioni di cui sopra sono soluzioni alternative a breve termine, non soluzioni.

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 

    } 
} 

risposta

4

È should't utilizzare l'evento Load o il costruttore, perché quando si aggiunge il controllo con lo strumento di progettazione viene creata un'istanza del UserControl e l'evento Load viene attivato. Nel tuo caso, quando ciò accade, _item è ancora vuoto. Un altro problema è che ci sono alcuni problemi serializzazione una lista, quindi mi piacerebbe usare un array:

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); 
      } 
     } 
    } 
} 

Il risultato nella finestra di progettazione:

enter image description here

+1

Ohhhhhhhhhhhh voglio solo abbracciarti destra ora, ho passato così tanto tempo a giocare con questo senza fortuna a trovare qualcosa su questo argomento con le mie povere ricerche su google. Ti capita di avere un riferimento ad un articolo che discute i problemi con la serializzazione di liste generiche nel designer? – Mohgeroth

+1

Haha :) Sono felice di aiutarti! –

+2

@Mohgeroth Non hai bisogno di un'altra collezione, ne hai già una nella collezione Controls che è dove i controlli figlio devono vivere comunque. – Tergiver