请问combobox里面有没有类似datagridview的tag

蓝色水 2007-10-12 11:29:30
请问combobox里面有没有类似datagridview的tag,这个tag是针对每个items
...全文
183 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京的雾霾天 2007-10-12
  • 打赏
  • 举报
回复
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;
蓝色水 2007-10-12
  • 打赏
  • 举报
回复
谢谢,我后来自己做了个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


}
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧