date[inc] = [0] = "01/01/2011"
dateTimePicker1.Value = {01/01/2011 00:00:00}
更改辅助日期时间选择器时,永远不会满足if条件。在此代码生效之前,必须更改第一个datetimepicker。在这两种情况下,此代码都由ValueChanged
触发。
有什么想法吗?
编辑:
int start;
int inc = 0;
while (true)
{
inc++;
if (Convert.ToString(dateTimePicker1.Value.ToShortDateString()) == date[inc])
{
start = inc;
inc = 0;
break;
}
}
= / =
int start;
int inc = 0;
while (true)
{
if (Convert.ToString(dateTimePicker1.Value.ToShortDateString()) == date[inc])
{
start = inc;
inc = 0;
break;
}
inc++;
}
仍然非常有帮助的评论,谢谢大家。显然由于增量而错过了第一个日期,导致无限循环。
答案 0 :(得分:1)
你正在进行字符串比较:
"01/01/2011" != "1/1/2011"
答案 1 :(得分:1)
尝试:
if (dateTimePicker1.Value.ToString("MM/dd/yyyy") == date[inc])
// ...
答案 2 :(得分:1)
尝试像这样比较
if (dateTimePicker1.Value.ToString("MM/dd/yyyy",
new System.Globalization.CultureInfo("en-US") == date[inc])
OR
//Considering your date format is MM/dd/yyyy, coz 01/01/2011 doesn't help in uderstanding
//in date format, if it would have been 01/13/2011, it would have helped
if (dateTimePicker1.Value.Date ==
DateTime.ParseExact(date[inc], "MM/dd/yyyy",
new System.Globalization.CultureInfo("en-US")).Date)
您也可以.Trim()
date[inc]
对string
进行帮助。
修改强>
比较.Trim()
要查找的内容时
string.Equals(string1, string2, StringComparison.InvariantCultureIgnoreCase)
希望这适合你。
答案 3 :(得分:0)
如果字符串永远不相等,那么就没有任何条件可以突破