读书人

如和在自定义控件添加ITEM属性解决方案

发布时间: 2012-02-06 15:52:45 作者: rapoo

如和在自定义控件添加ITEM属性
如题

[解决办法]
在封装类中做一个public的item集合成员,即可!
[解决办法]
由ComboBox继承一个子类
在子类里添加所需的属性.
[解决办法]

//****************************
//www.21centuryit.com *
//---------------------------*
//用户自定义控件,出自MSDN *
//****************************


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

// This sample demonstrates the use of various attributes for
// authoring a control.
namespace AttributesDemoControlLibrary
{
// This is the event handler delegate for the ThresholdExceeded event.
public delegate void ThresholdExceededEventHandler(ThresholdExceededEventArgs e);

// This control demonstrates a simple logging capability.
[ComplexBindingProperties( "DataSource ", "DataMember ")]
[DefaultBindingProperty( "TitleText ")]
[DefaultEvent( "ThresholdExceeded ")]
[DefaultProperty( "Threshold ")]
[HelpKeywordAttribute(typeof(UserControl))]
[ToolboxItem( "System.Windows.Forms.Design.AutoSizeToolboxItem,System.Design ")]
public class AttributesDemoControl : UserControl
{

// This backs the Threshold property.
private object thresholdValue;

// The default fore color value for DataGridView cells that
// contain values that exceed the threshold.
private static Color defaultAlertForeColorValue = Color.White;

// The default back color value for DataGridView cells that
// contain values that exceed the threshold.
private static Color defaultAlertBackColorValue = Color.Red;

// The ambient color value.
private static Color ambientColorValue = Color.Empty;

// The fore color value for DataGridView cells that
// contain values that exceed the threshold.
private Color alertForeColorValue = defaultAlertForeColorValue;

// The back color value for DataGridView cells that
// contain values that exceed the threshold.
private Color alertBackColorValue = defaultAlertBackColorValue;

// Child controls that comprise this UserControl.
private TableLayoutPanel tableLayoutPanel1;
private DataGridView dataGridView1;
private Label label1;

// Required for designer support.
private System.ComponentModel.IContainer components = null;

// Default constructor.
public AttributesDemoControl()
{
InitializeComponent();
}

[Category( "Appearance ")]
[Description( "The title of the log data. ")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Localizable(true)]
[HelpKeywordAttribute( "AttributesDemoControlLibrary.AttributesDemoControl.TitleText ")]
public string TitleText
{
get
{
return this.label1.Text;
}

set
{
this.label1.Text = value;


}
}

// The inherited Text property is hidden at design time and
// raises an exception at run time. This enforces a requirement
// that client code uses the TitleText property instead.
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}

[AmbientValue(typeof(Color), "Empty ")]
[Category( "Appearance ")]
[DefaultValue(typeof(Color), "White ")]
[Description( "The color used for painting alert text. ")]
public Color AlertForeColor
{
get
{
if (this.alertForeColorValue == Color.Empty &&
this.Parent != null)
{
return Parent.ForeColor;
}

return this.alertForeColorValue;
}

set
{
this.alertForeColorValue = value;
}
}

读书人网 >C#

热点推荐