C#用一个组合框切换语句?

时间:2011-01-28 19:54:12

标签: c# asp.net combobox

目前我正在尝试使用switch语句在时区与学校项目之间进行更改。

if (extTime1.timeZone == "CDT")
{
   switch (cboTimeZone.SelectedItem.ToString)
   {
      case "EST":
      time1.Hour = time1.Hour + 1; /* CDT hours + 1 to get EST */
      extTime1.Hour = extTime1.Hour + 1;
      break;

      case "MST":
      time1.Hour = time1.Hour - 1; /* CDT hours - 1 to get MST */
      break;

      case "PST":
      time1.Hour = time1.Hour - 2; /* CDT hours - 2 to get PST */
      break;

      default: /* CDT is the default time zone*/
      break;
  }

}

我似乎无法使cboTimeZone正常工作。在这种情况下,我一直认为SelectedItem是正确的选择。显然不是吗?

2 个答案:

答案 0 :(得分:1)

使用.Text。 asp.net ajax组合框实际上是文本框和下拉列表的组合,它实现了ITextControl接口。

答案 1 :(得分:0)

让我看看这个:

if (extTime1.timeZone == "CDT")
{
   // Try this
   string text = cboTimeZone.SelectedItem.ToString(); // don't forget the parenthesis
   MessageBox.Show(text, "Debug");
   // Then this
   text = cboTimeZone.Text;
   MessageBox.Show(text, "Debug");
   // then (as a safeguard)
   if (String.IsNullOrEmpty(text))
   {
       return;
   }
   switch (text)
   {
      case "EST":
      time1.Hour = time1.Hour + 1; /* CDT hours + 1 to get EST */
      extTime1.Hour = extTime1.Hour + 1;
      break;

      case "MST":
      time1.Hour = time1.Hour - 1; /* CDT hours - 1 to get MST */
      break;

      case "PST":
      time1.Hour = time1.Hour - 2; /* CDT hours - 2 to get PST */
      break;

      default: /* CDT is the default time zone*/
      break;
  }

}