惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
有赞技术团队
有赞技术团队
H
Help Net Security
V
Visual Studio Blog
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LangChain Blog
N
Netflix TechBlog - Medium
罗磊的独立博客
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements

博客园 - 极无宪

小红书如何采集市集 Textrimming 失效怎么办? 现在找个vs2019 安装好难呀! WPF:如何将调试信息写入到输出窗口? System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法。 警惕:开启了 SQL Profiler监测SQL语句忘记关了,C盘居然被占满了 nuget 总是获取失败怎么办? 还在用WebBrowser吗?你out了! 关于打印机共享的注意事项——又被叫去修电脑了 MVVM转换器Int2StringConverter基础类 ServicesController 的使用 winform中坐标系转换的问题,获取某点在屏幕中的绝对位置等 使用本地IIS Web 服务起转到win7 iis7后的系列问题 客户端元素中找不到与此名称匹配的终结点 关于错误的友好提示 调侃 亚马逊的 kindlegen VC编写在windows7下以管理员权限运行的程序(转) vs 已经在解决方案中打开了具有该名称的项目 解决方案"System.InvalidOperationException: 配置有 NoSecurityChanges 标志的 AppDomainManager 修改了 AppDomain 的安全状态"
propertygrid 下拉列表
极无宪 · 2013-07-29 · via 博客园 - 极无宪

2013-07-29 17:08  极无宪  阅读(586)  评论()    收藏  举报

using System;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace StandardValuesTest
{ 
public class StandardValuesIntConverter : System.ComponentModel.TypeConverter
{
private ArrayList values;
public StandardValuesIntConverter()
{
// Initializes the standard values list with defaults.
values = new ArrayList(new int[] { 1, 2, 3, 4, 5 });
}

// Indicates this converter provides a list of standard values.
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}

// Returns a StandardValuesCollection of standard value objects.
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
{ 
// Passes the local integer array.
StandardValuesCollection svc = 
new StandardValuesCollection(values); 
return svc;
}

// Returns true for a sourceType of string to indicate that 
// conversions from string to integer are supported. (The 
// GetStandardValues method requires a string to native type 
// conversion because the items in the drop-down list are 
// translated to string.)
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
if( sourceType == typeof(string) )
return true;
else 
return base.CanConvertFrom(context, sourceType);
}

// If the type of the value to convert is string, parses the string 
// and returns the integer to set the value of the property to. 
// This example first extends the integer array that supplies the 
// standard values collection if the user-entered value is not 
// already in the array.
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if( value.GetType() == typeof(string) )
{
// Parses the string to get the integer to set to the property.
int newVal = int.Parse((string)value);

// Tests whether new integer is already in the list.
if( !values.Contains(newVal) )
{
// If the integer is not in list, adds it in order.
values.Add(newVal);
values.Sort();
} 
// Returns the integer value to assign to the property.
return newVal;
}
else
return base.ConvertFrom(context, culture, value);
}
}

// Provides a test control with an integer property associated with 
// the StandardValuesIntConverter type converter.
public class IntStandardValuesControl : System.Windows.Forms.UserControl
{
[TypeConverter(typeof(StandardValuesIntConverter))]
public int TestInt
{
get
{
return this.integer_field;
}
set
{
if(value.GetType() == typeof(int))
this.integer_field = value;
}
}
private int integer_field = 0;

public IntStandardValuesControl()
{
this.BackColor = Color.White;
this.Size = new Size(472, 80);
}

// OnPaint override displays instructions for the example.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if(this.DesignMode)
{
e.Graphics.DrawString("TypeConverter.GetStandardValues Example Control", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Blue), 5, 5);
e.Graphics.DrawString("The type converter for the TestInt property of this", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 20);
e.Graphics.DrawString("component provides a list of standard values to the", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 30);
e.Graphics.DrawString("Properties window. Setting a value through a property", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 40);
e.Graphics.DrawString("grid adds it to the list of standard values.", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 50); 
}
else
{
e.Graphics.DrawString("TypeConverter.GetStandardValues Example Control", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Blue), 5, 5); 
e.Graphics.DrawString("This control was intended for use in design mode.", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 20); 
}
}
}
}

转msdn http://msdn.microsoft.com/zh-cn/library/ayybcxe5(v=vs.80).aspx