当前位置: 代码迷 >> .NET Framework >> comboBox解决思路
  详细解决方案

comboBox解决思路

热度:91   发布时间:2016-05-02 00:35:35.0
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;
}
  相关解决方案