以下を選択できるタイムピッカーの解決策を探しています。
00:30 > 01:00 > 01:30
23:30 に達すると、0:00 にラップアラウンドする必要があります。
つまり、上または下を選択して、30 分の期間を増やす必要があります。私はバーを組み込んhscroll
で修正しようとしましたtimepicker
が、これは非常にデリケートであり、より簡単な方法があるに違いないと思うので、私の見解では不必要ですか?
どんな提案も素晴らしいでしょう。
これを行うためにコントロールをサブクラス化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
私が考えることができる 1 つの方法は、統合された高さが 1 つの項目に設定された ListBox と、統合されたリストボックスのスクロールバーの上に配置された別の垂直スクロールバーを備えたユーザー コントロールを使用することです。
ListBox に可能な値を入力します (例: 00:00
to ) 23:30
。
スクロールバーの Scroll イベントでは、 と の比較を使用しe.OldValue
てListBoxe.NewValue
のプロパティを増分または減分し、適切な項目が表示され、上下にスクロールしているように見えます。TopIndex
次に、最初または最後のアイテムが表示されているかどうかを確認できますが、スクロールバーはそのイベントでスクロールを登録しないため、最初または最後のアイテムを超えて 1 つ以上のアイテムを移動して、ラップアラウンドを続けているように見えるようにする必要があります。 scolbar は常にアクティブで、その Scroll イベントを発生させます。
私が過去に行ったことは、リスト (2 列) を作成し、コンボボックスにバインドすることでした。最初の列は時刻を表す単なる文字列でした... 2 番目の列はそれに対応する double 値でした。次に、コンボボックス、時間表現に基づいた DISPLAY 値がありますが、実際の値は double 値に基づいています...
元:
Display Actual
00:00 = 0.0
00:30 = 0.5
01:00 = 1.0
....
12:30 = 12.5
13:00 = 13.0
etc.
それからしばらく時間が経ちましたが、0 から 23.50 まで 0.5 ずつ増加する単純なループを介して生成できます (夜の 23:30)。
または、日付のみを選択する日付/時刻ピッカーが 1 つあり、その隣に時刻 00.00、00.30 ... 23.00、23.30 を持つ ComboBox がある単純なソリューションを実行できます。日付ピッカーがあなたが探している正確な機能を備えているソリューションを検討しています。もっと良いものが見つかったら、この投稿を編集します。
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;
}