Клянусь, я искал ответ в течение нескольких часов, но не смог найти ни одного понятного примера или статьи :-( Как сделать, чтобы свойство пользовательского элемента управления Winform сохранило значение времени его разработки? Я добавил новый объект UserControl. I на элемент управления наложена метка. Добавлено свойство «KeyText». В нескольких статьях я упоминал атрибут «DesignerSerailizationVisibility», поэтому я также добавил его.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string KeyText
{
get { return label1.Text; }
set
{
label1.Text = value;
}
}
В элементе управления значение по умолчанию: label1.Text - «X».
Затем я добавил элемент управления в форму. Свойство «KeyText» показало в качестве одного из свойств элемента управления. Отлично. Поэтому я изменил его на «A». Форма времени разработки показала A ". Отлично. Я скомпилировал и запустил программу - элемент управления в форме показал" X "вместо" A ".
Что мне нужно изменить, чтобы сделать свойство сохраняемым? В VB6 я использую что-то под названием PropBag для сохранения и чтения свойств времени разработки.
Вот полный код моего тестового проекта. Форма клиента Form1 имеет два экземпляра: пользовательский элемент управления.
ctkKbdLabel.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouchKndTest.Properties;
namespace TouchKndTest
{
public partial class ctkKbdKeyLabel : UserControl
{
public ctkKbdKeyLabel()
{
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string KeyText
{
get { return this.label1.Text; }
set
{
label1.Text = value;
}
}
}
}
ctkKbdLabel.Designer.cs:
namespace TouchKndTest
{
partial class ctkKbdKeyLabel
{
/// <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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.Color.White;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Font = new System.Drawing.Font("Times New Roman", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 55);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// ctkKbdKeyLabel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.Controls.Add(this.label1);
this.Name = "ctkKbdKeyLabel";
this.Size = new System.Drawing.Size(52, 61);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
}
}
Форма тестирования клиента, Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TouchKndTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Форма тестирования клиента Form1.designer.cs:
namespace TouchKndTest
{
partial class Form1
{
/// <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 Windows Form 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.ctkKbdKeyLabel3 = new TouchKndTest.ctkKbdKeyLabel();
this.ctkKbdKeyLabel2 = new TouchKndTest.ctkKbdKeyLabel();
this.SuspendLayout();
//
// ctkKbdKeyLabel3
//
this.ctkKbdKeyLabel3.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.ctkKbdKeyLabel3.Location = new System.Drawing.Point(94, 12);
this.ctkKbdKeyLabel3.Name = "ctkKbdKeyLabel3";
this.ctkKbdKeyLabel3.Size = new System.Drawing.Size(62, 60);
this.ctkKbdKeyLabel3.TabIndex = 2;
//
// ctkKbdKeyLabel2
//
this.ctkKbdKeyLabel2.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.ctkKbdKeyLabel2.Location = new System.Drawing.Point(25, 12);
this.ctkKbdKeyLabel2.Name = "ctkKbdKeyLabel2";
this.ctkKbdKeyLabel2.Size = new System.Drawing.Size(53, 60);
this.ctkKbdKeyLabel2.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.ctkKbdKeyLabel3);
this.Controls.Add(this.ctkKbdKeyLabel2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private ctkKbdKeyLabel ctkKbdKeyLabel2;
private ctkKbdKeyLabel ctkKbdKeyLabel3;
}
}