2013-05-28 30 views
7

我要尋找一個解決方案,一個時間選擇器,讓我來選擇以下:尋找半時間選擇器控件每小時向上/向下

00:30 > 01:00 > 01:30 

當它到達23:30它需要環繞到0:00。

換句話說,我需要通過選擇向上或向下來增加半小時。我試過合併hscroll欄和修改timepicker,但在我看來,這是相當敏感和不必要的,因爲我懷疑必須有更簡單的方法嗎?

任何建議都非常好。

+3

也許你可以找到你正在使用的UI框架的東西。那是什麼,WPF,WinForms,ASP.NET,MVC/Razor? - 編輯:哇,這個問題4 upvotes。爲什麼?我看到幾乎沒有研究。那裏有大量的UI框架。看看[Telerik](http://www.telerik.com/help/winforms/editors-timepicker-overview.html)或[這個問題](http://stackoverflow.com/questions/1046268/time - 只薦換網的WinForms)。 – CodeCaster

+0

我的腦海中浮現出的第一件事是Enumerable.Range,但我很想看看最終解決方案的實際外觀。 –

+0

對不起@CodeCaster見編輯 – CR41G14

回答

6

我只是子類的一個DomainUpDown控制要做到這一點,下面的代碼:

class TimePicker : DomainUpDown 
{ 
    public TimePicker() 
    {   
     // build the list of times, in reverse order because the up/down buttons go the other way 
     for (double time = 23.5; time >= 0; time -= 0.5) 
     { 
      int hour = (int)time; // cast to an int, we only get the whole number which is what we want 
      int minutes = (int)((time - hour) * 60); // subtract the hour from the time variable to get the remainder of the hour, then multiply by 60 as .5 * 60 = 30 and 0 * 60 = 0 

      this.Items.Add(hour.ToString("00") + ":" + minutes.ToString("00")); // format the hour and minutes to always have two digits and concatenate them together with the colon between them, then add to the Items collection 
     } 

     this.SelectedIndex = Items.IndexOf("09:00"); // select a default time 

     this.Wrap = true; // this enables the picker to go to the first or last item if it is at the end of the list (i.e. if the user gets to 23:30 it wraps back around to 00:00 and vice versa) 
    } 
} 

將控件添加到您的形式,像這樣:

TimePicker picker1; 

public Form1() 
{ 
    InitializeComponent(); 

    picker1 = new TimePicker(); 
    picker1.Name = "timePicker"; 
    picker1.Location = new Point(10, 10); 

    Controls.Add(picker1); 
} 

然後,當我們想要得到的選擇的時間(我在這裏使用一個按鈕),只需使用SelectedItem屬性:

private void button1_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show(picker1.SelectedItem.ToString()); // will show "09:00" when 09:00 is selected in the picker 
} 

DomainUpDown的文檔:http://msdn.microsoft.com/en-us/library/system.windows.forms.domainupdown.aspx

+0

+1'for'循環有點痛,但實現得很好。 –

+0

@Monkieboy我無聊用手輸入時間xD – Sean

+0

這條線上的錯誤this.SelectedIndex = times.IndexOf(「09:00」) – CR41G14

0

或者你可以做一個簡單的解決方案,你有一個日期時間選擇器,只選擇日期和它旁邊的時間00.00,00.30 ... 23.00,23.30的組合框。我正在尋找一個解決方案,日期選擇器將具有您正在尋找的確切功能。如果我找到更好的東西,將編輯這篇文章。

編輯:

不知道這是有分量的NumericUpDown但是如果你擁有它,你可以用它使用的值改變事件的.NET版本。下面是一些示例代碼:

decimal previousValue = 0; 
    DateTime time = new DateTime(2000, 6, 10, 0, 0, 0); 
    bool justChanged = false; 
    private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
    { 
     if (justChanged) 
     { 
      justChanged = !justChanged; 
      return; 
     } 
     else 
      justChanged = !justChanged; 

     if (numericUpDown1.Value < previousValue) 
      time = time.AddMinutes(-30); 
     else 
      time = time.AddMinutes(30); 

     numericUpDown1.Value = decimal.Parse(time.ToString("HH,mm")); 

     previousValue = numericUpDown1.Value; 
    } 
+0

我不需要在這種情況下的日期,只有時間。我可以使用一個下拉菜單,會看到會發生什麼。謝謝 – CR41G14

1

我在過去做過的事情是建立了一個列表(2列)並綁定到組合框。第一列只是表示時間的字符串......第二列是與其對應的雙倍值。然後,combboox,我會根據時間表示的顯示值,而是基於雙值與實際值...

例如:

Display Actual 
00:00 = 0.0 
00:30 = 0.5 
01:00 = 1.0 
.... 
12:30 = 12.5 
13:00 = 13.0 
etc. 

它已經因爲這一段時間,但可以通過一個簡單的循環以0.5的增量生成,從0到23.50(23:30晚)

1

我可以想到的一種方式是使用帶有整體高度設置爲一個項目和一個列表框的用戶控件單獨的垂直滾動條放置在集成列表框滾動條的頂部。

用可能的值填充列表框,例如00:0023:30

在滾動條的滾動事件使用e.OldValuee.NewValue比較遞增或遞減列表框的TopIndex屬性,以使得示出此時,相應的項,並將其顯示爲向上或向下滾動。

然後,您可以檢查是否顯示第一個或最後一個項目,但由於滾動條在該事件中不會註冊滾動,因此您應該移動一個或多個項目,使其看起來像保留環繞和scollbar始終處於活動狀態並引發Scroll事件。