使用C#中的时间间隔填充下拉列表 [英] populate the dropdown list with time interval in C#
本文介绍了使用C#中的时间间隔填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个以时间间隔填充下拉列表的函数。例如,当用户点击下拉列表时,应该有:
I am seeking a function to populate the dropdown list with time interval. For example, when user clicks the dropdown list, there should be like:
8:00AM
8:30AM
9:00AM
....
12:00PM
12:30PM
如何在C#中执行此操作?
How can I do this in C#?
推荐答案
请参阅以下链接simillar问题: -
http://social.msdn.microsoft.com/Forums/en-US/ca21c56a-079e-438a-9a27-5e20a8471d82/how-to-bind- dropdown-box-with-time-interval?forum = csharpgeneral [ ^ ]
refer below link for simillar question:-
http://social.msdn.microsoft.com/Forums/en-US/ca21c56a-079e-438a-9a27-5e20a8471d82/how-to-bind-dropdown-box-with-time-interval?forum=csharpgeneral[^]
试试这个:
Try this:
private void populateDropdown(ComboBox dropdown, DateTime startTime, DateTime endTime, TimeSpan interval)
{
dropdown.Items.Clear();
DateTime time = startTime;
while (time <= endTime)
{
dropdown.Items.Add(time.ToString("HH:mm tt"));
time = time.Add(interval);
}
}
然后这样打电话:
Then call it like this:
populateDropdown(comboBox1, new DateTime(2000, 1, 1, 8, 0, 0), new DateTime(2000, 1, 1, 12, 30, 0), new TimeSpan(0, 30, 0));
除了以上两个解决方案,
你也可以尝试这个..
On top of above two solutions,
you can try this too..
DateTime start = DateTime.ParseExact(txtStart.Text,"HH:mm tt", null);
DateTime end = DateTime.ParseExact(txtEnd.Text, "HH:mm tt", null);
int interval = 30;
List<string> lstTimeIntervals = new List<string>();
for (DateTime i = start; i < end; i =i.AddMinutes(interval))
lstTimeIntervals.Add(i.ToString("HH:mm tt"));
dropdownList.DataSource = lstTimeIntervals;
dropdownList.DataBind();
这篇关于使用C#中的时间间隔填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文