User control is not updated in Visual Studio Designer

I am creating a custom control that produces radio buttons (No, it should not be radio buttons. I'm just trying to learn how to do this so that I can do some more complex things along the way, it may contain several lists of controls) that are added via the Items property ( similar to some other controls).

I can build a project, drag it onto the form from the component panel and add switches through the Items property. Unfortunately, this is not updated in the designer unless you do:

  • Restore a project 2-3 times
  • Close and reopen the form in the designer

At first, I had logic that put them in the form contained in the constructor after initialization, but this does not work, so I switched to Form_Load.

What am I missing? The above options are just short-term workarounds, not solutions.

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 } } 
+6
source share
2 answers

You should not use the Load event or constructor, because when you add a control using the constructor tool, an instance of UserControl is created and the Load event is fired. In your case, when this happens, _item is still empty. Another problem is that there are some problems with serializing the list, so I would use an 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 ); } } } } 

Result in the form constructor:

enter image description here

+4
source

The reason is that you only add controls to the ControlsCollection in the Load event. The following is a guide to creating container controls in WinForms that I searched on google: http://www.codeproject.com/Articles/9238/WinForms-Custom-Container-Control

0
source

Source: https://habr.com/ru/post/925354/


All Articles