ComboBox没有到具体一个item的Tag属性。
你可以使用一个结构体或对象添加到到Items,以此来扩充Item的属性。这样像Tag属性就容易实现了。
你只需重写结构体或类的ToString方法。比如:
struct itemEx
{
public object Tag;
public string Text;
public itemEx(object tag, string text)
{
this.Tag = tag;
this.Text =text;
}
public override string ToString()
{
return this.Text;
}
}
itemEx item = new itemEx(123, "123");
this.combobox1.Items.Add(item);
//取值:
itemEx item =(itemEx) this.combobox1.SelectedItem;
item.Tag;
| |
|
对我有用[0] 丢个板砖[0] 引用 举报 管理 TOP
精华推荐:div+css和table布局的讨论,欢迎拍砖!
|
| #2楼 得分:0回复于:2007-10-12 14:30:06
谢谢,我后来自己做了个shortstringdirectory,你的是另一个参考不错,谢谢
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace M_RC
{
class ShortStringDictionary:DictionaryBase
{
#region
public String this[String key]
{
get
{
return ((String)Dictionary[key]);
}
set
{
Dictionary[key] = value;
}
}
public ICollection Keys {
get{
return(Dictionary.Keys);
}
}
public ICollection Values
{
get
{
return (Dictionary.Values);
}
}
public void Add(String key, String value)
{
if (Dictionary.Contains(key) != false)
return;
Dictionary.Add(key, value);
}
public bool contains(String key)
{
return (Dictionary.Contains(key));
}
public void Remove(String key)
{
Dictionary.Remove(key);
}
protected override void OnInsert(object key, object value)
{
if (key.GetType() != typeof(System.String))
throw new ArgumentException("键值必须是一个字符串类型。", "key");
else
{
String strKey = (String)key;
//if (strKey.Length < 10)
// throw new ArgumentException("键值不能少于10个字符");
}
if (value.GetType() != typeof(System.String))
throw new ArgumentException("键值必须是一个String类型", "value");
else {
String strValue = (String) value;
//if ( strValue.Length > 5 )
// throw new ArgumentException( "value must be no more than 5 characters in length.", "value" );
}
}
protected override void OnRemove( Object key, Object value ) {
if ( key.GetType() != typeof(System.String) )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
//if ( strKey.Length > 5 )
// throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}
}
protected override void OnSet( Object key, Object oldValue, Object newValue ) {
if ( key.GetType() != typeof(System.String) )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
//if ( strKey.Length > 5 )
// throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}
if ( newValue.GetType() != typeof(System.String) )
throw new ArgumentException( "newValue must be of type String.", "newValue" );
else {
String strValue = (String) newValue;
//if ( strValue.Length > 5 )
// throw new ArgumentException( "newValue must be no more than 5 characters in length.", "newValue" );
}
}
protected override void OnValidate( Object key, Object value ) {
if ( key.GetType() != typeof(System.String) )
throw new ArgumentException( "key must be of type String.", "key" );
else {
String strKey = (String) key;
//if ( strKey.Length > 5 )
// throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
}
if ( value.GetType() != typeof(System.String) )
throw new ArgumentException( "value must be of type String.", "value" );
else {
String strValue = (String) value;
//if ( strValue.Length > 5 )
// throw new ArgumentException( "value must be no more than 5 characters in length.", "value" );
}
}
#endregion
}
}
|
|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理