C# - 代码不工作,不明白为什么,datetime转换 [英] C# - Code not working and don't understand why, datetime converting
问题描述
这是我的代码
this is my code
int i = 0;
foreach (string item in Properties.Settings.Default.DatesEventNames)
{
//add the headings
if ((i++ % 3) == 0)
{
//add label
Label neweventlabel = new Label();
neweventlabel.Content = item;
SP1.Children.Add(neweventlabel);
DateTime yourDateTime = DateTime.Parse(item + 1);
DateTime yourDateTime2 = DateTime.Parse(item + 2);
//add progressbar
ProgressBar pb = new ProgressBar();
pb.Maximum = (yourDateTime - yourDateTime2).TotalDays;
pb.Value = (yourDateTime - DateTime.Now.Date).TotalDays;
pb.Height = 20;
SP1.Children.Add(pb);
}
}
错误是,只有发布了一个标签,它也意味着发布进度条,所以我发现错误的位置是
the error is, only the one label is posted, its meant to post the progressbar too, so I found out where the error is it is
DateTime yourDateTime = DateTime.Parse(item + 1);
DateTime yourDateTime2 = DateTime.Parse(item + 2);
但我不明白为什么它不起作用。
这就是字符串中的样子
but I don''t understand why its not working.
this is what it looks like in the string collection
eventname<br />
9/01/2013<br />
20/01/2013
所以这就是程序中的样子
eventname
| ----- progressbar - 价值----- |
它只是它的视觉效果。
可以请一些人帮我解决问题?
so this is what its ment to look like in the program
eventname
|-----progressbar-with the values-----|
its just a ruff visual of it.
can some one please help me solve the problem?
推荐答案
它无法正常工作,无法编译。什么是你的问题,羞于承认呢? :-)
这只是乱码:DateTime.Parse(item + 1); item
是字符串,这是正确的,因为方法Parse
需要一个字符串参数,但你不能在字符串中加1。 />
甚至不清楚+1可能意味着什么。加一天?一小时?如果这是这样的,你应该首先解析item
以获取DateTime
的实例,然后使用其中一个函数在结果时间内增加一些时间间隔。
-SA
It is not working, it cannot compile. What''s your problem, ashamed to admit it? :-)
This is just gibberish:DateTime.Parse(item + 1); item
is string, that''s correct, because the methodParse
expects a string parameter, but you cannot add 1 to a string.
It''s not even clear what could you probably meant by "+1". Adding a day? an hour? If this was something like this, you should first parseitem
to obtain an instance ofDateTime
and then use one of the functions adding some time span to the resulting time.
—SA
根据你的评论你应该尝试以下代码来解决这个问题。
As per your comments you should try the following code to solve this.
DateTime yourDateTime = DateTime.Parse(Properties.Settings.Default.DatesEventNames[1]);
DateTime yourDateTime2 = DateTime.Parse(Properties.Settings.Default.DatesEventNames[2]);
表示您在访问此集合时应该小心的时间。检查数组计数,否则它会破坏你的应用程序。
mean time you should be careful in accessing this collection. check the array count else it blows your application.
这篇关于C# - 代码不工作,不明白为什么,datetime转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!