首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

comboBox解决思路

2012-04-08 
comboBoxpublic class ComboxItem{public string Text, Valuepublic ComboxItem(string _Text, string _V

comboBox
public class ComboxItem
  {
  public string Text, Value;
  public ComboxItem(string _Text, string _Value)
  {
  Text = _Text;
  Value = _Value;
  }
  public override string ToString()
  {
  return Text;
  }

  }

 InsurePeriods ye = new ManagePolicy().GetInsurePeriods();
  for (int i = 0; i < ye.Count; i++)
  {
  this.comboBox2.Items.Add(new ComboxItem(ye[i].Year.ToString(),ye[i].iYear.ToString()));//Text,Value
  }
  if (ye.Count > 0)
  {
  this.comboBox1.SelectedIndex = 0;
  }

comboBox只显示Text时,我如何取到ye[i].iYear的值,也就是Value。

[解决办法]
string v=(string)combox.SelectedValue ;
[解决办法]
if(comboBox2.SelectedItem != null)
MessageBox.Show((comboBox2.SelectedItem as ComboxItem).Value);
[解决办法]
public class ComboxItem
{
public string Text, Value;
public ComboxItem(string _Text, string _Value)
{
Text = _Text;
Value = _Value;
}
public override string ToString()
{
return Text;
}
}

private void Form1_Load(object sender, EventArgs e)
{
string[] ye = new string[] { "2001,5", "2002,15"};
for (int i = 0; i < ye.Length; i++)
{
this.comboBox1.Items.Add(new ComboxItem(ye[i].Split(',')[0], ye[i].Split(',')[1]));//Text,Value
}
if (ye.Length > 0)
{
this.comboBox1.SelectedIndex = 0;
}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
label1.Text=(comboBox1.SelectedItem as ComboxItem).Value;
}

热点排行