6

Tôi đang tạo một điều khiển tùy chỉnh để đưa ra các nút radio (Không cần phải có nút radio. Tôi chỉ đang cố gắng tìm hiểu cách thực hiện điều này vì vậy tôi có thể làm cho một số thứ phức tạp hơn xuống đường có thể chứa nhiều danh sách điều khiển) được thêm vào thông qua thuộc tính Items (tương tự như một số điều khiển khác).Điều khiển tùy chỉnh không cập nhật trong Visual Studio Designer

Tôi có thể tạo dự án, kéo nó vào biểu mẫu từ bảng thành phần và thêm nút radio qua thuộc tính Mục. Thật không may này không được cập nhật trong thiết kế, trừ khi bạn hoặc:

  • Rebuild dự án 2-3 lần
  • Đóng và mở lại mẫu tại nhà thiết kế

Lúc đầu, tôi đã có logic mà đặt này trên biểu mẫu chứa trong hàm dựng sau khi khởi tạo nhưng điều đó không hoạt động nên tôi đã chuyển sang Form_Load.

Tôi đang thiếu gì? Các tùy chọn ở trên chỉ là giải pháp ngắn hạn chứ không phải giải pháp.

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 

    } 
} 

Trả lời

4

Bạn should't sử dụng sự kiện Load hoặc các nhà xây dựng, bởi vì khi bạn thêm sự kiểm soát với các công cụ thiết kế một thể hiện của các UserControl được tạo ra và sự kiện Load được kích hoạt. Trong trường hợp của bạn khi điều này xảy ra _item vẫn còn trống. Một vấn đề khác là có một số vấn đề serializing một danh sách, vì vậy tôi muốn sử dụng một mảng:

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

Kết quả trong Form Designer:

enter image description here

+1

Ohhhhhhhhhhhh Tôi chỉ muốn ôm em ngay bây giờ, tôi đã dành quá nhiều thời gian chơi đùa với điều này mà không có may mắn tìm thấy bất cứ điều gì về chủ đề này với tìm kiếm google nghèo của tôi. Bạn có xảy ra để có một tham chiếu đến một bài báo thảo luận về các vấn đề với serializing danh sách chung trong các nhà thiết kế? – Mohgeroth

+1

Haha :) Tôi rất vui được giúp bạn! –

+2

@Mohgeroth Bạn không cần một bộ sưu tập khác, bạn đã có một bộ sưu tập trong bộ sưu tập Điều khiển, nơi mà con bạn kiểm soát phải sống dù sao đi nữa. – Tergiver

Các vấn đề liên quan